// INIT VARIABLES




// INIT FUNCTIONS (onload statements)
$(document).ready(function(){
   navList();
 });

// IE bug fix to activate the sub menu items
navList = function() {
	if (document.all&&document.getElementById&&document.getElementById("nav")) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

// EMBEDDED FUNCTIONS (executed within code)

// Replaces ID with confirmation copy
confirmMe = function(outputLayer,outputCopy) {
	document.getElementById(outputLayer).innerHTML = outputCopy;
}

// Shows and hides various tabs and their content in POD sub pages
tabchange = function(object) {
	tabRoot = document.getElementById("tabs");
	aTags=tabRoot.getElementsByTagName("a");
	// clear arrow states for tabs
	for (i=0;i<aTags.length;i++) {
		$(aTags[i]).removeClass("down");
		contentTabName = "tab" + (i+1) + "content";
		document.getElementById(contentTabName).style.display="none";
	}
	$(object).addClass("down");
	contentTabToShow = object.id + "content";
	document.getElementById(contentTabToShow).style.display="block";
}

// Shows and hides various Abstract content in HJ Story
abstractControl = function(object,abstractNum) {
	abstractName = "#abstract_" + abstractNum;
	if (object.className == "icn_read") {
		object.className = "icn_unread";
		object.innerHTML = "Back to Summary";
		$(abstractName).show();
	//$("div."+abstractName).hide(); // if it is a classname
	}
	else if (object.className == "icn_unread") {
		object.className = "icn_read";
		object.innerHTML = "Read Abstract";
		$(abstractName).hide();
	}

}






// clear field value function: removes the default value onfocus, and adds back if nothing entered 
function fieldClear(obj) {
	if(obj.Val) {
		if (obj.value == '') { 
			obj.value = obj.Val;
			obj.Val = null;
			obj.first = null;
		} 
		else {
			obj.Val = null;
		}
	} else if (!obj.first) { 
		obj.Val = obj.value;
		obj.value = ''; 
		obj.first = 'true';
	} 
}
// DHTML Popups: Opens DHTML Layer
function popOpen(id,topOffset) {
	if(!topOffset) topOffset = 100;
	setSize(topOffset);
	var overShade = document.getElementById(id);
	var topPad =  pos+topOffset;
	// height-padding = div height
	overShade.style.height = (ph - topPad) + 'px'; /* remove topPad from height */
	overShade.style.width = 960 + 'px' // Set static for dealer site
	overShade.style.paddingTop = topPad + 'px';
	overShade.style.display = 'block';
	if (document.all && !navigator.appVersion.match(/MSIE 7.0/)) {
		coverSelects(overShade);
	}
}
// DHTML Popups: Closes DHTML Layer
function popClose(id) {
	document.getElementById(id).style.display = "";
	if (document.all && !navigator.appVersion.match(/MSIE 7.0/)) {
		document.getElementById("popup-cover").outerHTML = "";
	}
}
// DHTML Popups: Acquires sizes for DHTML Layer
function setSize(topOffset) { /* sets size of shade */
	if (window.pageYOffset != null) { /* moz and safari */
		pos = window.pageYOffset;
		ph = document.documentElement.scrollHeight;
		pw = document.documentElement.scrollWidth;
		if (document.body.scrollHeight > document.documentElement.scrollHeight) {
			ph = document.body.scrollHeight;
			pw = document.body.scrollWidth;
		}
	} else if (document.documentElement.scrollTop > document.body.scrollTop) { /* ie, catch if Standards compliance mode */
		pos = document.documentElement.scrollTop;
		ph = document.documentElement.scrollHeight;
		pw = document.documentElement.scrollWidth;
		if (document.documentElement.clientHeight > document.documentElement.scrollHeight) {
			ph = document.documentElement.clientHeight;
		}
	} else if (document.body != null) { /* if IE 5.5 */
		pos = document.body.scrollTop;
		ph = document.body.scrollHeight;
		pw = document.body.scrollWidth;
		if (document.documentElement.scrollHeight > document.body.scrollHeight) {
			ph = document.documentElement.scrollHeight;
		}
		ph = ph + pos + topOffset; /* fix box model */
	}
}
// DHTML Popups: covers select form elements with iframe in IE 6&< so they do not show through the popup
function coverSelects(par) {
		var covHeight = document.body.scrollHeight+"px"; // add the negative margin to the height
		var coverFrame = "<IFRAME id='popup-cover' style='";
		coverFrame = coverFrame + "height:"+covHeight+";' ";
		coverFrame = coverFrame + "src='javascript:false;' frameBorder='0' scrolling='no'></IFRAME>";
		par.insertAdjacentHTML("afterEnd",coverFrame); // create new iframe that is the size of the popup window
}
function getDealerMailToString(emailAddress,emailName){document.write('<a href="mailto:'+emailAddress+'"><p><strong>'+emailName+'</strong></p></a>');}


function getDealerMailToString2(emailAddress,emailName){
	var onClickAction = "popOpen('international_fleet_sales',66);fillRepName('"+emailName+"','"+emailAddress+"')";
	document.write('<a href="javascript:void(0)" onclick="'+onClickAction+'"><p><strong>'+emailName+'</strong></p></a>');
}
function getDealerMailToString3(emailAddress,emailName,domain,region){
	var onClickAction = "requestIframe('"+emailName+"','"+emailAddress+"','" +domain+"','" + region + "');fillRepName('" + emailName + "');popOpen('international_fleet_sales',66);";
	document.write('<a href="javascript:void(0)" onclick="'+onClickAction+'" class="arrow_link" style="font-size:1.1em">Contact Representative</a>');
}
function getDealerMailToString4(emailAddress,emailName,domain,region){
	var onClickAction = "requestIframe('"+emailName+"','"+emailAddress+"','" +domain+"','" + region + "');fillRepName('" + emailName + "');popOpen('international_fleet_sales',66);";
	document.write('<a href="javascript:void(0)" onclick="'+onClickAction+'"><p><strong>'+emailName+'</strong></p></a>');
}
function fillRepName(n) {
	document.getElementById('repName').innerHTML = n.toUpperCase();
}

var allowSend = true;
//AR: Begin Change 11/17
function sendMessageToDealer(dealerEmailElementId, senderNameElementId, senderEmailElementId, senderPhoneElementId, messageElementId){
	if(allowSend) {
	if(location.href.indexOf("localhost")!= -1 && location.href.indexOf("staging.")!=-1 && location.href.indexOf("pat.")!=-1) {
		var dealerEmail = document.getElementById(dealerEmailElementId).value;
	} else {
		var dealerEmail = "gnettles@sapient.com";
	}
	var senderName = document.getElementById(senderNameElementId).value;
	var senderEmail = document.getElementById(senderEmailElementId).value;
	var senderPhone = document.getElementById(senderPhoneElementId).value;
	var message = document.getElementById(messageElementId).value;
	var errordivid = "emailErrorDiv";
	allowSend = false;
	if(senderName == "" || message == "" || senderEmail == "") {
		errorMsg = "Please fill out all required fields."
		confirmMe(errordivid, errorMsg);
		return;
	}
	if(xValidateEmail(senderEmail)){
		//confirmMe(divid,'<img src="../images/loading.gif" height="16" width="16" align="top"> Sending Subscription Information...');
		$.get('RequestProcessor.aspx',
		{ username: 'hondajet', password: 'hondajet', dealerEmail: dealerEmail, senderName: senderName, senderEmail: senderEmail, senderPhone: senderPhone, messageBody: message, RequestType:"MessageDealer"},
		   function(xml){
				c = String(xml);
				var mpos 		= c.indexOf('message', 0) + 8;
				var qpos 		= c.indexOf('/message', mpos) - 1;
				var message 	= c.substring(mpos, qpos);
				if (c.indexOf("pass")!=-1){
				    document.getElementById("confirmContent").style.display="block";
				    document.getElementById("formContent").style.display="none";
				}
		   }
		 );
	} else {
	    allowSend = true;
	    if(senderEmail == ""){
	        errorMsg = "Required Field";
	    } else {
	        errorMsg = "We're sorry - our system does not recognize the format of your email address. Please re-enter.";
	    }
		confirmMe(errordivid, errorMsg);
	}
	}	
}
function fieldClear(senderNameElementId, senderEmailElementId, senderPhoneElementId, messageElementId) {
	for(var i=0;i<arguments.length;i++) {
		document.getElementById(arguments[i]).value = "";
	}
	document.getElementById("emailErrorDiv").innerHTML = "";
	document.getElementById("confirmContent").style.display="none";
	document.getElementById("formContent").style.display="block";
	allowSend = true;
}

//-------------------------------------------------------------------------------------------------------
/*** Validates an email address */
 //-------------------------------------------------------------------------------------------------------
function xValidateEmail(xES){
        var xVE = true;
        var xA  = xES.indexOf("@");
        var xB  = xES.substring(0,xA);
        var xD  = xES.length-1;
        var xE  = xES.lastIndexOf(".");
        var xC  = xES.substring(xA+1,xE);
        var xF  = (xD)-(xE+1);
        
        if(xA==-1){xVE=false;}
        if(xA != xES.lastIndexOf("@")){xVE=false;}
        if(xCheckSUVC(xB)!=true){xVE=false;}
        if(xB.length<1){xVE=false;}
        if(xCheckSUVC(xC)!=true){xVE=false;}
        if(xC.length<1){xVE=false;}
        if(xCheckSUVC(xES.substr(xE+1,xD))!=true){xVE=false;}
        if(xF<1 || xF>3){xVE=false;}
        return xVE;
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
function xCheckSUVC(xESS){
        var xV  = true;
        var xUC = new Array("!","£","$","%","^","&","*","+","=","?",":",";","'","	"," ",'"',"~","#","/");
        for(var i=0; i<=xUC.length-1;i++){
                if(xESS.indexOf(xUC[i],0)!=-1){
                        xV=false;
                        break;
                }
        }
        return xV;
}


//AR: End Change 11/17

function requestIframe(dealerName, dealerEmail, domain, region) {
    if(location.href.indexOf("localhost")!= -1){
        dealerEmail = "gnettles@sapient.com";
    }
	var mySrc = domain + "/contactus/contactDealer.aspx?ref="+dealerEmail+"&dname="+dealerName+"&dreg="+region;
	document.getElementById("contactDealer").src = mySrc
}