// JavaScript Document

  // wait for the DOM to be loaded 
        $(document).ready(function() { 
								   
		
   // Enable hover effect on the style bar buttons.
  $('#bar .query-button').hover(function() {
  $("#queryhdr").attr({src : "http://www.cast-inc.com/graphics/page/query-btn_f2.png"});
    $(this).addClass('hover');
  }, function() {
  $("#queryhdr").attr({src : "http://www.cast-inc.com/graphics/page/query-btn.png"});
    $(this).removeClass('hover');
  });
 	
		// testing presetting cores field
		//var coresOfInterest = $(".querylink").text();
		//$('#Cores').val(coresOfInterest);
		
		$('#reset').click(function() {
			val.resetForm();
			$("#content").text("");
			
		});
	
	// uees the jquery validate function to validate form - places the errors after label elements
		var val = $("#myForm").validate({
					errorPlacement: function(error, element) {
						error.insertAfter(element.siblings("label"));
					},
					errorClass: "invalid",
						event: "submit",
						rules: {
							Company: {
								required: true
							},
							
							Cores: {
								required: true
							},
							First_Name: {
								required: true
							},
							Last_Name: {
								required: true
							},
							Phone: {
								required: true
							},
							Country: {
								required: true
							},
							Email: {
								required: true,
								email: true
							}
							
						},
						messages: {
							Company: {
								required: 'Please enter your company name.'
							},
							
							Cores: {
								required: 'Please enter the cores you are interested in.'
							},
							First_Name: {
								required: 'Please enter your first name.'
							},
							Last_Name: {
								required: 'Please enter your last name.'
							},
							
							Phone: {
								required: 'Please enter a phone number.'
							},
							Country: {
								required: 'Please enter your country.'
							},
							Email:  'Please enter a valid e-mail address.'
							}
					});

		
		
    // bind form using 'ajaxForm' 
	
	var options = {
		success: function() {
			_gaq.push(['_trackPageview', '/queryFormSubmitted'+window.parent.location]);
			
        },
		
        beforeSubmit: function() {
			
		expireDate = new Date;
		expireDate.setMonth(expireDate.getMonth()+6);
		document.cookie = "firstName="+escape($("#First_Name").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "lastName="+escape($("#Last_Name").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "company="+escape($("#Company").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "email="+escape($("#Email").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "Title="+escape($("#title").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "Address1="+escape($("#address1").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "Address2="+escape($("#address2").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "City="+escape($("#city").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "State="+escape($("#state").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "Zip="+escape($("#zip").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "phone="+escape($("#Phone").val())+";expires=" + expireDate.toGMTString();
		document.cookie = "country="+escape($("#Country").val())+";expires=" + expireDate.toGMTString();
	
		
		$("#content").text("");
        return $('#myForm').validate().form();
        },
        target: '#content'
    };
    $('#myForm').ajaxForm(options);			

	});
		
		
/* Store the form field entries in cookies before sending the form entries to the server */   
function submitForm1(coreInfoForm){
	expireDate = new Date;
	expireDate.setMonth(expireDate.getMonth()+6);
	document.cookie = "firstName="+escape(coreInfoForm.First_Name.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "lastName="+escape(coreInfoForm.Last_Name.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "company="+escape(coreInfoForm.Company.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "email="+escape(coreInfoForm.Email.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "Title="+escape(coreInfoForm.title.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "Address1="+escape(coreInfoForm.address1.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "Address2="+escape(coreInfoForm.address2.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "City="+escape(coreInfoForm.city.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "State="+escape(coreInfoForm.state.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "Zip="+escape(coreInfoForm.zip.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "phone="+escape(coreInfoForm.Phone.value)+";expires=" + expireDate.toGMTString();
	document.cookie = "country="+escape(coreInfoForm.Country.value)+";expires=" + expireDate.toGMTString();
	return true;	  	
	
	}
	
	
	
	function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
	
	function GetCookie (name){
	var result="";
	var myCookie = " " + document.cookie + ";";
	var searchName = " " + name + "=";	
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1) {
	startOfCookie += searchName.length;
	endOfCookie = myCookie.indexOf(";",startOfCookie);
	result=unescape(myCookie.substring(startOfCookie,endOfCookie));
	}
	return result;
	}




$(function() {
		$('.get-info a').click(function(e) {
			e.preventDefault();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
	            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
	            autoOpen: true,
	            width: 900,
	            height: 450,
	            modal: true,
				position: 'top',
	            resizable: true,
				autoResize: true,
	            overlay: {
	                opacity: 0.5,
	                background: "black"
	            }
	        }).width(900 - horizontalPadding).height(450 - verticalPadding);
			
		 	return false;
		});
	});

$(function() {
		$('.site-info a').click(function(e) {
			e.preventDefault();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
	            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
	            autoOpen: true,
	            width: 1100,
	            height: 450,
	            modal: true,
	            resizable: true,
				autoResize: true,
	            overlay: {
	                opacity: 0.5,
	                background: "black"
	            }
	        }).width(1100 - horizontalPadding).height(450 - verticalPadding);	        
		});
		
	});

	
$(function() {
		$('.learn-more a').click(function(e) {
			e.preventDefault();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
	            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
	            autoOpen: true,
	            width: 1100,
	            height: 450,
	            modal: true,
	            resizable: true,
				autoResize: true,
	            overlay: {
	                opacity: 0.5,
	                background: "black"
	            }
	        }).width(1100 - horizontalPadding).height(450 - verticalPadding);	        
		});
		
	});

$(function() {
		$('.silicon-choices a').click(function(e) {
			e.preventDefault();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
	            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
	            autoOpen: true,
	            width: 960,
	            height: 450,
	            modal: true,
	            resizable: true,
				autoResize: true,
	            overlay: {
	                opacity: 0.5,
	                background: "black"
	            }
	        }).width(960 - horizontalPadding).height(450 - verticalPadding);	        
		});
		
		$('.silicon-overview a').click(function(e) {
			e.preventDefault();
			var $this = $(this);
			var horizontalPadding = 30;
			var verticalPadding = 30;
	        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
	            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
	            autoOpen: true,
	            width: 960,
	            height: 450,
	            modal: true,
	            resizable: true,
				autoResize: true,
	            overlay: {
	                opacity: 0.5,
	                background: "black"
	            }
	        }).width(960 - horizontalPadding).height(450 - verticalPadding);	        
		});
		
	});



/*

$(function() {
/* This sets the properties of the dialog window that we are displaying the query form in. */		
/*$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 500,
			width: 660,
			modal: true,
			position: 'top',
			// clear the form response when you close the dialog window
			close: function() {
				$("#content").text("");
				
			}
		});
		
		
/* Give the query button class get-info */
/* First thing that happens is the form is pre-loaded with any existing cookies saved from previous form submissions */
/* Second thing that happens is a dialog window is opened and the form is displayed in it */
/* Third thing that happens is event handlers are set for the query buttons but we are not using this  */

/*		$('.get-info').click(function() {
		 $('#First_Name').val(GetCookie("firstName"));
		 $('#Last_Name').val(GetCookie("lastName"));
		 $('#Company').val(GetCookie("company"));
		 $('#Email').val(GetCookie("email"));
		 $('#title').val(GetCookie("Title"));
		 $('#address1').val(GetCookie("Address1"));
		 $('#address2').val(GetCookie("Address2"));
		 $('#city').val(GetCookie("City"));
		 $('#state').val(GetCookie("State"));
		 $('#zip').val(GetCookie("Zip"));
		 $('#Phone').val(GetCookie("phone"));
		 $('#Country').val(GetCookie("country"));
		 $('#dialog').dialog('open');
	     $('form').show();
		 return false;
		})
		/* style the button here - we are not using for this application
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		}); */

/*	});  */
