/*
'--------------------------------------------------------------------------------------------------
' Title			: Site Wide JavaScript
' Description	: JavaScript functions and declarations for the entire site
'--------------------------------------------------------------------------------------------------
' History
' 12/29/2009	: DPE - Created Page
'--------------------------------------------------------------------------------------------------
*/

$(document).ready(function(){
	
	initZipCodeSearch() //Setup the zip code search box
	initSiteSearch()	//Setup the site search box

	$("#dialog").dialog({
		modal: true,
		autoOpen: false,
		resizable: false
	});

	$("#confBox").dialog({
		modal: true,
		autoOpen: false,
		height: 500,
		width: 1000
	});

	$("#link").click(function() {
		$('#dialog').dialog('open');
		return false;
	});

	$("#confLink").click(function() {
		$('#confBox').dialog('open');
		return false;
	});
	/* example use for id based solution
	$("#link").click(function() {
	$('#dialog').dialog('open');
	return false;
	});
	//*/
});

// Controls and validates the ZipCode Search form
function initZipCodeSearch(){
	
	if($("#ZipCodeSearchQuery").attr("value") == '' || $("#ZipCodeSearchQuery").attr("value") == 'Zip Code...'){
		$("#ZipCodeSearchQuery").attr({value:"Zip Code..."});
		$("#ZipCodeSearchQuery").css({color:"#616161"});
	}
	
	// Validate the form before submission...
	$("#ZipCodeSearchBtn").click(
		function(){ // Validate search field...
			
			var searchValue = jQuery.trim($("#ZipCodeSearchQuery").attr("value"));
			
			if(searchValue == '' || searchValue == 'Zip Code...' || searchValue.length != 5 || (searchValue != parseInt(searchValue))) {
				alert('Please enter a valid 5-digit zip code.');
				$("#ZipCodeSearchQuery").focus();
				return false;
			} else {
				//return false;
				$("#ZipCodeSearchForm").submit();
			}
			
		}
	);
	
	$("#ZipCodeSearchQuery").focus(
		function(){ // highlight and clear the search field
			if($(this).attr("value") == 'Zip Code...'){
				$(this).attr({value:""});
			}
			$(this).css({'color':'#000','font-style':'normal'});
		}
	);
	$("#ZipCodeSearchQuery").blur(
		function(){ // highlight and clear the search field
			if($(this).attr("value") == '' || $(this).attr("value") == 'Zip Code...'){
				$(this).attr({value:"Zip Code..."});
				$(this).css({'color':'#616161','font-style':'italic'});
			}
		}
	);
	
}


