/******************************************************/
/*	Description :	Main Site Routines	               /
/*	Routines	:	Rollovers, Validation, Redirection /
/*              :   Div Toggle Ctrls 				   / 
/*	Version		:	4.0								   /
/*	Date Stamp  :	7/20/05 - RH					   /
/******************************************************/

//-----------------------------------------------------Stock Macromedia Rollover Code->>

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-----------------------------------------------------Contact Validation && Redirection->>
// Vs. 1.02

var SUCCESS 		= "thankYou.jsp";
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 == "") {
		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 == "") {
		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();
	}
}

//-----------------------------------------------------Redirection && thankYou Routines->>
// Vs. 3.0

//	Print Message (...) [USAGE] [RH]
//		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();
	//	FailSafe -> Redirects after timer + 5 seconds.
	setTimeout("location='index.jsp'",(timer+5000));
}

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.jsp';
		// vs.3.5
		// changes message while redirecting
		rd=document.getElementById("rdtxt");
		rd.value="Redirecting";
	}
}

MM_reloadPage(true);

//---------------------------------------------------------------------------Div Toggle Ctrls->>
// Vs. 2.08

//	divToggle (...) [USAGE] [RH]
//		Creates explorer style collapsable divs
//		@Params:
	//		[0] ->	id  		-> Div ID
	//		[1]	->	useImgs		-> using +/- images (optional, default: true)
//		#Notes:
	//		-	name the controlling img-link the same as the div with "Ctrl" at the end !important
	//		-	plus/minus images MUST be in the same folder
	//		-	plus/minus images can be any extension (gif|jpg|etc)

function divToggle()
{
	var a=divToggle.arguments;
	var id=a[0];
	var useImgs=(a[1]!=null)?a[1]:false;	
	elem=document.getElementById(id);
	elem.style.display=(elem.style.display=="none" || elem.style.display=="")?"block":"none";

	if(useImgs){
		// toggle (path)/(plus|minus).(gif|jpg)
		img=document.getElementById(id+"Ctrl");
		lS=img.src.lastIndexOf("/") + 1;	// last slash
		path=img.src.substr(0,lS);			// read path
		pm=img.src.substr(lS);				// read img name
		ext=pm.substr(pm.lastIndexOf("."));	// get img extension
		img.src=path + ((pm=="minus"+ext)?"plus"+ext:"minus"+ext);
	}
}


//---------------------------------------------------------------------------Print Friendly Page->>
// v1.0 [IE] [RH]

function print_directions(){
	if( document.getElementById('directions_frame')){
		theHead=document.getElementById('directions_head').innerHTML;	// directions.jsp
		theTxt=document.getElementById('directions_frame').innerHTML;	// drivingDirections.jsp
		theWin=window.open("","printWin");
		theWin.document.write(theHead + "<br><br>");
		theWin.document.write(theTxt);
	}
}

//-----------------------------------BOOKMARK

function favoris() {
            url=location.href;
            title=document.title;
            if ( navigator.appName != 'Microsoft Internet Explorer' ){
                        window.sidebar.addPanel(title,url,""); 
            } else { 
                        window.external.AddFavorite(url,title); 
            } 
}

// EOF;

