/******************************************************/
/*	Description :	Contact Form Validation Routines   /
/*	Form-Number :	4								   /
/*	Version		:	3.5								   /
/*	Date Stamp  :	6/20/05 - RH					   /
/******************************************************/

var FAILURE 		= "error.htm";
var ValidateName	= true;


//	Validates the required fields
function isFieldsValid() {
	var msg = "";
	
	//	Validate the fields
	if(ValidateName){
		msg += validateName(document.emailForm.Name.value);
	}
	
	if(msg != "") {
		msg += "\n";
	}
	msg += validateEmail(document.emailForm.Email.value);
	
	//	Display a message if validation failed
	if(msg != "") {
		alert("Please correct the following errors so that we can process your message:" +
			  "\n\n" + msg);
		return false;
	}
	else {
		return true;
	}
}

//	Validates the name field
function validateName(name) {
	var msg = "";
	var errorMsg = "  - Enter a valid name";

	name = trimString(name);	
	if(name == "" || name=="name") {
		msg = errorMsg;
	}
	
	return msg;
}

//	Validate the email field
function validateEmail(email) {
	var msg = "";
	var errorMsg = "  - Enter a valid email address";
	
	email = trimString(email);	
	if(email == "" || email=="email") {
		msg = errorMsg;
	}
	else {
		var at = "@"
		var dot = "."
		var lat = email.indexOf(at)
		var lstr = email.length - 1;
		var ldot = email.indexOf(dot)

		//	Validate presence of at least one @
	  	if(email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == lstr) {
			msg = errorMsg;
		}	
		//	Validate presence of only one @
		else if(email.indexOf(at, (lat + 1)) != -1) {
			msg = errorMsg;
		}
		//	Validate presence of at last one .
		else if(email.indexOf(dot) == -1 || email.indexOf(dot) == 0 || email.indexOf(dot) == lstr) {
			msg = errorMsg;
		}		
		//	Validate that .@ or @. is not present
		else if(email.substring(lat - 1, lat) == dot || email.substring(lat + 1, lat + 2) == dot) {
			msg = errorMsg;
		}
		//	Validate @<char>. pattern is found
		else if(email.indexOf(dot, (lat + 2)) == -1) {
			msg = errorMsg;
		}
	}

	return msg;
}
  
//	Resets the form
function resetForm() {
	document.all.emailForm.reset();
}

var timerID;

//	Submits the message to the server
function submitMessage() {
	if(isFieldsValid()) {
	
		if(isBrowserSupported()) {
			document.emailForm.target = "responseFrame";
			document.emailForm.submit();
			timerID = setInterval("pollForResponse()", 500);
		}
		else {
			openCenteredWindow("/pd3/mail.htm", "emailPopup", 350, 250);
			document.emailForm.target = "emailPopup";
			setTimeout("document.emailForm.submit()", 500);
		}
	}
}

//	Returns true if a supported browser (IE 4.x or higher; Netscape 6.x or higher) 
//	is being used
function isBrowserSupported() {
	var isSupported = false

	if(navigator.appName == 'Microsoft Internet Explorer') {
		if(getIEVersionNum() >= 5) {
		  	isSupported = true;
		}
	}
	else if(navigator.appName == 'Netscape') {
		if(getNetscapeVersionNum() >= 5) {
			isSupported = true;
		}
	}
	
	return isSupported;
}

//	Gets the version number for Internet Explorer
function getIEVersionNum() {
	var brVer = navigator.userAgent;
	var brVerId = brVer.indexOf('MSIE');
	var brNum = brVer.substr(brVerId, 8);

	return brNum.substring(5, 6);
}

//	Gets the version number for Netscape
function getNetscapeVersionNum() {
	var brVer = navigator.userAgent;
	var reg = new RegExp('/');
	var brVerId = brVer.search(reg);
	var brNum = brVer.substring(brVerId + 1);

	return brNum.substring(0, 1);
}

