


$(document).ready(function() {
	
	/* init */
			   			   
	if ($("#gs_sales_inquiry").is(":checked")) { 
		$(".gs_sales_only").show();		
	} else {
		$(".gs_sales_only").hide();
	}
	
	if ($("#prod_sales_inquiry").is(":checked")) { 
		$(".prod_sales_only").show();		
	} else {
		$(".prod_sales_only").hide();
	}
	
	
	/* actions */
	$("#prod_frm_submit").click(function() { validateFrm("prod"); });
	$("#gs_frm_submit").click(function() { validateFrm("gs"); });

	$("#gs_sales_inquiry").click(function() { 
		 gs_type_selected = "sales";						   								
		$(".gs_sales_only").show();
		$("#gs_inquiry_label").text("Services Required and Project Description:");
	});	
	
	$("#gs_support_inquiry").click(function() { 
		  gs_type_selected = "support";	
		$(".gs_sales_only").hide();
		$("#gs_inquiry_label").text("Inquiry / Feedback:");
	});	
		
	$("#gs_comments_inquiry").click(function() {
		 gs_type_selected = "comments"
		$(".gs_sales_only").hide();
		$("#gs_inquiry_label").text("Inquiry / Feedback:");
	});	
 
	$("#prod_sales_inquiry").click(function() { 
		 prod_type_selected = "sales";					   									
		$(".prod_sales_only").show();
		$("#prod_inquiry_label").text("Services Required and Project Description:");
	});	
	
	$("#prod_support_inquiry").click(function() { 
		prod_type_selected = "support";
		$(".prod_sales_only").hide();
		$("#prod_inquiry_label").text("Inquiry / Feedback:");
	});	
		
	$("#prod_comments_inquiry").click(function() {
		prod_type_selected = "comments";
		$(".prod_sales_only").hide();
		$("#prod_inquiry_label").text("Inquiry / Feedback:");
	});	
	
	
	
			// GS AOI click
	   		$(".gs_aoilist").click(function() {
					if ($("#gs_cb_other_aoi").is(':checked') != true) {
						 $("#gs_other_aoi").val("");
						 $("#gs_other_aoi_req").hide();
		 
					} else { 
					   $("#gs_other_aoi_req").show();
					   $("#gs_other_aoi").focus();
					}
			});		 
	   

			// GS AOI page load state
			if ($("#gs_cb_other_aoi").is(':checked') != true) {
			
				 $("#gs_other_aoi").val("");
				 $("#gs_other_aoi_req").hide();
			} 
			
 

			// GS Lead Source click
			$(".gs_leadlist").click(function() {
				
					if ($("#gs_cb_other_lead_source").is(':checked') != true) {
						 $("#gs_other_lead_source").val("");
						$("#gs_other_lead_source_req").hide();
					}  else { 
						$("#gs_other_lead_source").focus();
						$("#gs_other_lead_source_req").show();
					}
			});
	
			// GS Lead Source page load state
			if ($("#gs_cb_other_lead_source").is(':checked') != true) {
				 $("#gs_other_lead_source").val("");
			} 
	
			// GS AOI click
	   		$(".prod_aoilist").click(function() {
					if ($("#prod_cb_other_aoi").is(':checked') != true) {
						 $("#prod_other_aoi").val(" ");
						 //$("#prod_other_aoi_req").hide();
		 
					} else { 
					  // $("#prod_other_aoi_req").show();
	 
					   $("#prod_other_aoi").focus();
					}
			});		 
	   

			// prod AOI page load state
			if ($("#prod_cb_other_aoi").is(':checked') != true) {
				 $("#prod_other_aoi").val("");
				// $("#prod_other_aoi_req").hide();
			} 
			
 

			// prod Lead Source click
			$(".prod_leadlist").click(function() {
				
					if ($("#prod_cb_other_lead_source").is(':checked') != true) {
						 $("#prod_other_lead_source").val("");
					}  else { 
						 $("#prod_other_lead_source").focus();
					}
			});
	
			// GS Lead Source page load state
			if ($("#prod_cb_other_lead_source").is(':checked') != true) {
				 $("#prod_other_lead_source").val("");
				 $("#gs_other_lead_source_req").hide();
			} 









 
	
	/* functions */
	
//	function setState(which) {
//		 
//	if (($("#"+which+"_country").val() == "United States") || ($("#"+which+"_country").val() == "Canada") ) {
//					$("#"+which+"_state_holder").fadeIn();
//				} else {
//					$("#"+which+"_state").val("");
//					$("#"+which+"_state_holder").fadeOut();
//				}
//	}

	$("#gs_country").change(function() { setState("gs"); });
	$("#prod_country").change(function() { setState("prod"); });
 
	setState("gs");
	setState("prod");


 	function setState(which) { 
 				   
			if (($("#"+which+"_country").val() == "United States") || ($("#"+which+"_country").val() == "Canada") ||  ($("#"+which+"_country").val() == "United Kingdom")) {
 
 
 	 		$.ajax({ 
				   	async: false,
				   	url: "/handlers/get_states.php?c="+$("#"+which+"_country").val(),
					context: document.body, 
				   	success: function(data){ 
						$("#"+which+"_state").html(data);
					}
			 });
 
				$("#"+which+"_state_holder").fadeIn();
 
				
			} else {
				 
				$("#"+which+"_state").val("");
				$("#"+which+"_state_holder").hide();
			}
 
	}



		 
function validateFrm(which) {
	
	var missing_list = "";
	var required = new Array("email", "company", "first_name", "last_name", "phone", "country");
	
	   var missing = 0;
 
 // type check...
    if ( $("#"+which+"_sales_inquiry").is(":checked") || $("#"+which+"_comments_inquiry").is(":checked") || $("#"+which+"_support_inquiry").is(":checked"))  {
	// ok!	
} else {
	alert("Please select a topic for your inquiry.");
	return false;		  
}


 
 

	   

	  

 	  // aoi is required
	   aoi = 0;
	   if ($(".gs_aoilist").is(":checked")) {
			 aoi++;
	   }
	   
	   if ($(".prod_aoilist").is(":checked")) {
			 aoi++;
	   }
	   
	   if (aoi < 1) {
			missing++;   
				missing_list += " aoi | ";
	   }

		
			// enforce aoi other for gs
			 if ($("#gs_cb_other_aoi").is(":checked")) {
				if ($("#gs_other_aoi").val().length < 1) {
					missing++;
					missing_list += "aoi_other | ";
				}
			}	
			
			
		  if ($("#prod_cb_other_aoi").is(":checked")) {
				if ($("#prod_other_aoi").val().length < 1) {
					missing++;
					missing_list += "aoi_other (prod) | ";
				}
			}	
			
 // sales?
   if ( $("#"+which+"_sales_inquiry").is(":checked") ) {			
	   	required.push("city");
        required.push("address");
		required.push("phone");
		
		if ( ($("#"+which+"_country").val() == "United States") || ($("#"+which+"_country").val() == "Canada") ) { 
            	required.push("state");
        }   
 
 
 	 if (which == "gs") {
	
		  
			if (!($(".gs_leadlist").is(":checked"))) {
					missing++;
					missing_list += "lead_source | ";
		   }
		   	   //if other: lead source selected, make theother field reqd
		   if ($("#gs_cb_other_lead_source").is(":checked")) {
				if (!($("#gs_other_lead_source").val())) {
					missing++;
					missing_list += "lead_source_other | ";
				}
			}
			
 
			
	 }
 
 
   }
   
 
 
 
 	 // look for empty required inputs
 	
 	  $.each(required, function(i, v) {
			if (!($("#"+which+"_"+v).val())) {
 			//if (!($("#"+v).val())) {
				missing++;
				missing_list += v + " | ";
			}
	  });



	  
 
 

	if (missing > 0) {
 	  alert ("Please check that a response has been\nprovided for all required fields.");	
		//   alert (missing_list);	
	  		  return false;
	} else { 
	
		
	    // check valid email
		if (checkEmail($("#"+which+"_email").val())) {
		 	
		
		// if gs... go to SFDC
		if (which == "gs") {

			if ( $("#gs_sales_inquiry").is(":checked") ) {
				$("#street").val($("#gs_address").val()+"\n"+$("#gs_address2").val()+"\n"+$("#gs_address3").val());

			// btamilio 04/20/2010 -- no longer needed to go directly
			// $("#gs_frm").attr("action", "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8");
			}

		} else {
		
			$("#street").val($("#prod_address").val()+"\n"+$("#prod_address2").val()+"\n"+$("#prod_address3").val());
			
		}
	
			   $("#"+which+"_frm").submit();
			   return true;
		} else { 
			  alert ("Email address does not seem valid.");	
	  		  return false;
		}
	      
	}
 
 
   // alert($("#inquiry_type").val());

 
 
}



}); 