/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------  */
/* Functions to care form validation / popup / form submit for the marketing ad at the bottom of the home page */

function popUpMessage ( url, w, h ) { 
	args = "innerWidth=" + w + ",";
	args += "innerHeight=" + h + ",";
	args += "location=0" + ","
	args += "menubar=0" + ","
	args += "resizable=0" + ","
	args += "scrollbars=0" + ","
	args += "status=0" + ","
	args += "titlebar=0" + ","
	args += "toolbar=0" + ","
	args += "hotkeys=0" + ","
	
	var demo_window = window.open(url, "main", args);
	demo_window.focus();
}

function pop_conf () {
    var browser_name = navigator.appName;
	var width = 450;
	var height = 600;
	if (browser_name.indexOf("WebTV") != -1) return null;
	if (parseInt(navigator.appVersion) >= 4 )
	{xposition = (screen.width - width) / 2;
		yposition = (screen.height - height) / 2;
	}

	args = "innerWidth=" + width + ",";
	args += "innerHeight=" + height + ",";
	args += "location=0" + ","
	args += "menubar=0" + ","
	args += "resizable=0" + ","
	args += "scrollbars=0" + ","
	args += "status=0" + ","
	args += "titlebar=0" + ","
	args += "toolbar=0" + ","
	args += "hotkeys=0" + ","
	args += "screenx=" + xposition + ",";
	args += "screeny=" + yposition + ",";
	args += "left=" + xposition + ",";
	args += "top=" + yposition;

	var name = document.botform.cds_name_b.value;
	var address1 = document.botform.cds_address_1_b.value;
	var address2 = document.botform.cds_address_2_b.value;
	var city = document.botform.cds_city_b.value;
	var state = document.botform.cds_state_b.value;
	var zip = document.botform.cds_zip_b.value;
	var email = document.botform.cds_email_b.value;
	
	var error = validateform(name, address1, address2, city, state, zip, email);
	
	if (error){
		alert(error);}
	else{

		if (address2 == "Address (Line 2)"){address2 = "";}
	
		var fields = "cds_name=" + name + "&cds_address_1=" + address1 + "&cds_address_2=" + address2 +"&cds_city=" + city + "&cds_state=" + state + "&cds_zip=" + zip + "&cds_email=" + email;
	
		var conf_page = "/ads/freetrial_CID_conf.asp?";
		var conf_page = conf_page + fields;
		
		var demo_window = window.open(conf_page, "main", args);
			demo_window.focus();
	}
}

function validateform(name, address1, address2, city, state, zip, email)
{
    var error = '';
	var descriptionType = '';

	if ((name == "") || (name == "Name"))
	{ error = error + 'Please enter Full Name.\n' }
	
	if ((address1 == "") || (address1 == "Address (Line 1)"))
	{error = error + 'Please enter Address.\n'; }
	
	if ((city == "") || (city == "City")) 
	{error = error + 'Please enter City.\n'; }

	if ((state == "") || (state == "State")) 
	{error = error + 'Please enter State.\n'; }

	if ((zip == "") || (zip == "Zip Code")) 
	{error = error + 'Please enter Zip.\n'; }
	
	if ((email == "") || (email == "E-mail (required)"))
	{error = error + 'Please enter Email.\n'; }

	if (!isEmail(email))
	{error = error + 'Please enter valid Email.\n'; }

	if(error) {
	error = 'The following error(s) occurred:\n'+error;}
	else {error = ''; }
	
	return error;
}

function isEmail(str) {
	  // are regular expressions supported?
	  var supported = 0;
	  if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	  }
	  if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
}
function collectcdsb(link)
{

//pull data from the CDS form to send to the Prepop Gateway
var name = document.botform.cds_name_b.value;
var address1 = document.botform.cds_address_1_b.value;
var address2 = document.botform.cds_address_2_b.value
var city = document.botform.cds_city_b.value;
var state = document.botform.cds_state_b.value;
var zip = document.botform.cds_zip_b.value;
var email = document.botform.cds_email_b.value;

var error = validateform(name, address1, address2, city, state, zip, email);

if (error){
	alert(error);}
else{
	if (address2 == "Address (Line 2)"){address2 = "";}

	var fields = "&cds_name=" + name + "&cds_address_1=" + address1 + "&cds_address_2=" + address2 +"&cds_city=" + city + "&cds_state=" + state + "&cds_zip=" + zip + "&cds_email=" + email;
	var cdslink = link + fields
	
	window.open(cdslink, "window");
	}

}
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------  */





/* Other jQuery Stuff */
$(document).ready(function(){
	var lastVal = ""; // Global var to hold the last state of a field contents
	$(".shadowedInput:not(.static)").focus(function(){
		lastVal = $(this).val();
		$(this).val("");
	});
	$(".shadowedInput:not(.static)").blur(function(){
		if ($(this).val() == ""){
		$(this).val(lastVal);
		} else { 
			$(this).unbind('focus');
	}
	});
	
	
	// Use the fucking miracle of jQuery to fix the SBS layout by finding the 5th sibling 
	// and clearing it all the way to the left. 
	$("#rightCol > .pageSection > .detailColumn:first").next().next().next().next().attr("style","clear: left;");
});


