function isIE() {
	if (navigator.appName.indexOf("Microsoft") == 0) {
		return true;
	}

	return false;
}

function isIE7() {
	if (navigator.userAgent.indexOf('MSIE 7.0') != -1) {
		return true;
	}
	return false;
}

function isSafari() {
	if (navigator.userAgent.indexOf('safari') != -1) {
		return true;
	}
	return false;
}

// checks existence of ID in markup
function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

// get element
function getMe(idName) {
	if (checkID(idName)){
		return document.getElementById(idName);
	}
}

// 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();
	}
}

// sets the href of a value
function srcMe(idName,s) {
	if (checkID(idName)) {
		document.getElementById(idName).src=s;
	}
}

// sets the href of a value
function hrefMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).href=h;
	}
}

// clears an element
function clearMe(t,val) {
	if (t.value==val) {
		t.value = '';
	}
}

// gives INNERHTML to an element
function htmlMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = h;
	}
}


//gets the value
function getHTML(idName) {
 if (checkID(idName)) {
 		return document.getElementById(idName).innerHTML;
	}
}

//gets the value
function getSrc(idName) {
 if (checkID(idName)) {
 		return document.getElementById(idName).src;
	}
}

//gets the value
function getValue(idName) {
 if (checkID(idName)) {
 		return document.getElementById(idName).value;
	}
}

// gives value to an ID
function valueMe(idName,val) {
	if (checkID(idName)){
		document.getElementById(idName).value=val;
	}
}

// gives value to an ID attribute
function valueHTMLAttrMe(idName,attr,val) {
	if (checkID(idName)){
		if (isIE()) {
			var obj = document.getElementById(idName);
			var obj2 = obj.cloneNode(false);
			obj2.setAttribute(attr, val);
			obj.parentNode.replaceChild(obj2,obj);
			obj2.focus();
			obj2.select();
		}
		else {
			document.getElementById(idName).setAttribute(attr, val);
		}
	}
}

// gives name to an ID (only work on html element that have name attribute)
function nameMe(idName,name) {
	if (checkID(idName)){
		document.getElementById(idName).name=name;
	}
}

// gets the class
function getClassName(idName) {
 if (checkID(idName)) {
 		return document.getElementById(idName).className;
	}
}

// shows a html block
function showMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="block";
	}
}

// shows a html block
function showMe2(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";
	}
}

// Removes an option from HTML select list
function removeSelectOption(selectbox, optionId, optionValue) {
	if (isIE7()) {
	}
	else {
		var i;
		for (i=0; i<selectbox.options.length;++i) {
			//alert(selectbox.options[i].value);
			if (selectbox.options[i].value == optionValue) {
				selectbox.remove(i);
			}
		}
	}
}

/* changes a stylesheet class
 * if changeClass(idName,toClass) changes a stylesheet class to a specified ID
 * if changeClass(parentIdName,childIdName,toClass) changes a child stylesheet class within a collection and reset the others
*/
function changeClass() {
 	var args = changeClass.arguments;

	switch(args.length) {
		case 2: //changes a stylesheet class to a specified ID
			var idName = args[0], toClass = args[1];
			if (checkID(idName)) {
				document.getElementById(idName).className=toClass;
			}
			break;

		case 3: //changes a child stylesheet class within a collection and reset the others
			var parentIdName = args[0], childIdName = args[1], toClass = args[2];
			if (checkID(parentIdName) && checkID(childIdName)) {
				var parentObj = document.getElementById(parentIdName);
				var childObj = document.getElementById(childIdName);
			    var childList = parentObj.getElementsByTagName(childObj.tagName); //array containing the elements within parent
				for (var x=0; x<childList.length; x++){ //loop through each child element
					if (childList[x].id == childIdName) {
						childList[x].className = toClass;
					}
					else {
						childList[x].className = '';
					}
				}
			}
			break;
	}
}

// show/hide elements within a collection where childIdName is the element to show
function changeLayer(parentIdName,childIdName) {
	if (checkID(parentIdName) && checkID(childIdName)) {
		var parentObj = document.getElementById(parentIdName);
		var childObj = document.getElementById(childIdName);
	    var childList = parentObj.getElementsByTagName(childObj.tagName); //array containing the elements within parent

	    for (var x = 0; x < childList.length; x++)
		 { //loop through each child element

		     if ((childList[x].className == "show") || (childList[x].className == "hidden")) 
			{
			    if (childList[x].id == childIdName) 
				{
				    childList[x].style.display = 'block';
				    if (childList[x].id == "featureContent2") {
				        document.getElementById("table1").style.backgroundColor = 'Black';
				    }
				    else {
				        document.getElementById("table1").style.backgroundColor = 'White';
				    }
				}
				else
				{
					childList[x].style.display = 'none';
				}
			}
		}
	}
}

