// Browser Info
function UFcheckBrowser(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase();
	this.dom=document.getElementById?1:0
	this.all=document.all?1:0;
	this.layers=document.layers?1:0;
	this.images=document.images?1:0;
	this.IE = navigator.appName=="Microsoft Internet Explorer"?1:0;
	this.NS = navigator.appName=="Netscape"?1:0;
	this.MAC = (this.agent.indexOf("mac")!=-1);
	return this
}

oUFBrowser=UFcheckBrowser();

function UFformatCurrency(sNum) {
	sNum = sNum.toString().replace(/\$|\,/g,'');
	oUFBrowser=UFcheckBrowser();
	if(isNaN(sNum))
		sNum = "0";
	
	var sTemp = sNum;
	
	var bSign = (Number(sNum) == (sNum = Math.abs(sNum)));
	sNum = Math.floor(sNum*100+0.50000000001);
	var sCents = sNum%100;
	sNum = Math.floor(sNum/100).toString();
	
	if(sCents<10)
		sCents = "0" + sCents;

	for (var i = 0; i < Math.floor((sNum.length-(1+i))/3); i++)
		sNum = sNum.substring(0,sNum.length-(4*i+3))+ ',' + sNum.substring(sNum.length-(4*i+3));

	return (((bSign)?'':'-') + '$' + sNum + '.' + sCents);
}

function UFChangeImage(sIMGName , sImageSRC , sLayerName) {

	oUFBrowser=UFcheckBrowser();

	var sLayerPrepend = "";

	if (typeof(sLayerName) != 'undefined' && sLayerName != '') {
		if ( (oUFBrowser.dom) && ("" + document.getElementsByName(sIMGName).item(0).src != 'undefined') ) { 
			// Since we're doing getElementsByName, we don't need to get the layer document
		} else if (oUFBrowser.layers) { 
			sLayerPrepend = "document.layers[sLayerName].";
		} else if (oUFBrowser.all) {
			sLayerPrepend = "document.all[sLayerName].";
		}
	}

	if (oUFBrowser.images) {
		if ( (oUFBrowser.dom) && ("" + document.getElementsByName(sIMGName).item(0).src != 'undefined') ) { 
			// For these, we're still using getElementsByName which returns a collection
			// However, since we're supporting older browsers, we don't want to give each image a "name" AND "id"
			// So, since there is only one element with this name (or else the older browser functionality would not work properly),
			// We'll just grab the first element of the collection
			eval(sLayerPrepend + "document.getElementsByName(sIMGName).item(0).src = sImageSRC");
		} else if (oUFBrowser.layers) { 
			eval(sLayerPrepend + "document.images[sIMGName].src = sImageSRC");
		} else if (oUFBrowser.all) {
			eval(sLayerPrepend + "document.images[sIMGName].src = sImageSRC");
		}
	}
}

function UFChangeDivText(sName , sText) {

	oUFBrowser=UFcheckBrowser();

	if (oUFBrowser.dom)  { 
		if (document.getElementById(sName)) {
			document.getElementById(sName).innerHTML = '';
			document.getElementById(sName).innerHTML = sText;
		}
	} else if (oUFBrowser.all) {
		if (document.all[sName]) {
			document.all[sName].innerHTML = '';
			document.all[sName].innerHTML = sText;
		}
	} else if (oUFBrowser.layers) {	
		if (document.layers[sName]) {
			document.layers[sName].document.open();
			document.layers[sName].document.writeln('');
			document.layers[sName].document.writeln(sText);
			document.layers[sName].document.close();
		}
	}

}

function UFHideLayer(sLayerName) { 

	oUFBrowser=UFcheckBrowser();

	if (oUFBrowser.dom) { 
		if (document.getElementById(sLayerName)) {
			document.getElementById(sLayerName).style.visibility = 'hidden';
		}
	} else if (oUFBrowser.layers) {
		if (document.layers[sLayerName]) {		
	        document.layers[sLayerName].visibility = 'hide';	
		}
	} else if (oUFBrowser.all)  {
		if (document.all[sLayerName]) {
	        document.all[sLayerName].style.visibility = 'hidden';
//	       	document.all[sLayerName].style.zIndex = -100;
		}
	}
}

