var detectMacXFF;
var userAgent = navigator.userAgent.toLowerCase();
var dontCloseAlert = false;
var fProgress = "off";
if (userAgent.indexOf('firefox')!=-1) {
   detectMacXFF =  true; 
} else {
 detectMacXFF = false;
}

function showTableRow(idName)
{
 if (detectMacXFF) {
  displayWay = "table-row";
 } else {
  displayWay = "block";
 }
 showMeFull(idName,displayWay);
}

function invisibleMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.visibility = "hidden";
	}
}

function visibleMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.visibility = "visible";
	}
}

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

function getChecked(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).checked;
	}
}

function hrefMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).href = h;
	}
}

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


function enableMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).disabled=false;
	}
}


function disableMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).disabled=true;
	}
}

function checkMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).checked = true;
	}
}

function uncheckMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).checked = false;
	}
}

// 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();
	}
}

function srcMe(idName,iSrc) {
	 if (checkID(idName)) {
	 	if (iSrc == "") {
			hideMe(idName);
		} else {
		 	document.getElementById(idName).src=iSrc;
		}
	}
}

// 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;
	}
}

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


// shows a html block
function showMeFull(idName,displayWay) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display=displayWay;
	}
}

// shows a html block
function showMe2(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}



// gives INNERHTML to an element
function htmlMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = h;
	}
}


// shows a html block
function getHTML(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).innerHTML;
	}
}

// shows a html block
function getValue(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).value;
	}
}


// shows a html block
function slipMe(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;
	}
}

// changes a stylesheet class to a specified ID
function changeStyle(idName,styleType,styleValue) {
	var pxTypes = "|paddingtop|paddingleft|paddingright|paddingbottom|margintop|marginleft|marginbottom|marginright|right|top|left|bottom|width|height|offsetWidth|offsetHeight|scrollWidth|scrollHeight|";
	var findStyle = "|"+styleType.toLowerCase()+"|";
	if (checkID(idName)) {
		if (pxTypes.indexOf(findStyle) >=0) {
			eval("document.getElementById(idName).style."+styleType+"='"+styleValue+"px'");
		} else {
			eval("document.getElementById(idName).style."+styleType+"='"+styleValue+"'");
		}
	}
}


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



function Popup(myUrl,popType) {
	var winName='flames';
	var winTop=50;
	var winLeft=100;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();
	switch (popType) {
		case 1:   // mailing list
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=520,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 2:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 3:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 4:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 5:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=300,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
	}
	pop.focus();
}



function isEmail(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 adjustHeight() {	
	var lHeight, cHeight;
	if (checkID("leftNav") && checkID("contentContainer") && checkID("leftFruit")) {
		lHeight = document.getElementById("leftNav").offsetHeight;
		if (checkID("contentContainer")) {
			cHeight = document.getElementById("contentContainer").offsetHeight;
		}
		if (cHeight < lHeight) {
			document.getElementById("contentContainer").style.height = lHeight+"px";
		} else {
			document.getElementById("leftNav").style.height = cHeight+"px";
		}
		if (cHeight > 644 && !detectMacXFF) {
			document.getElementById("leftFruit").style.bottom = "-1px";
		}
	}
}



function styleMe(idName,styleType) {
    addTag(idName, "<"+styleType+">", "</"+styleType+">", "");
}


function showSub(idName) {
	showMe(idName);
	hiddenFrame(idName);

}

function hideSub(idName) {
	hideMe(idName);
	hideMe("hiddenFrame_"+idName);
}

function getOffH(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetHeight;
	} else {
		return 0;
	}
}

function getOffW(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetWidth;
	} else {
		return 0;
	}
}

function setHeight(idName,h) {
	if (checkID(idName)) {
			document.getElementById(idName).style.height = (h)+"px";
	}
}		

function setWidth(idName,w) {
	if (checkID(idName)) {
			document.getElementById(idName).style.width = (w)+"px";
	}
}

function hiddenFrame(hub) {
	var curHeight = getOffH(hub);
	var curWidth = getOffW(hub);
	setHeight("hiddenFrame_"+hub,curHeight-2);
	setWidth("hiddenFrame_"+hub,curWidth);
	showMe("hiddenFrame_"+hub);
}
		
function leftHighlight() {
	if (checkID("leftHighlight")) {
		htmlMe("leftTarget",getHTML("leftHighlight"));
	}
}



function checkForm(formArray) {
	var checkCount = 0;
	var fieldCount = 0;
	var otherCount = 0;
	var otherField = 0;
	for (f = 0; f < formArray.length; f++) {
		changeStyle(formArray[f],"background","#FFFFFF");
		fieldCount++;
		if (getValue(formArray[f]) == "") { 
			checkCount++;
			break;
		}
	}
	for (f = 0; f < formArray.length; f++) {
		if (formArray[f].toLowerCase() == "email2") { 
			if (!validateEmail(getValue(formArray[f]))) {
				otherCount++;
				otherField = f;
				break;
			}
		} else if (formArray[f].toLowerCase() == "postalcode1") { 
			if (!checkPostal(getValue(formArray[f]),1)) {
				otherCount++;
				otherField = f;
				break;
			}
		} else if (formArray[f].toLowerCase() == "postalcode2") { 
			if (!checkPostal(getValue(formArray[f]),2)) {
				otherCount++;
				otherField = f;
				break;
			}
		}
	}
	if (checkCount > 0) {
		alert("You have not entered valid information for all the required fields, please check and try again.");
		focusMe(formArray[fieldCount-1]);
		changeStyle(formArray[fieldCount-1],"background","#ccffff");
		return false;
	} else if (otherCount > 0) {
		alert("You have not entered valid information for all the required fields, please check and try again.");
		focusMe(formArray[otherField]);
		changeStyle(formArray[otherField],"background","#ccffff");
		return false;
	} else{
		return true;
	}
	
}	