// Controls and validates the ZipCode Search form
function initSiteSearch(){
	
	if($("#SiteSearchQuery").attr("value") == '' || $("#SiteSearchQuery").attr("value") == 'Search our site...'){
		$("#SiteSearchQuery").attr({value:"Search our site..."});
		$("#SiteSearchQuery").css({color:"#616161"});
	}
	
	// Validate the form before submission...
	$("#SiteSearchBtn").click(
		function(){ // Validate search field...
			
			var searchValue = jQuery.trim($("#SiteSearchQuery").attr("value"));
			
			if(searchValue == '' || searchValue == 'Search our site...' ) {
				alert('Please enter a valid search term.');
				$("#SiteSearchQuery").focus();
				return false;
			} else {
				//return false;
				$("#SiteSearchForm").submit();
			}
			
		}
	);
	
	$("#SiteSearchQuery").focus(
		function(){ // highlight and clear the search field
			if($(this).attr("value") == 'Search our site...'){
				$(this).attr({value:""});
			}
			$(this).css({'color':'#000','font-style':'normal'});
		}
	);
	$("#SiteSearchQuery").blur(
		function(){ // highlight and clear the search field
			if($(this).attr("value") == '' || $(this).attr("value") == 'Search our site...'){
				$(this).attr({value:"Search our site..."});
				$(this).css({'color':'#616161','font-style':'italic'});
			}
		}
	);
	
}



	function screamIn(obj){
		$('#dialog').dialog('open');
		var passTxtObj = document.getElementById('pass_txt_');
		var isMenuObj = document.getElementById('is_menu_');
		passTxtObj.value = obj.innerHTML;
		isMenuObj.value = 0;
	}

	function screamInMenu(obj){
		$('#dialog').dialog('open');
		var passTxtObj = document.getElementById('pass_txt_');
		var isMenuObj = document.getElementById('is_menu_');
		passTxtObj.value = obj.innerHTML;
		isMenuObj.value = 1;
	}

	function screamInImg(obj){
		$('#dialog').dialog('open');
		var passTxtObj = document.getElementById('pass_txt_');
		var isMenuObj = document.getElementById('is_menu_');
		passTxtObj.value = obj.alt;
		isMenuObj.value = 1;
	}

	function screamInSlideShow(obj){
		$('#dialog').dialog('open');
		var passTxtObj = document.getElementById('pass_txt_');
		var isMenuObj = document.getElementById('is_menu_');
		passTxtObj.value = obj.id;
		isMenuObj.value = 0;
	}

	function validateContactForm(obj){
		var passedObj = document.getElementById(obj).value;
		var passedErrorObj = document.getElementById(obj + "_error");

		switch(obj){
			case "email":
				var regexp = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				var regx = true;
				break;
			case "phone":
			case "homephone":
			case "businessphone":
			case "businessphone_2":
			case "mobilephone":
				var regexp = /^\(\d{3}\) \d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$|^\d{3} \d{3}-\d{4}$/;
				var regx = true;
				break;
			case "zip":
			case "b_zip":
				var regexp = /^\d{5}$/;
				var regx = true;
				break;
			default:
				break;
		}

		if(regx == true){
			if(passedObj != "" && (regexp.test(passedObj))){
				//return valid image
				passedErrorObj.innerHTML = "<span style=\"vertical-align:top; padding-left: 10px;\" id="+passedErrorObj+"><img src=\"/national/includes/pass.gif\" border=\"0\" /></span>";
			}else{
				//return error image
				passedErrorObj.innerHTML = "<span style=\"vertical-align:top; padding-left: 10px;\" id="+passedErrorObj+"><img src=\"/national/includes/fail.gif\" border=\"0\" /></span>";
			}
		}else{
			if(passedObj != ""){
				passedErrorObj.innerHTML = "<span style=\"vertical-align:top; padding-left: 10px;\" id="+passedErrorObj+"><img src=\"/national/includes/pass.gif\" border=\"0\" /></span>";
			}else{
				passedErrorObj.innerHTML = "<span style=\"vertical-align:top; padding-left: 10px;\" id="+passedErrorObj+"><img src=\"/national/includes/fail.gif\" border=\"0\" /></span>";
			}
		}
	}

	function checkForms(obj){
		var x=document.getElementById(obj);
		var tmpBool = true;
		for (var i=0;i<x.length;i++)
		{
			if (x.elements[i].value == '' && x.elements[i].id != "uword"){
//				document.write(x.elements[i].id);
//				document.write("<br />");
				document.getElementById("form_error").innerHTML = "Form must be completed.";
				tmpBool = false;
				return false;
			}
		}
		return jcap();
	}

	function enableSubmit()
	{
		if (document.data.confidentiality.checked == true) {
			document.getElementById('submitButton').innerHTML = '<input type=\"submit\" class=\"buttons\" value=\"Submit This Inquiry\" />';
		} else {
			document.getElementById('submitButton').innerHTML = '<input type=\"submit\" disabled class=\"buttons\" value=\"Submit This Inquiry\" />';
		}
		return false;
	}
	/*
	function screamOut(zipId){
		var zipObj = document.getElementById(zipId);
		var zipCode = zipObj.value;
		if (zipCode != ''){
			xajax_setInit(zipCode);
		}
		$('#dialog').dialog('close');
	}
	//*/
