var bgColor;
var isHome = false;
function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

// global function for image directory
function imageDir(i) {
	return "<img src=\"../graphics/"+i+".jpg\">";
}

// global function for graphics directory
function iconsDir(i) {
	return "<img src=\"../graphics/"+lang+"/icons/"+i+".gif\">";
}

function checkMatch(curValue,thisValue,showThis,showThat) {
	var output = "";
	if (curValue==thisValue) {
		output = showThis;
	} else {	
		output = showThat;
	}
	return output;	
}

function matchArray(val,array1,array2) {
	var output = val;
	for (a = 0; a < array1.length; a++) {
		if (array1[a] == val) {
			output = array2[a];
		}
	}
	return output;
}

function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

function hideMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="none";
	}
}

function clearMe(t,val) {
	if (t.value == val) {
		t.value = "";
	}
}


function showMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="block";
	}
}

function changeClass(idName,newClass) {
	if (checkID(idName)) {
		document.getElementById(idName).className=newClass;
	}
}

// submit an element
function submitMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).submit();
	}
}

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 headOn(thisName) {
	if (((sectionName == "") || (sectionName == "null")) && ((subName == "") || (subName == "null"))) {
		if (hubName == thisName) {
			return "on";
		} else {
			return "";
		}
	} else {
		return "";
	}
}


function fixAmp(str) {
	return str.replace("_","&");
}
	
function applyLang(h) {
	if (h.indexOf("?") >=0) {
		return h+"&lang="+lang;
	} else {
		return h+"?lang="+lang;
	}
}

function goHere(u) {
	window.location.href=applyLang(u);
}

function leftNav(hubName,sectionName,subName) {
	var lHTML = "";
	lHTML += "<ul class=\"leftLinks\">";
	for (b = 0; b < mainArray.length; b++) {
		lHTML += "<li class=\""+headOn(mainArray[b])+"\"><p><a href=\""+applyLang(mainHrefArray[b])+"\">"+mainNameArray[b]+"</a></p></li>";
		if (mainArray[b] == hubName) {
			lHTML += subleftNav(hubName,sectionName,subName);
		}
	}
	lHTML += "</ul>";
	document.write(lHTML);
}

function subleftNav(h,secName,sName) {
	var html = "";
	var useArray = matchArray(h,mainArray,subArray); // this tells which sub array to use (ie section1Array);
	var useNameArray = matchArray(h,mainArray,subNameArray); // this tells which subName array to use (ie section1NameArray);
	var useHrefArray = matchArray(h,mainArray,subHrefArray); // this tells which subHref array to use (ie section1HrefArray);

	var useSubSectionArray = matchArray(h,mainArray,subSectionArray); // this says which sub section array we're using (subSection1Array);
	var useSubSectionNameArray = matchArray(h,mainArray,subSectionNameArray); // this says which sub section array we're using (subSection1Array);
	var useSubSectionHrefArray = matchArray(h,mainArray,subSectionHrefArray); // this says which sub section array we're using (subSection1Array);

	var subUseArray, subUseNameArray, subUseHrefArray;
		if (sectionName != "null") {
			html += "<ul>";
			for (x = 0; x < useArray.length; x++) {
				html += "<li class=\""+checkMatch(sectionName,useArray[x],'on','')+"\"><p><a href=\""+applyLang(useHrefArray[x])+"\">"+useNameArray[x]+"</a></p></li>";
				if (subName != "null") {
					subUseArray = matchArray(useArray[x],useArray,useSubSectionArray) // this tells which sub array to use (ie section1Sub1Array);
					subUseNameArray = matchArray(useArray[x],useArray,useSubSectionNameArray) // this tells which subName array to use (ie section1Sub1NameArray);
					subUseHrefArray = matchArray(useArray[x],useArray,useSubSectionHrefArray) // this tells which subHref array to use (ie section1Sub1HrefArray);
					//alert(useNameArray[x]);
					if ((subUseHrefArray[0] != "") && (useArray[x] == secName)) {
						html += "<ul>";
						for (y = 0; y < subUseArray.length; y++) {
							html += "<li class=\""+checkMatch(subName,subUseArray[y],'on','')+"\"><p><a href=\""+applyLang(subUseHrefArray[y])+"\">"+fixAmp(subUseNameArray[y])+"</a></p></li>";
						}
						html += "</ul>";
					}
				}
			}
			html += "</ul>";
		}
		return html;
}

var swatchArray = new Array();
var swatchValueArray = new Array();

function swatchBoxes() {
	var html = "";
	for (s = 0; s < swatchArray.length; s++) {
		if (s % 5 == 0) {
			if (s != 0) {
				html += "</div>";
			}
			html += "<div class=\"swatchColumn\">\n";
		}
		html += "<div class=\"swatchContainer\">\n";
			html += "<div class=\"swatchBlock\">\n";
				html += "<div class=\"swatchColor\" style=\"background-color:#"+swatchArray[s]+";\"></div>\n";
				html += "<div class=\"swatchString\">"+swatchValueArray[s]+"</div>\n";
			html += "</div>\n";
		html += "</div>\n";
	}
	html += "</div>\n";
	document.write(html);
}
								
function doLang(lang) {
// all we have to do is get current url and swap the lang directive...
	var loc = window.location+"";
	var splitLoc, redirect;
	if (loc.indexOf("?") >=0) { // already has a querystring
		if (loc.indexOf("lang") >=0) { // already has lang paramater
			splitLoc = loc.split("lang=");
			redirect = splitLoc[0]+"lang="+lang;
		} else {
			redirect = slocation.href=loc+"&lang="+lang;
		}
	} else {
		if (loc.indexOf(".jsp") >=0) {
			redirect = loc+"?lang="+lang;
		} else {
			redirect = loc+"index.jsp?lang="+lang;
		}
	}
	window.location.href=redirect;
}

function showAd(adID) {
	var html = "";
	html += "<div class=\"ad\">"+adID+"</div>"
}

function doPageTitle(bg) {
	if (checkID("pageTitle")) {
		document.getElementById("pageTitle").style.background=bg;
	}
}

function runscript(bg) {
	doPageTitle(bg);
}



function isBlank(v) {
	if (v == "") {
		return true;
	} else {
		return false;
	}
}


function getValue(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).value;
	}
}

function htmlMe(idName,html) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = html;
	}
}

function validateEmail(email) {
	invalidChars = " ',~!#$%^&*/\()[]{}<>?|;:+=\"";
	if (email == "") {
		return false;
	}
	// checks for bad characters
	for (i = 0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) != -1) {
			return false;
		}
	}
	//finds position of @ symbol
	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos + 1) != -1) {
		return false;
	}
	//finds position of . in email
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	atSplit = email.split("@");
	beforeAt = atSplit[0];
	afterAt = atSplit[1];
	if (atSplit[0].length == 1) { 
		return false;
	}
	periodSplit = atSplit[1].split(".");
	beforePeriod = periodSplit[0];
	if (beforePeriod.length <=1) {
		return false;
	}
	return true;
}

function replaceAmp(s) {
	return s.replace("_","&");
}


function replaceTT() {
	if (checkID("titleTextString")) {
		document.getElementById("titleTextString").innerHTML = replaceAmp(document.getElementById("titleTextString").innerHTML);
	}
}