function validateEmail(addr) {
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   return false;
}
if (atPos == 0) {
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   return false;
}
if (addr.indexOf('.@',0) != -1){
   return false;
}
if (addr.indexOf('..',0) != -1) {
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   return false;
}
return true;
}

function isNumeric(str) {
	var good = "1234567890."
	var count = 0;
	for (s = 0; s < str.length; s++) {
		if (good.indexOf(str.charAt(s)) < 0) {
			count++;
			break;
		}
	}
	if (count > 0) {
		return false;
	} else {
		return true;
	}
}

// needs to be in A1A 1A1 format
function checkPostal(pc,boxNum) {
	var numbers = "1234567890";
	var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var isGood, char1, char2, char3;
	if (pc.length != 3) {
		isGood = false;
	} else {
		char1 = pc.charAt(0);
		char2 = pc.charAt(1);
		char3 = pc.charAt(2);
		if (boxNum == 1) { // A1A format
			if ((letters.indexOf(char1) >=0) && (numbers.indexOf(char2) >=0) && (letters.indexOf(char3) >=0)) {
				isGood = true;
			} else {
				isGood = false;
			}
		} else if (boxNum == 2) { // 1A1 format
			if ((numbers.indexOf(char1) >=0) && (letters.indexOf(char2) >=0) && (numbers.indexOf(char3) >=0)) {
				isGood = true;
			} else {
				isGood = false;
			}
		} else {
			isGood = false;
		}
	}
	if (isGood) {
		return true;
	} else {
		return false;
	}		
}

function focusName(idName) {
	if (idName != "") {
		focusMe(idName);
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function getHeight(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetHeight;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function getWidth(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetWidth;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function setHeight(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).style.height = h+"px";
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function setWidth(idName,w) {
	if (checkID(idName)) {
		document.getElementById(idName).style.width = w+"px";
	}
}


function updateQty(idName,theCount) {
	var curBase = "0";
	for (i = 0; i < idArray.length; i++) {
		curBase = baseQArray[i]+"";
		if (curBase.indexOf("|") >= 0) { // more list options
			var splitBase = curBase.split("|");
			if (idArray[i]==idName) {
				if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==(document.getElementById("opt_"+idName+"_0").options.length-1)) { // greater option
					if (idName == "SNPR-0002") {
						document.getElementById("quant_"+idName).value=3;					
					} else {
						document.getElementById("quant_"+idName).value=1;					
					}
					
				} else if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==1) { // greater option
					document.getElementById("quant_"+idName).value=parseInt(splitBase[0]);
				} else if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==2) { // greater option
					document.getElementById("quant_"+idName).value=parseInt(splitBase[1]);
				} else if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==3) { // greater option
					document.getElementById("quant_"+idName).value=parseInt(splitBase[2]);
				} else if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==4) { // greater option
					document.getElementById("quant_"+idName).value=parseInt(splitBase[3]);
				}
			}
		} else {
			if (idArray[i]==idName) {
				if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==(document.getElementById("opt_"+idName+"_0").options.length-1)) { // greater option
					document.getElementById("quant_"+idName).value=1;
				} else if (document.getElementById("opt_"+idName+"_0").options.selectedIndex==2) { // greater option
					document.getElementById("quant_"+idName).value=parseInt(curBase);
				}
			}
		}
	}
}

	function checkQty(thisValue,idName,isDetail,theCount) {
		var nums = "1234567890";
		var isNumber = 0;
		var quantity = 0;
		// so if quantity is at least the baseQArray, then choose [1] of split in valueArray
		// based on idArray value
		
		for (s = 0; s < thisValue.length; s++) {
			if (nums.indexOf(thisValue.charAt(s)) < 0) {
				isNumber++; // if anything is not a number add to this variable
			}
		}
		
		if (isNumber == 0) { // valid number
			quantity = parseInt(thisValue);
			for (i = 0; i < idArray.length; i++) {
				if (idArray[i]==idName) { // match
				
					var curBase = baseQArray[i]+"";
					if (curBase.indexOf("|") >= 0) { // has a split
						var splitBase = curBase.split("|");
						var optLength = document.getElementById("opt_"+idArray[i]+"_0").options.length-1;
						var didUpdate = false;
						for (o = 0; o < optLength-1; o++) {
							if (quantity >= parseInt(splitBase[o])) { // activate full price or first item in dropdown 
								if (document.getElementById("opt_"+idArray[i]+"_0")) {
									document.getElementById("opt_"+idArray[i]+"_0").options.selectedIndex=o+1;
								}
								didUpdate = true;
								break;
							}
						}
						if (!didUpdate) {
							document.getElementById("opt_"+idArray[i]+"_0").options.selectedIndex=optLength;
						}
						if (isDetail) {
							eval("updateprice"+theCount+"('"+idArray[i]+"')");
						} else {
							eval("updateprice"+priceArray[i]+"()");
						}
					} else {
						if (quantity >= baseQArray[i]) { // activate full price or first item in dropdown 
							if (document.getElementById("opt_"+idArray[i]+"_0")) {
								document.getElementById("opt_"+idArray[i]+"_0").options.selectedIndex=1;
							}
						} else {
							if (document.getElementById("opt_"+idArray[i]+"_0")) {
								document.getElementById("opt_"+idArray[i]+"_0").options.selectedIndex=2;
							}
						}
						if (isDetail) {
							eval("updateprice0('"+idArray[i]+"')");
						} else {
							eval("updateprice"+priceArray[i]+"()");
						}
					}
				}
			}
		}
	}