function UFShowLayer (sLayerName) {  

	oUFBrowser=UFcheckBrowser();

	if (oUFBrowser.dom) { 
		if (document.getElementById(sLayerName)) {
			document.getElementById(sLayerName).style.visibility = 'visible';
		}
	} else if (oUFBrowser.layers) {
		if (document.layers[sLayerName]) {
		        document.layers[sLayerName].visibility = 'visible';
		}
	}  else if (oUFBrowser.all) { // Internet Explorer
			if (document.all[sLayerName]) {
		        document.all[sLayerName].style.visibility = 'visible';
//		        document.all[sLayerName].style.zIndex = 100;
			}
	}
}

function UFSwitchLayer(sLayerName) {

	oUFBrowser=UFcheckBrowser();

	if (oUFBrowser.dom) { 
		if (document.getElementById(sLayerName)) {
			if (document.getElementById(sLayerName).style.visibility == 'hidden') {
				UFShowLayer(sLayerName);
			} else {
				UFHideLayer(sLayerName);
			}
		}
	} else if (oUFBrowser.layers) {
		if (document.layers[sLayerName]) {
			if (document.layers[sLayerName].visibility == 'hide') {
				UFShowLayer(sLayerName);
			} else {
				UFHideLayer(sLayerName);
			}
		}
	} else if (oUFBrowser.all) {
		if (document.all[sLayerName]) {
			if (document.all[sLayerName].style.visibility=='hidden') {
				UFShowLayer(sLayerName);
			} else {
				UFHideLayer(sLayerName);
			}
		}
	}
}

function UFMoveLayer(sName,iX,iY) {
	oUFBrowser=UFcheckBrowser();
	if(oUFBrowser.layers) {
		if (document.layers[sName]) {
			document.layers[sName].left=iX;
			document.layers[sName].top=iY;
		}
	} else if(oUFBrowser.all) {
		if (document.all[sName]) {
			document.all[sName].style.left=iX;
			document.all[sName].style.top=iY;
		}
	} else if(oUFBrowser.dom)  { 
		if (document.getElementById(sName)) {
			document.getElementById(sName).style.left=iX;
			document.getElementById(sName).style.top=iY;
		}
	}
}

