$(document).ready(function() {

  $("form.fContactUs input[type='submit']").live("click", function() {
		
		//validate the form based on the form's class. All forms so far share the class "conForm".
		//unfortunately, all the IDs are the same too.  Someone enjoys typing repeatedly.  Each form will need
		//to add a unique class name, so we have a unique form instance.  
		return validateForm("fContactUs");
    });
		
		$("form.fnewsletter input[type='submit']").live("click", function() {
		//validate the form based on the form's class. All forms so far share the class "conForm".
		//unfortunately, all the IDs are the same too.  Someone enjoys typing repeatedly.  Each form will need
		//to add a unique class name, so we have a unique form instance.  
		return validateForm("fnewsletter");
    });
		$("form.fsurvey input[type='submit']").live("click", function() {
		//validate the form based on the form's class. All forms so far share the class "conForm".
		//unfortunately, all the IDs are the same too.  Someone enjoys typing repeatedly.  Each form will need
		//to add a unique class name, so we have a unique form instance.  
		return validateForm("fsurvey");
    });
		
		
		
//alert("Click gallery");
		$('#gallery div').mouseover(function(){
			//alert("Click gallery");
			$('#popOver').css("display", "block");
			var img = $(this).attr("img");
			var des = $(this).attr("des");
			var top = $(this).attr("top" );
			var left = $(this).attr("left");
			//alert("Click gallery " + top + "  " + left);
			
			 $('#popOver').css("margin-top", top);
			 $('#popOver').css("margin-left", left);
			
			 $('#popOver').html(" <img height='263' width='400' src='/images/gallery/"+ img +"'><p>"+  des +"</p>");
			
		}); 
	$('#gallery div').mouseout(function(){
			 $('#popOver').css("display", "none");
});



// Home page//

 var numRand = Math.floor(Math.random()*3)

	if(numRand == 0){
		
		$('#pw-ctn').addClass(" pm black");
		$('#pos').html("<p>Our clients routinely tell us how much they value our proactive project management approach.</p><p>Count on us to help you accelerate your most pressing NCE, API, or custom chemistry project.</p>");
		var urlLink ="/services/project_management.php";
	}
	if(numRand == 1){
		$('#pw-ctn').addClass(" rd right hgFFF")
		$('#pos').html("Our experienced R&D team is ready to help you accelerate your most pressing NCE, API, or custom chemistry project.");
		var urlLink ="/services/index.php";
	}
	if(numRand == 2){
		$('#pw-ctn').addClass(" aw right hgFFF")
		$('#pos').html("");
		var urlLink ="/capabilities/index.php";
	}
	
	$('#POA').click(function(){
															 
 	 	window.location = urlLink;
	 
	}); 
	
	
	
	
// Products Page form
$('.title-ctn').click(function(){
		var podID = 	$(this).attr("title");
		var podTitle = 	$(this).attr("name");
		var podNum = 	$(this).attr("prodNum");
		//alert(podTitle + "  "+ podID + "  "+ podNum);
			
 	 $('.cpop').css("display","block");
	 $('.cpop').css("top","10px");
	 $('.modal').css("display","block");
	 $('html').scrollTop(0);
	 
	 $('#ajax-contact-form').populate({
			'ChemicalName': podTitle,
			'CASNumber': podID,
			'ProductNum': podNum
			 
		})
	}); 
									 
		
		
		
// career Page form
$('#applyT').click(function(){
		var posTitle = 	$(this).attr("title");
		//alert(posTitle);
			
 	 $('.cpop').css("display","block");
	 $('.cpop').css("top","10px");
	 $('.modal').css("display","block");
	 
	 $('#ajax-contact-form').populate({
			'position': posTitle
		})
	 
	}); 



$('.cpop span').click(function(){
															 
 	 $('.cpop').css("display","none");
	 $('.modal').css("display","none");


	 
	});



});

//validation functions

function validateForm(formID){

		var isvalid = true;
		var v="";
		var requiredfields="";
		var rf = new Array;
		
		switch(formID)
			{
			case "fContactUs":
				//list of required fields for this form...
				requiredfields="firstName,lastName,email";
				rf=requiredfields.split(",");
			  break;
			case "fnewsletter":
				//list of required fields for this form...
				requiredfields="firstName,lastName,email";
				rf=requiredfields.split(",");
			  break;
			case "fsurvey":
				//list of required fields for this form...
				requiredfields="customername,project";
				rf=requiredfields.split(",");
			  break;

			default:
			  //no default action currently.
			}

//now do the specific form validation....
		for (i=0;i<=rf.length-1;i++)
		{
			v=spaceFix($("#" + rf[i]).val());
			if(v.length==0)
			{
				$("#" + rf[i]).css("border-color","#CC0000");
				$("#" + rf[i]).css("background-color","#fff6f6");
				$(".formError").show();
				//i'd like to style the label as well but the page is coded in a way that makes this difficult.  Boohoo
				isvalid=false;
				
			} 
/*	
else
			
			{ alert("len: " + v.length + " -" +  v);}
			
			*/
			
			
		}  //end for
		
		
		return isvalid;
}

function spaceFix(v){
	v = v.replace(" ","~@~")	;
	v = v.replace("~@~~@~","~@~");
	v = v.replace("~@~~@~","~@~");
	v = v.replace("~@~"," ");
	v=wsTrim(v);
	return v;
}

function wsTrim(s) {
	s = s.replace(/(^\s*)|(\s*$)/gi,"");
	s = s.replace(/[ ]{2,}/gi," ");
	s = s.replace(/\n /,"\n");
	return s;
}






