﻿$(function(){
		   
	Cufon.replace('h1.r');
 	//Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h2.r');
	Cufon.replace('h3');
	Cufon.replace('#featured_slide p.header');
	Cufon.replace('dt');
	
	//validateSendEmailForm();
});

function validateSendEmailForm(){
    
									
		var nullNameError="Please insert your name";
		var nullCompanyError="Please insert company name";
		var nullEmailError="Please insert your email";
		var nullPhoneError="Please insert phone number";
		var invalidEmailError="Please insert a valid email address";
		var nullStateError2="Please insert your state";
		var urlToPhp = "";
		var valid=true;
		
		$("#nameError").text("");
		$("#emailError").text("");  
		$("#companyError").text("");
		$("#phoneError").text("");
		$("#stateError2").text("");
		valid=true;  
		
		if(document.getElementById("nameTextBox").value=="" || document.getElementById("nameTextBox").value==null){
			$("#nameError").text(nullNameError);
			valid=false;
		}
		
		if(document.getElementById("companyTextBox").value=="" || document.getElementById("companyTextBox").value==null){
			$("#companyError").text(nullCompanyError);
			valid=false;
		}
		
		if(document.getElementById("phoneTextBox").value=="" || document.getElementById("phoneTextBox").value==null){
			$("#phoneError").text(nullPhoneError);
			valid=false;
		}
		
		var reg = "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/";
		var email = document.getElementById("emailTextBox").value;
		if(!(email.indexOf(".") > 0) || !(email.indexOf("@") > 0)) {
			$("#emailError").text(invalidEmailError); 
			valid=false;
		}
		
		if(document.getElementById("emailTextBox").value=="" || document.getElementById("emailTextBox").value==null){
			$("#emailError").text(nullEmailError);
			valid=false;
		}
		
		//verify whether the state is empty or not filled out
		if(document.getElementById("state2").value=="" || document.getElementById("state2").value==null){
			$("#stateError2").text(nullStateError2);
			valid=false;
		}
		
		var name=document.getElementById("nameTextBox").value;
		var question=document.getElementById("questionTextArea").value;
		var email = document.getElementById("emailTextBox").value;
		var company = document.getElementById("companyTextBox").value;
		var phone = document.getElementById("phoneTextBox").value;
		var referralURL = document.getElementById("referringURL").value;
		var pageURL = document.getElementById("pageURL").value;
		
		var state = document.getElementById("state2").value;
		

		var product ="";
		if(document.getElementById("group").checked == 1){
			product = " Group Edition ";
		}
			
		if(document.getElementById("professional").checked == 1){
			product = product + " Professional Edition ";
		}
		
		if(document.getElementById("enterprise").checked == 1){
			product = product + " Enterprise Edition ";
		}
		

		if(valid==true){
			var postDetails = "formEmail=" + email + "&formName=" + name + "&formQuestion=" + question + "&formCompany=" + company + "&formProduct=" + product + "&formPhone=" + phone + "&referralURL=" + referralURL + "&pageURL=" + pageURL + "&state=" + state + "&valid=" + valid;
			
			
			urlToPhp="http://www.contractlogix.com/form_post.asp?" + postDetails;
			
			$.ajax({  
				type: "POST",  
				url: urlToPhp,  
 				data: postDetails,
				success: function() {  
				
				$("#message").html("<span class=\"message\"><img src=\"images/loading.gif\" />Thank You " + name + "! Submitting...<br /></span>");
				$("#formThanks").html("<span><h3>Thank You " + name + "!</h3><br />Your demo request has been submitted. We\'ll contact you momentarily to schedule your demo!</span>");
				
				//$("#formThanks").append('<div><img height="1" width="1" style="border-style:none;" src="http://www.googleadservices.com/pagead/conversion/1022896321/?label=6Z_zCJWD5wEQwdHg5wM&amp;guid=ON&amp;script=0"/></div>');


				function ppcconversion() {
				var iframe = document.createElement('iframe');
					iframe.style.width = '0px';
					iframe.style.height = '0px';
					document.body.appendChild(iframe);
					iframe.src = 'http://www.contractlogix.com/_track/gAdwords.html';
				};
				
				ppcconversion();

				setTimeout('$("#formLeft").slideUp("slow");$("#formRight").slideUp("slow");', 3000);
				setTimeout('$("#formThanks").slideDown("slow");', 3000);
				setTimeout('$("#message").hide();$("#panel").slideUp("slow");$("#toggle a").toggle()', 5000);

				$("#submitForm").each(function(){
					this.reset();
					valid=false;
				});
				} 

			}); 
		}
}