function UFWindowCreate(sName, sType, sScreenLocation, sScrollable, sResizable, iWidth, iHeight, sURL) {
	oUFBrowser=UFcheckBrowser();
	var sWindowParams="";
	var iWindowLeft=0;
	var iWindowTop=0;
	
	// Set "Is Scrollable" Default
	if(sScrollable !="yes" && sScrollable !="no") {
		sScrollable ="no"; 
	} 
	
	// Set "Is Resizable" Default
	if(sResizable !="yes" && sResizable !="no") {
		sResizable ="no"; 
	} 
	
	// Predefined Window Types
	switch (sType) {
		case "mini":	
			sWindowParams="resizable="+sResizable+",status=yes,location=yes,scrollbars="+sScrollable+",menubar=no,toolbar=no,directories=no,width="+iWidth+",height="+iHeight;
			break;
		case "compact":	
			sWindowParams="resizable="+sResizable+",status=no,location=no,scrollbars="+sScrollable+",menubar=no,toolbar=no,directories=no,width="+iWidth+",height="+iHeight;
			break;
		case "small":	
			sWindowParams="resizable="+sResizable+",status=yes,location=no,scrollbars="+sScrollable+",menubar=no,toolbar=no,directories=no,width="+iWidth+",height="+iHeight;
			break;
		default:
		case "standard":	
			sWindowParams="resizable=yes,status=yes,location=yes,scrollbars="+sScrollable+",menubar=yes,toolbar=yes,directories=yes,width="+iWidth+",height="+iHeight;
			break;
	}

	// Predefined Screen Locations
	switch (sScreenLocation) {
		case "topright":
			iWindowLeft = (screen.width-iWidth);
			iWindowTop = 0;
			break;
		case "topcenter":
			iWindowLeft = ( (screen.width-iWidth) >>1 );
			iWindowTop = 0;
			break;
		case "topleft":
			iWindowLeft = 0;
	   	  	iWindowTop = 0;
			break;		
		case "bottomright":
			iWindowLeft = (screen.width-iWidth);
			iWindowTop = (screen.height-iHeight);
			break;
		case "bottomcenter":
			iWindowLeft = ( (screen.width-iWidth) >>1 );
			iWindowTop = (screen.height-iHeight);
			break;
		case "bottomleft":
			iWindowLeft = 0;
	   	  	iWindowTop = (screen.height-iHeight);
			break;
		case "leftcenter":
			iWindowLeft = 0;
	   	  	iWindowTop = ( (screen.height-iHeight) >>1 );
			break;		
		case "rightcenter":
			iWindowLeft = (screen.width-iWidth);
			iWindowTop = ( (screen.height-iHeight) >>1 );
			break;
		default:
		case "center":
   			iWindowLeft = ( (screen.width-iWidth) >>1 );
	   	  	iWindowTop = ( (screen.height-iHeight) >>1 );
			break;
	}
	
	// raised all non-Mac windows by 27 pix to make room for start bar
	if(iWindowTop>27 && !oUFBrowser.MAC) {
		iWindowTop-=27;
	} 
	if (oUFBrowser.IE) {
		sWindowParams += ",top=" + iWindowTop + ",left=" + iWindowLeft;
  	} else if (oUFBrowser.NS) {
		sWindowParams += ",screenX=" + iWindowLeft + ",screenY=" + iWindowTop;
	}
	
	eval('WMWindow_'+sName+' = window.open("'+sURL+'", "WMWin_'+sName+'", "'+sWindowParams+'")');
	
	eval('setTimeout("UFExistThenFocus(WMWindow_'+sName+')", 2000)');
}

function UFExistThenFocus(oWindowName) {
	if(oWindowName) {
		if(!oWindowName.closed) {
			oWindowName.focus();
		}
	}
}

function UFWindowClose(sName) {
	eval('setTimeout("WMWindow_'+sName+'.close()", 1000)');
}

function UFValidateZipcode(sValue) {
	var re = /^\d{5}$|\d{5}-\d{4}$|\d{9}$/;
	return re.test(sValue);
}

function UFValidateEmail(sEmailAddress){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	bValidEmailReturn = filter.test(sEmailAddress);
	if(bValidEmailReturn) {
		filter  = /\./;
		bValidEmailReturn = !filter.test(sEmailAddress.substring((sEmailAddress.length-2),sEmailAddress.length));
	}
	return bValidEmailReturn;
	}



function UFValidateUSPhoneNum(sPhoneNumber) {
	var filter = /^\d{3}-\d{3}-\d{4}$/;
	return filter.test(sPhoneNumber);
}

function UFPrintFrame(frame) {
	oUFBrowser=UFcheckBrowser();
    // handle IE5
    if (window.print && this.IE) {
        frame.focus();
        window.print();
//        link.focus();
    
    // handle NS4
    } else if (window.print) {
        frame.print();
    
    // handle IE4 (not Mac)
    } else if (oUFBrowser.IE && !oUFBrowser.MAC) {
        frame.focus();
        // call vbscript function
        // then return focus to original frame
//        setTimeout("vbPrintPage(); link.focus();", 100);
		setTimeout("vbPrintPage();" , 1000);
    // other browsers
    } else {
	    var str = "";
	    str += "Your browser does not support automatic printing.\n";
	    str += "This page may be printed by selecting File -> Print\n";
	    str += "or by clicking the Print icon in the tool bar.";
    }
}

function UFPrintWindow() {
	oUFBrowser=UFcheckBrowser();
    // handle NS4, IE5
    if (window.print) { 
        window.print();

    // handle IE4 (not Mac)
    } else if (this.IE && !this.MAC) { 
        // call vbscript function
        vbPrintPage();
                
    // other browsers
    } else { 
	    var str = "";
	    str += "Your browser does not support automatic printing.\n";
	    str += "This page may be printed by selecting File -> Print\n";
	    str += "or by clicking the Print icon in the tool bar.";
    }
}

