//preload images
jQuery.preloadImages = function(){for(var i = 0; i<arguments.length; i++){jQuery("<img>").attr("src", arguments[i]);}}
$.preloadImages("images/common/form_back_bot.png", "images/common/form_back_top.png","images/common/form_back_top.png");

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popForm() {

	if($('#chat_wrap').css('top')=='0px') {
	$('#chat_wrap').animate({top:"-500px"},500);	
	}
	
	$('#promo_content').css("position","absolute"); 
	$('#promo_content').css("top","-1000px"); 
	$('#promo_content').css("left","-1000px"); 
	
	$('#overlay').height($(document).height());
	$('#overlay').show();
	$('#form_wrap').show();
	$('#form_reveal').animate({height:$(document).height()},500,function() { 
	
	$('.submit').css("position","relative"); 
	$('.submit').css("top","0px"); 
	$('.submit').css("left","0px"); 
	
	$('.submit').fadeIn(200);
	
	
	});	
	
	pageTracker._trackPageview('/salesforcepopup');
}

function closePop() {

	$('#overlay').height('100%');
	$('#overlay').hide();
	$('#form_wrap').hide();
	$('#form_reveal').height('0px');
	$('.submit').css("position","absolute"); 
	$('.submit').css("top","-1000px"); 
	$('.submit').css("left","-1000px");
	$('#promo_content').css("position","relative"); 
	$('#promo_content').css("top","0"); 
	$('#promo_content').css("left","0");
	
	//reset the chat timer
	window.setTimeout(function() { openChat(); }, 30000);
	
}

function salesForm() {
	
	//reset error variable
	var error = "";
	
	//reset all the labels to no error
	$("#salesform label").removeClass('error_label');
	
	
	//check to see if any of the fields are empty
	if($("#f_name").val()=="") {
		$("#f_name_lbl").addClass('error_label');
		error = 'req';}
	
	if($("#l_name").val()=="") {
		$("#l_name_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#biz_name").val()=="") {
		$("#biz_name_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#email").val()=="") {
		$("#email_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#phone_no").val()=="") {
		$("#phone_no_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#products").val()=="") {
		$("#products_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#website").val()=="") {
		$("#website_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#ticket").val()=="") {
		$("#ticket_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#volume").val()=="") {
		$("#volume_lbl").addClass('error_label');
		error = 'req';}	
	
	//if all the fields are filled, validate email address
	if(error=="") {
		var mess = validemail($("#email").val());
	
	if(mess==false) {	
		error='valid';
		$("#email_lbl").addClass('error_label');}
	}
		
	
	if(error=='req') {	
		$("#salesformalert").addClass('form_error_alert');
		$("#salesformalert").text('Please fill out all the required fields.');
	
	} else if(error=='valid') {
		$("#salesformalert").addClass('form_error_alert');
		$("#salesformalert").text('Please enter a valid email address.');
	} else {
	
	///send email
	var form_values = $("#salesform").serialize();
	
	
	//ajax email function
	$.ajax({ 
   		type: "GET", 
   		url: "salesForce.php", 
   		data: form_values, 
   		success: function(msg){ 
	   		pageTracker._trackPageview('/salesforcesuccess');
			 $("#salesformalert").addClass('form_success_alert');
			 $("#salesformalert").text('Thank you, we have received your information and will be contacting you shortly.');
			 $("#salesform input").val('');
			 $("#salesform textarea").val('');
   		} 
 		});
	
}}


function contactForm() {
	
	//reset error variable
	var error = "";
	
	//reset all the labels to no error
	$("#contactform label").removeClass('error_label');
	
	
	//check to see if any of the fields are empty
	if($("#con_name").val()=="") {
		$("#con_name_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#con_biz_name").val()=="") {
		$("#con_biz_name_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#con_email").val()=="") {
		$("#con_email_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#con_phone").val()=="") {
		$("#con_phone_lbl").addClass('error_label');
		error = 'req';}
		
	if($("#con_comments").val()=="") {
		$("#con_comments_lbl").addClass('error_label');
		error = 'req';}
	
	
	//if all the fields are filled, validate email address
	if(error=="") {
		var mess = validemail($("#con_email").val());
	
	if(mess==false) {	
		error='valid';
		$("#con_email_lbl").addClass('error_label');}
	}
		
	
	if(error=='req') {	
		$("#contactformalert").addClass('form_error_alert');
		$("#contactformalert").text('Please fill out all the required fields.');
		return false;
	
	} else if(error=='valid') {
		$("#contactformalert").addClass('form_error_alert');
		$("#contactformalert").text('Please enter a valid email address.');
		return false;
	} else {
	
	return true;
	
}}



function validemail(email) {
	var AtPos = email.indexOf("@");
	var StopPos = email.lastIndexOf(".");
	var error = "";

	if (AtPos == -1 || StopPos == -1) {
	error = 1;}
	
	if (StopPos < AtPos) {
	error = 1;}
	
	if (StopPos - AtPos == 1) {
	error = 1;}
	
	if(error==1) {
	return false;	
	} else {
	return true;	
	}
}