;jQuery.preloadCssImages = function(settings){
	settings = jQuery.extend({
		statusTextEl: null,
		statusBarEl: null,
		errorDelay: 999, // handles 404-Errors in IE
		simultaneousCacheLoading: 2
	}, settings);
	var allImgs = [],
		loaded = 0,
		imgUrls = [],
		thisSheetRules,	
		errorTimer;
	
	function onImgComplete(){
		clearTimeout(errorTimer);
		if (imgUrls && imgUrls.length && imgUrls[loaded]) {
			loaded++;
			if (settings.statusTextEl) {
				var nowloading = (imgUrls[loaded]) ? 
					'Now Loading: <span>' + imgUrls[loaded].split('/')[imgUrls[loaded].split('/').length - 1] : 
					'Loading complete'; // wrong status-text bug fixed
				jQuery(settings.statusTextEl).html('<span class="numLoaded">' + loaded + '</span> of <span class="numTotal">' + imgUrls.length + '</span> loaded (<span class="percentLoaded">' + (loaded / imgUrls.length * 100).toFixed(0) + '%</span>) <span class="currentImg">' + nowloading + '</span></span>');
			}
			if (settings.statusBarEl) {
				var barWidth = jQuery(settings.statusBarEl).width();
				jQuery(settings.statusBarEl).css('background-position', -(barWidth - (barWidth * loaded / imgUrls.length).toFixed(0)) + 'px 50%');
			}
			loadImgs();
		}
	}
	
	function loadImgs(){
		//only load 1 image at the same time / most browsers can only handle 2 http requests, 1 should remain for user-interaction (Ajax, other images, normal page requests...)
		// otherwise set simultaneousCacheLoading to a higher number for simultaneous downloads
		if(imgUrls && imgUrls.length && imgUrls[loaded]){
			var img = new Image(); //new img obj
			img.src = imgUrls[loaded];	//set src either absolute or rel to css dir
			if(!img.complete){
				jQuery(img).bind('error load onreadystatechange', onImgComplete);
			} else {
				onImgComplete();
			}
			errorTimer = setTimeout(onImgComplete, settings.errorDelay); // handles 404-Errors in IE
		}
	}
	
	function parseCSS(sheets, urls) {
		var w3cImport = false,
			imported = [],
			importedSrc = [],
			baseURL;
		var sheetIndex = sheets.length;
		while(sheetIndex--){//loop through each stylesheet
			
			var cssPile = '';//create large string of all css rules in sheet
			
			if(urls && urls[sheetIndex]){
				baseURL = urls[sheetIndex];
			} else {
				var csshref = (sheets[sheetIndex].href) ? sheets[sheetIndex].href : 'window.location.href';
				var baseURLarr = csshref.split('/');//split href at / to make array
				baseURLarr.pop();//remove file path from baseURL array
				baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
				if (baseURL) {
					baseURL += '/'; //tack on a / if needed
				}
			}
			if(sheets[sheetIndex].cssRules || sheets[sheetIndex].rules){
				thisSheetRules = (sheets[sheetIndex].cssRules) ? //->>> http://www.quirksmode.org/dom/w3c_css.html
					sheets[sheetIndex].cssRules : //w3
					sheets[sheetIndex].rules; //ie 
				var ruleIndex = thisSheetRules.length;
				while(ruleIndex--){
					if(thisSheetRules[ruleIndex].style && thisSheetRules[ruleIndex].style.cssText){
						var text = thisSheetRules[ruleIndex].style.cssText;
						if(text.toLowerCase().indexOf('url') != -1){ // only add rules to the string if you can assume, to find an image, speed improvement
							cssPile += text; // thisSheetRules[ruleIndex].style.cssText instead of thisSheetRules[ruleIndex].cssText is a huge speed improvement
						}
					} else if(thisSheetRules[ruleIndex].styleSheet) {
						imported.push(thisSheetRules[ruleIndex].styleSheet);
						w3cImport = true;
					}
					
				}
			}
			//parse cssPile for image urls
			var tmpImage = cssPile.match(/[^\("]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename" / '"' for opera-bugfix
			if(tmpImage){
				var i = tmpImage.length;
				while(i--){ // handle baseUrl here for multiple stylesheets in different folders bug
					var imgSrc = (tmpImage[i].charAt(0) == '/' || tmpImage[i].match('://')) ? // protocol-bug fixed
						tmpImage[i] : 
						baseURL + tmpImage[i];
					
					if(jQuery.inArray(imgSrc, imgUrls) == -1){
						imgUrls.push(imgSrc);
					}
				}
			}
			
			if(!w3cImport && sheets[sheetIndex].imports && sheets[sheetIndex].imports.length) {
				for(var iImport = 0, importLen = sheets[sheetIndex].imports.length; iImport < importLen; iImport++){
					var iHref = sheets[sheetIndex].imports[iImport].href;
					iHref = iHref.split('/');
					iHref.pop();
					iHref = iHref.join('/');
					if (iHref) {
						iHref += '/'; //tack on a / if needed
					}
					var iSrc = (iHref.charAt(0) == '/' || iHref.match('://')) ? // protocol-bug fixed
						iHref : 
						baseURL + iHref;
					
					importedSrc.push(iSrc);
					imported.push(sheets[sheetIndex].imports[iImport]);
				}
				
				
			}
		}//loop
		if(imported.length){
			parseCSS(imported, importedSrc);
			return false;
		}
		var downloads = settings.simultaneousCacheLoading;
		while( downloads--){
			setTimeout(loadImgs, downloads);
		}
	}
	parseCSS(document.styleSheets);
	return imgUrls;
};





//Document Ready to do Stuff
$(document).ready(function() {
						   
		$('#chooseEdition').easyAccordion({ 
			autoStart: true, 
			slideInterval: 6000,
			slideNum: false
		});
						   
	$(function () {
			
			var counter = 0,
				divs = $('#changeText1, #changeText2, #changeText3, #changeText4');
			
			function showDiv () {
				divs.hide() // hide all divs
					.filter(function (index) { return index == counter % 4; }) // figure out correct div to show
					.show('fast'); // and show it
					
				counter++;
			}; // function to loop through divs and show correct div
			
			showDiv(); // show first div    
			
			start_int = setInterval(function () {
				showDiv(); // show next div
			},  6000); // do this every 10 seconds    
				
		});
		
		$("#chooseEdition").click(function(){
			clearInterval(start_int);
		});	
		$("#chooseyouredition").click(function(){
			clearInterval(start_int);
			$("#changeText2").hide("fast");
			$("#changeText3").hide("fast");
			$("#changeText4").hide("fast");
			setTimeout('$("#changeText1").show("fast");', 500);
		});	
		$("#groupedition").click(function(){
			clearInterval(start_int);
			$("#changeText1").hide("fast");
			$("#changeText3").hide("fast");
			$("#changeText4").hide("fast");
			setTimeout('$("#changeText2").show("fast");', 500);

		});	
		$("#professionaledition").click(function(){
			clearInterval(start_int);
			$("#changeText1").hide("fast");
			$("#changeText2").hide("fast");
			$("#changeText4").hide("fast");
			setTimeout('$("#changeText3").show("fast");', 500);
		});	
		$("#enterpriseedition").click(function(){
			clearInterval(start_int);
			$("#changeText1").hide("fast");
			$("#changeText2").hide("fast");
			$("#changeText3").hide("fast");
			setTimeout('$("#changeText4").show("fast");', 500);
		});	

						   
						   
	
	$("#open").click(function(){
		$("div#panel").slideDown("slow");	
	});	
	
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});		
	
	$("#marquee1").marquee({fxEasingShow: "swing",fxEasingScroll: "linear", pauseOnHover: true});
	
	$("#formThanks").css("display","none");
	
	
	$('#ourClients').tinycarousel({pager: true, interval: true, animation: true, intervaltime: 3000, display:3});
	
	$("#sendButton").click(function(){
				validateSendEmailForm();
		});
	
});



//GOOGLE ANALYTICS - IMPORTANT
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-13295766-3']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