function UFPrePrint() {
	oUFBrowser=UFcheckBrowser();
	if (oUFBrowser.IE && !window.print && !oUFBrowser.MAC) {
		with (document) {
			write('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" ');
			writeln('CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2">');
			writeln('</OBJECT>');
			writeln('<' + 'SCRIPT LANGUAGE="VBScript">');
			writeln('Sub window_onunload');
			writeln('    On Error Resume Next');
			writeln('    Set WB = nothing');
			writeln('End Sub');
			writeln('Sub vbPrintPage');
			writeln('    OLECMDID_PRINT = 6');
			writeln('    OLECMDEXECOPT_DONTPROMPTUSER = 2');
			writeln('    OLECMDEXECOPT_PROMPTUSER = 1');
			writeln('    On Error Resume Next');
			writeln('    WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER');
			writeln('End Sub');
			writeln('<' + '/SCRIPT>');
		}
	}
}

function UFReloadDealerResults() {
	document.location = "/tools/locator/default.asp?SearchType=last";
}

function UFValidateForm(frm, fields) {
	if (typeof fields != "object") return false;
	var pass = true;
	for (i = 0; i < fields.length; i++) {
		var elem = eval("frm['" + fields[i] + "']");

		if (elem) {
			if (elem.type.indexOf("select") > -1) {
				if (elem.selectedIndex == 0) pass = false;
			} else {
				if (UFTrim(elem.value).length < 1) pass = false;
			}
		}
	}
	return pass;
}

function UFGetCookie(sName) { 
    sIndex = document.cookie.indexOf(sName + "=");
    if (sIndex == -1) {
		return null;
	}
    sIndex = document.cookie.indexOf("=", sIndex) + 1;
    sEndStr = document.cookie.indexOf(";", sIndex);
    if (sEndStr == -1) {
		sEndStr = document.cookie.length;
	}
    return unescape(document.cookie.substring(sIndex, sEndStr));
}

function UFJSPageSelf() {
	sPage=self.location.href;
	aPagePieces=sPage.split("/")
	sThisPage="";
	for(i=3;i<aPagePieces.length;i++) {
		sThisPage+="/"+aPagePieces[i];
	}
	aTempArray=sThisPage.split("?");
	return aTempArray[0];
}

function UFJSPageParams() {
	aTempArray=self.location.href.split("?");
	sThisPageParams=aTempArray[1];
	return sThisPageParams;
}

function UFTrim(sInput) {
	if (typeof sInput != "string") { 
   		return sInput; 
	}
   	var sRetValue = sInput;
   	var sChar = sRetValue.substring(0, 1);
   	while (sChar == " ") { 
      sRetValue = sRetValue.substring(1, sRetValue.length);
      sChar = sRetValue.substring(0, 1);
	}
	sChar = sRetValue.substring(sRetValue.length-1, sRetValue.length);
	while (sChar == " ") {
      sRetValue = sRetValue.substring(0, sRetValue.length-1);
      sChar = sRetValue.substring(sRetValue.length-1, sRetValue.length);
	}
	while (sRetValue.indexOf("  ") != -1) { 
		sRetValue = sRetValue.substring(0, sRetValue.indexOf("  ")) + sRetValue.substring(sRetValue.indexOf("  ")+1, sRetValue.length);
   	}
	return sRetValue;
} 

function UFGetLayer(sLayerName) {

	if (sLayerName == '') {
		return null;
	}

	oUFBrowser=UFcheckBrowser();

	if ( (oUFBrowser.dom) && (document.getElementById(sLayerName) != 'undefined') ) { 
		return document.getElementById(sLayerName);
	} else if (oUFBrowser.layers) { 
		return document.layers[sLayerName];
	} else if (oUFBrowser.all) {
		return document.all[sLayerName];
	}

}

function UFMakeTextAltFriendly(sText) {
	sText = sText.replace(/"/gi , '\'\'');		// Change double quotes to two single quotes
	sText = sText.replace(/<br>/gi , '\n');		// Change <br>'s to line returns
	return sText;
}