//	Polling function that waits for a response from the server
function pollForResponse() {
	var response = responseFrame.document.body.innerHTML;
	
	
	if(response != null && response != "") {
		stopTimer();
		
		if(response.indexOf("Please Wait") != -1) {
			parent.document.location.href = SUCCESS;
		}

		else {
			parent.document.location.href = FAILURE;	
		}
	}
	
}

//	Clears the polling timer
function stopTimer()
{
	clearInterval(timerID);
}

//	Opens a window that is centered on the screen
function openCenteredWindow(url, name, width, height) {

	// Compute the window's top and left positions so that it is centered
	var windowWidth 	= width;
	var windowHeight 	= height;
	var screenWidth 	= screen.availWidth;
	var screenHeight 	= screen.availHeight;
	var left 			= screenWidth  / 2 - windowWidth  / 2;
	var top 			= screenHeight / 2 - windowHeight / 2;
	var properties		= "resizable=yes,left=" + left + ",top=" + top + ",width=" + 
		windowWidth + ",height=" + windowHeight;

	window.open(url, name, properties);
}

//	Trims a string
function trimString(str) {
	//	Remove leading whitespaces
	while (str.charAt(0) == ' '  || str.charAt(0) == '\t' ||
		   str.charAt(0) == '\r' || str.charAt(0) == '\n') {
		str = str.substring(1,str.length);
	}
	
	//	Remove trailing whitespaces
	while (str.charAt(str.length - 1) == ' '  || str.charAt(str.length - 1) == '\t' ||
		   str.charAt(str.length - 1) == '\r' || str.charAt(str.length - 1) == '\n') {
    	str = str.substring(0, str.length - 1);
	}
	
	return str;
}

//	Netscape 4.x bug fix - reloads the page when the browser is resized
function MM_reloadPage(init) {  
	if (init==true) with (navigator) {
		if((appName=="Netscape") && (parseInt(appVersion)==4)) {
			document.MM_pgW=innerWidth; 
			document.MM_pgH=innerHeight; 
			onresize=MM_reloadPage; 
		}
	}
	else if(innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) {
		location.reload();
	}
}

/*//-------------------------------------------------->>
		Routines for thankYou/
*///-------------------------------------------------->>

//	Print Message (...)
//		Outputs code to create the countdown box
//		Params:
	//		[0] ->	Time (milliseconds::1000~1sec)
	//		[1]	->	Foreground Color
var delay=100;
function printMessage(){
	var a=printMessage.arguments;
	var timer=(a[0]!=null)?a[0]:5000; 
	var txtcolor=(a[2]!=null)?a[2]:"black"; 
	
	// Random
		var symbol=".";
		var size=22;
		var bStr="";
		var num_sym=50;
		/* vs.2.0
		// increases .s for more time
		for(var i=0;i<timer;i+=100)
			bStr+=symbol;
		*/
		// vs.3.0
		// increases delay for more time
		for(var i=0;i<num_sym;i++)
			bStr+=symbol;
		delay=Math.ceil(timer/num_sym);
			
	document.write("<p align=\"center\"><strong>Thank you for your submission. <br>We will get back to you as soon as posssible </strong><br><br>");
	document.write("<input name=\"rdtxt\" type=\"text\" value=\"Redirecting, Please Wait...\" size=\"23\" style=\"border:none; background-color:transparent; color:"+txtcolor+"; text-align:center;\"><br>");
	document.write("<input type=\"text\" id=\"timeBox\" style=\"border:none; background-color:transparent; color:"+txtcolor+";\" value=\""+bStr+"\" size=\""+size+"\" readonly=\"1\"></p>");
	remDot(delay);
	//	FailSafe -> Redirects after timer + 5 seconds.
	setTimeout("location='../index/'",(timer+5000));
}

//	Rem Dot()
function remDot(){
	b=document.getElementById("timeBox");
	if(b.value.length>0){
		b.value=b.value.substr(0,b.value.length-1);
		setTimeout("remDot()",parseInt(delay));
	}else{
		location='../index/';
		// vs.3.5
		// changes message while redirecting
		rd=document.getElementById("rdtxt");
		rd.value="Redirecting";
	}
}

MM_reloadPage(true);