/*
	This file handles contains both global functions and initialization for the HEG site.
	NOTE: This file depends on '/scripts/gea_global.js' and '/scripts/jquery.js' (from GEA root)
*/

$(document).ready(function() {
	
	$("input.hasInstructionMessage").each(function() {
		if ($(this).attr("value") == $(this).attr("defaultValue")) {
			$(this).css("color","#B2B2B2");
        }
		if ($(this).attr("value") == "") {
			$(this).attr("value", $(this).attr("defaultValue"));
			$(this).css("color","#B2B2B2");
        }
	});
	
	$("input.hasInstructionMessage").focus(function() {
		if ($(this).attr("value") == $(this).attr("defaultValue")) {
			$(this).attr("value", "");
			$(this).css("color","#333333");
        }
	});
	
	$("input.hasInstructionMessage").blur(function() {
		if ($(this).attr("value") == "") {
			$(this).attr("value", $(this).attr("defaultValue"));
			$(this).css("color","#B2B2B2");
        }
	});
	
	var currentPage = $('body').attr('class');
	var selectedSpan = "span." + currentPage;
	if(currentPage != ""){
		$(selectedSpan).addClass('selected');
	}
	
	$("table:has(.shademe) tr:odd").addClass('shademe');
	
});



function isValidEmailAddress (string) {
	return /^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/.test(string);
}

function isValidZip(string)	{
	return /^\d{5}$/.test(string);
}

// for email subscribe form in footer
function validateFooterSubscribeForm (form) {
	if (!isValidEmailAddress(form.i_emailgeneric.value)) {
		window.alert("Please enter a valid email address.");
		form.i_emailgeneric.focus();
		form.i_emailgeneric.select();
		return false;
	}
	if(form.i_hmpostcde.value != form.i_hmpostcde.defaultValue) {
		if (!isValidZip(form.i_hmpostcde.value)) {
			window.alert("Please enter a valid zip code.");
			form.i_hmpostcde.focus();
			form.i_hmpostcde.select();
			return false;
		}
	} else {
		form.i_hmpostcde.value = "";
	}
	
	return true;
}




// number of current product image on spec pages (defaulted to first image)
var currentSpecImage = 0;

/* this functions sets the selected thumbnail and populates the larger image
	Cloned and modified version of "setMainImage" from GEA Global JS
 */
function setMainHEGImage(num) {
	
	// check the limits
	if( num < 0 ) { num = productImagesArray.length -1 }
	if( num >= productImagesArray.length ) { num = 0; }
	
	// set selected thumbnail
	document.getElementById("productThumbnail"+currentSpecImage ).childNodes[0].className = "";
	document.getElementById("productThumbnail"+num ).childNodes[0].className = "doubleBorder";
	
	// write main image
	mainImageSrc = productImagesArray[num][1];
	document.getElementById("mainImageContainer").innerHTML = "<img id=\"mainImage\" src=\"" + mainImageSrc + "\" width=\"480\" height=\"500\" alt=\"\" title=\"\" />";
	
	currentSpecImage = num;
}

/* this function populates the thumbnails and larger image on spec pages
	Cloned and modified version of "populateProductImages" from GEA Global JS */
function populateHEGProductImages() {
	// hide arrows and thumbs if only one image is available
	if(productImagesArray.length == 1) {
		document.getElementById("thumbnailsViewer").style.display = "none";
	}
	
	// populate the thumbnails
	for(i=0; i<productImagesArray.length; i++) {
		document.getElementById("thumbnailsViewer").innerHTML += "<a id=\"productThumbnail" + i + "\" href=\"javascript:setMainHEGImage(" + i + ");\"><img src=\"" + productImagesArray[i][0] + "\" width=\"60\" height=\"63\" alt=\"View larger\" title=\"View larger\" /></a> ";
	}
	
	// set the first thumbnail as selected and populate the larger images
	setMainHEGImage(currentSpecImage);
}


/********* FUNCTIONS FOR CONTACT FORM *********/

//check for tab functions
function checkForTab(evt, fone1, fone2)
{
	if(document.layers) {
		if (evt.target.value.length >= 3) {
			fone2.focus();
		}
	} else {
		if (fone1.value.length > 2) {
			fone2.focus();
		}
	}
}

function validPositiveInt(fld,minDigits,maxDigits) {
	regEx = new RegExp("^[0-9]{"+minDigits+","+maxDigits+"}$");
	if(fld.value.search(regEx) == -1) { 
		return false; }
	return true;
}

function validEmail(fld) {
	if(fld.value.search(/^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/) == -1) {
		return false; }
	return true;
}

// form validation functions
function validateContact(fm) {
	if(fm.txt01First_Name.value=="") { alert("Please enter your first name."); fm.txt01First_Name.select(); return false; }
	if(fm.txt02Last_Name.value=="") { alert("Please enter your last name."); fm.txt02Last_Name.select(); return false; }
	if(!validPositiveInt(fm.phone1,3,3)) { alert("Please enter a valid phone number."); fm.phone1.select(); return false; }
	if(!validPositiveInt(fm.phone2,3,3)) { alert("Please enter a valid phone number."); fm.phone2.select(); return false; }
	if(!validPositiveInt(fm.phone3,4,4)) { alert("Please enter a valid phone number."); fm.phone3.select(); return false; }
	fm.txt08Phone.value = fm.phone1.value + "-" + fm.phone2.value + "-" + fm.phone3.value;
	fm.txt09Alternate_Phone.value = fm.Alternate_Phone1.value + "-" + fm.Alternate_Phone2.value + "-" + fm.Alternate_Phone3.value;
	fm.txt10Fax.value = fm.fax1.value + "-" + fm.fax2.value + "-" + fm.fax3.value;
	if(!validEmail(fm.txt11EMail_Address)) { alert("Please enter a valid email address."); fm.txt11EMail_Address.select(); return false; }
	if(fm.txt13Comments.value=="") { alert("Please enter your comments."); fm.txt13Comments.select(); return false; }
	// now submit the form
	return true;
}
