var NEWLINE = "\n";


function validateContactForm(form) {
	var errors = "";

	if (isBlank(form.name.value)) {
		errors += "Please enter your name." + NEWLINE;
	}
	if (isBlank(form.company.value)) {
		errors += "Please enter your company name." + NEWLINE;
	}
	if (isBlank(form.title.value)) {
		errors += "Please enter your title." + NEWLINE;
	}
	if (isBlank(form.phone.value)) {
		errors += "Please enter your telephone number." + NEWLINE;
	}
	if (isBlank(form.email.value)) {
		errors += "Please enter E-mail address." + NEWLINE;
	}
	else if (echeck(form.email.value)==false){
		errors += "Invalid Email format. Please correct the e-mail address." + NEWLINE;
	}

	return checkErrors(errors);
}


function validateMLForm(form) {
	var errors = "";

	if (isBlank(form.email.value)) {
		errors += "Please enter E-mail address." + NEWLINE;
	}
	else if (echeck(form.email.value)==false){
		errors += "Invalid Email format. Please correct the e-mail address." + NEWLINE;
	}

	return checkErrors(errors);
}


function trim(inputString) {
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { 
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { 
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}
	return retValue; 
}
 
function isBlank(field) {
	if (trim(field).length == 0) {
		return true;
	}
	return false
}

function checkErrors(errors) {
	if (errors.length > 0 ) {
		alert("Please add the following and click the Send button: " + "\n" + "\n" + errors);
		return false;
	} else {
		return true;
	}
}

function setCancel() {
	cancel = true;
}

function IsNumeric(strString) {
	//  check for valid numeric strings
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	return true					
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_thefacts_over = newImage("../images/nav_thefacts-over.gif");
		nav_epiphanies_over = newImage("../images/nav_epiphanies-over.gif");
		nav_solutions_over = newImage("../images/nav_solutions-over.gif");
		nav_capabilities_over = newImage("../images/nav_capabilities-over.gif");
		nav_thebuzz_over = newImage("../images/nav_thebuzz-over.gif");
		nav_contactus_over = newImage("../images/nav_contactus-over.gif");
		nav_sitemap_over = newImage("../images/nav_sitemap-over.gif");
		preloadFlag = true;
	}
}
