var affinity = getAffinity(affinityArray);
var offerEN = "The prices you are about to view do not reflect the additional savings available by combining these special employee offers with others in your region. Visit your nearest Dodge, Chrysler and Jeep retailer for more details.";
var offerFR = "Les prix que vous &ecirc;tes sur le point de voir ne comprennent pas les &eacute;conomies additionnelles que vous pouvez r&eacute;aliser en combinant ces offres &agrave; l'intention des employ&acute;s avec les autres en vigueur dans votre r&eacute;gion. Pour plus de d&eacute;tails, visitez le concessionnaire Dodge, Chrysler et Jeep de votre r&eacute;gion.";
var pricingURL = "pricing_"+affinity+"_"+lang+".html";
var aboutURL = "about_"+affinity+"_"+lang+".html";
var viewURL = "view_"+affinity+"_"+lang+".html";
var bonusURL = "bonus_"+affinity+"_"+lang+".html";
var ebonusURL = "ebonus_"+affinity+"_"+lang+".html";
var choiceURL = "choice_"+affinity+"_"+lang+".html";
var friendsURL = "friends_"+affinity+"_"+lang+".html";

var SelValueOld = 0;
  function hideAll(numberOfLayers) {
  for (var x = 1; x <= numberOfLayers; x++) {
    var layerName = document.getElementById(x);	
    layerName.style.display='none';
   }
  }

function doDiv(dropdown) {
	if (SelValueOld > 0) {
		if (document.getElementById(SelValueOld)) {
			document.getElementById(SelValueOld).style.display='none';
		}
	}
	var myindex  = dropdown.selectedIndex
	var SelValue = dropdown.options[myindex].value
	if (document.getElementById(SelValue)) {
		document.getElementById(SelValue).style.display='block';
	}
	if (document.getElementById('default')) {
		document.getElementById('default').style.display='none';
	}
	SelValueOld = SelValue;
	htmlMe("affName",addSpace(affinity.toUpperCase()));
	return true;
}

// checks existence of ID in markup
function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

// focuses on element
function focusMe(idName) {
	if (checkID(idName)){
		document.getElementById(idName).focus();
	}
}

// submit an element
function submitMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).submit();
	}
}

// submit an element
function resetMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).reset();
	}
}

// clears an element
function clearMe(t,val) {
	if (t.value==val) {
		t.value = '';
	}
}

function valueMe(idName,val) {
	if (checkID(idName)){
		document.getElementById(idName).value=val;
	}
}

function hrefMe(idName,h) {
	if (checkID(idName)){
		document.getElementById(idName).href=h;
	}
}

function htmlMe(idName,h) {
	if (checkID(idName)){
		document.getElementById(idName).innerHTML=h;
	}
}

// shows a html block
function showMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="block";
	}
}

// shows a html block
function inlineMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}


// hides a html block
function hideMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="none";
	}
}

// changes a stylesheet class to a specified ID
function changeClass(idName,toClass) {
	if (checkID(idName)) {
		document.getElementById(idName).className=toClass;
	}
}


function bodyBG(startX,gradHeight,bg) {
	var x = startX;
	var html = "";
	while (x >= 0) {
		html += "<div style=\"opacity: "+x/100+"; filter: alpha(opacity="+x+"); background-color:"+bg+"; font-size:"+gradHeight+"px; height:"+gradHeight+"px; line-height:"+gradHeight+"px; width:100%;\">&nbsp;</div>";
		x--;
	}
	return html;
}

function home() {
	var bodyHTML = bodyBG(homeStart,homeHeight,homeBG);
	htmlMe("bodyBG",bodyHTML);
}

function tab() {
	var bodyHTML = bodyBG(tabStart,tabHeight,tabBG);
	htmlMe("topnavBG",bodyHTML);
}


function tabPop() {
	var bodyHTML = bodyBG(tabStart,tabHeight,tabBG);
	htmlMe("topnavpopBG",bodyHTML);
}

function getAffinity(affArray) {
	var loc = window.location+"";
	if (loc.indexOf("affiliate") >=0) { 
		return affArray[0]; // Affiliate
	} 
}	 

function addSpace(str) {
	if (str != "") {
		return str.replace("_"," ");
	}
}

function applyLang(aName) {
	if (lang == "fr") {
		if (aName == "RETIREE") {
			return "Retrait&eacute;";
		} else if (aName == "EMPLOYEE") {
			return "Employ&eacute;";
	} else if (aName == "AFFILICATE") {
			return "Affiliate;";			
		}
	} else {
		return aName;
	}
}

function doMainImage(aff) {
	showMe("mainImg_"+aff);
	showMe("topnav_"+aff);
	if (aff=="employee") {
		inlineMe("employee_logo");
	}
	showMe("logintext_"+aff);
	showMe("logo_"+aff);
}

function doText(aff) {
	showMe("text_"+aff);
	htmlMe("affName",applyLang(addSpace(aff.toUpperCase())));
	showMe("copy_"+aff);
}


function doLink(aff) {
	
	if (lang == "fr") {
		aff = aff + "_fr";
	}
	hrefMe("link_certificate1",aff+".pdf");
	hrefMe("link_certificate2",aff+".pdf");
	hrefMe("link_certificate3",aff+"_flyer.pdf");
	hrefMe("link_certificate4",aff+".pdf");
	if (checkID("link_certificate1")) {
		document.getElementById("link_certificate1").onclick = function () {
			urchinTracker(aff+".pdf");
		}
	}
	if (checkID("link_certificate2")) {
		document.getElementById("link_certificate2").onclick = function () {
			urchinTracker(aff+".pdf");
		}
	}
	if (checkID("link_certificate3")) {
		document.getElementById("link_certificate3").onclick = function () {
			urchinTracker(aff+"_flyer.pdf");
		}
	}
	if (checkID("link_certificate4")) {
		document.getElementById("link_certificate4").onclick = function () {
			urchinTracker(aff+".pdf");
		}
	}
}

function doAffinity() {
	doMainImage(affinity);
	doText(affinity);
	doLink(affinity);
}


function warning(offerLang) {
	var offerText, offerURL;
	if (offerLang == "fr") {
		offerText = offerFR;
		offerURL = "http://www.votrechrysler.ca/";
	} else {
		offerText = offerEN;
		offerURL = "http://www.getachryslerjeepdodge.ca/";
	}	
  var response = window.confirm(offerText);
  if (response) {
		Popup(offerURL,0);
  }
}


function Popup(myUrl,popType) {
	var winName='chrysler';
	var winTop=50;
	var winLeft=100;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();

	switch (popType) {
		case 0:   // 
	  	pop = window.open(myUrl,winName+winTime,'width=780,height=520,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=yes,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 1:   // 
	  	pop = window.open(myUrl,winName+winTime,'width=1024,height=650,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=yes,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
	}

	pop.focus();

}

function doFrench(page) {
	window.location.href = "../fr/"+page;
}

function doEnglish(page) {
	window.location.href = "../en/"+page;
}