function invokeEvent(parentIdName,childIdName,event) {
	if (checkID(parentIdName) && checkID(childIdName)) {
		var parentObj = document.getElementById(parentIdName);
		var childObj = document.getElementById(childIdName);
	    var childList = parentObj.getElementsByTagName(childObj.tagName); //array containing the elements within parent
		for (var x=0; x<childList.length; x++) { //loop through each child element
			if (childList[x].id == childIdName) {
				if (event == "onclick") {childList[x].onclick();break;}
				else if (event == "ondblclick") {childList[x].ondblclick();break;}
				else if (event == "onmousedown") {childList[x].onmousedown();break;}
				else if (event == "onmouseup") {childList[x].onmouseup();break;}
				else if (event == "onmouseover") {childList[x].onmouseover();break;}
				else if (event == "onmousemove") {childList[x].onmousemove();break;}
				else if (event == "onmouseout") {childList[x].onmouseout();break;}
				else if (event == "onkeypress") {childList[x].onkeypress();break;}
				else if (event == "onkeydown") {childList[x].onkeydown();break;}
				else if (event == "onkeyup") {childList[x].onkeyup();break;}
			}
		}
	}
}

// changes a stylesheet class to a specified ID
function changeStyle(idName,styleType,toStyle) {
	if (checkID(idName)) {
		eval("document.getElementById(idName).style."+styleType+"="+toClass);
	}
}

function showSub(hubName,navName) {
	changeClass("top"+navName,'on');
	changeClass("link"+navName,'on');
	showMe(hubName+"sub"+navName);
}

function hideSubs(hubName,navName) {
	changeClass("top"+navName,'');
	changeClass("link"+navName,'');
	for (n = 0; n < navArray.length; n++) {
		hideMe(hubName+"sub"+navArray[n]);
	}
}

function iFrameHeight(idName) {
	if(document.getElementById(idName)) {
		if (idName=="commentsFrame") {
			if (document.getElementById(idName).contentDocument) {
				h = document.getElementById(idName).contentDocument.getElementById("mac50template").offsetHeight+FFextraHeight;
			} else {
				h = document.frames(idName).document.getElementById("mac50template").scrollHeight;
			}
		} else {

			if (document.getElementById(idName).contentDocument) {
				h = document.getElementById(idName).contentDocument.body.offsetHeight+FFextraHeight;
			} else {
				h = document.frames(idName).document.body.scrollHeight;
			}
		}
		document.getElementById(idName).style.height = h;
	}
}

function tabNext(t,l,f) {
	if (t.value.length == l) {
		focusMe(f);
	}
}

function removeError(str) {
	var space;
	if (str.indexOf("error") >=0) { // keep only until space
		space = str.indexOf(" ");
		return str.substr(0,space);
	} else {
		return (str);
	}
}

function checkForm(arrayStr) {
	var splitStr = arrayStr.split("|");
	var count = 0;
	for (s = 0; s < splitStr.length; s++) {
		if (getValue(splitStr[s])=="") {
			count++;
			alert("Please fill in all required fields.");
			focusMe(splitStr[s]);
			changeClass(splitStr[s],getClassName(splitStr[s])+" error");
			break;
		} else {
			//alert();
			changeClass(splitStr[s],removeError(getClassName(splitStr[s])));
		}
	}
	if (count == 0) {
		submitMe('quoteForm');
	}
}

function Popup(myUrl,popType) {
	var winName='flames';
	var winTop=50;
	var winLeft=150;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();

	switch (popType) {
		case 0:   // gift popups
	  	pop = window.open(myUrl,winName,'width=600,height=350,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
	}

	pop.focus();

}

function gradientDown(bg,x,gradHeight,step) {
	while (x >= 0) {
		document.write ("<div style=\"opacity: "+x/100+"; filter: alpha(opacity="+x+"); background:"+bg+"; font-size:"+gradHeight+"px; height:"+gradHeight+"px; line-height:"+gradHeight+"px; width:100%;\"></div>");
		x = x - step;
	}
}

function gradientUp(bg,x,gradHeight) {
	while (x <= 100) {
		document.write ("<div style=\"opacity: "+x/100+"; filter: alpha(opacity="+x+"); background:"+bg+"; font-size:"+gradHeight+"px; height:"+gradHeight+"px; line-height:"+gradHeight+"px; width:100%;\"></div>");
		x = x + step;
	}
}

//Qualify an HREF
function qualifyHREF(href)
{
    //get the current document location object
    var loc = document.location;

	//if absolute path
	if (href.substr(0,4) == "http") {
		//need to add the port if missing because IE skip it
		if (href.indexOf(loc.port) == -1) {
			href = href.replace(loc.hostname,loc.host);
		}
	}

	return href;
}

function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;

  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

function submitLoginForm(loginForm) {
	var re_email=/^[a-zA-Z][\'\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/i;
	if ((loginForm.username.value == undefined) ||
	    (loginForm.username.value != undefined && loginForm.username.value.search(re_email)<0)) {
		alert('To log in, please enter a valid email address.')
		return false;
	}
	else if ((loginForm.password.value == undefined) ||
			 (loginForm.password.value == "")) {
		alert('To log in, please enter a password.')
		return false;
	}

	return loginForm.submit();
}

function encode_utf8( s ) {
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s ) {
  return decodeURIComponent( escape( s ) );
}
