/* THESE ARE THE GLOBAL JAVASCRIPT FUNCTIONS.  THEY WILL APPEAR ON EVERY PAGE ON THE SITE */

function popupWindow(w_url,w_width,w_height) {
openWindow = window.open(w_url,"win"+w_height,"toolbar=0,location=0,scrollbars=1,directories=0,resizable=yes,status=0,menubar=0,width=" + w_width + ",height=" + w_height);
if (openWindow.opener == null) openWindow.opener = self;
openWindow.focus();
return false;
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function toggleLayer(whichLayer) {
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	elem = document.layers[whichLayer];
	vis = elem.style;
	// if the style.display value is blank we try to figure it out here
	switch (vis.display) {
		case 'block':
			vis.display = 'none';
		break;
		case '':
			vis.display = 'block';
		break;
		case 'none':
			vis.display = 'block';
		break;
	}
}



function sanitize_string(cleanString) { // used to prevent code injection & satisfy McAfee & trustmark
	if (cleanString.toLowerCase().indexOf('script')==-1 && cleanString.toLowerCase().indexOf('iframe')==-1 && cleanString.toLowerCase().indexOf('java')==-1) { // make sure 'java', 'script' or 'iframe' never are a variable
		cleanString.replace('<','&lt;'); // replace < with &lt;  per McAfee
		cleanString.replace('>','&gt;'); // replace > with &gt;
		cleanString.replace("'","&apos;"); // replace ' with &apos;
		cleanString.replace('"','&#x22;'); // replace " with &#x22;
		cleanString.replace(')','&#x29;'); // replace ) with &#x29;
		cleanString.replace('\(','&#x28;'); // replace ( with &#x28;
		return cleanString;
	} else {
		return null;	
		exit;
	}
}	

function redirect_string(toUrl) {  // redirect to supplied url - used on product_search_listing for "sort by" drop down menu
	if (sanitize_string(toUrl)) window.location=toUrl;
}

/* added 5/5/09 for setting a vehicle selected cookie on page_top.inc.php */
function select_vehicle(cookie_name,cookie_val,expire_days,ururl) {
	if (expire_days==null) expire_days=365;
	// make sure cookie_val is an integer
	// if ((cookie_name!=null) && (cookie_val==parseInt(cookie_val))) setCookie(cookie_name,cookie_val,expire_days);
	if ((cookie_name!=null) && (cookie_val==parseInt(cookie_val))) {
		if (parseInt(cookie_val)==1000 || parseInt(cookie_val)==1100 || parseInt(cookie_val)==1150 || parseInt(cookie_val)==1200 || parseInt(cookie_val)==1201) {
			// show the cherokee pages if cherokee selected
			if (ururl.indexOf('/default.php')==0) {  // indexOf() shows the first instance of a string as 0 if string doesn't exist then it returns -1
				ururl='/default_cherokee.php';
			} else if (ururl.indexOf('/categories/')==0 && ururl.indexOf('default_cherokee.php')==-1 && ururl.indexOf('overstock')==-1) {
				ururl=ururl.replace('default.php','default_cherokee.php');
			}
			setCookie('globv','Cherokee',expire_days);
		} else {  // a vehicle other than a cherokee was selected - show only wrangler pages
			if (ururl.indexOf('/default_cherokee.php')==0) {
				ururl='/default.php';
			} else if (ururl.indexOf('/categories/')==0 && ururl.indexOf('default.php')==-1) {
				ururl=ururl.replace('default_cherokee.php','default.php');
			}
			setCookie('globv','Wrangler',expire_days);
		}
		// add sv= to the end of the product search to indicate that a new vehicle was selected which takes presidence over the cv= value from a link
		if (ururl.indexOf('sv=')!==-1) ururl=ururl+cookie_val;
		setCookie(cookie_name,cookie_val,expire_days);
   	}
   	window.location = ururl;
//   	window.location.reload();
}

function setCookie(cookie_name,cookie_val,expire_days) {
	var expire_date = new Date();
	expire_date.setDate(expire_date.getDate()+expire_days);
	document.cookie=cookie_name+ "=" +escape(cookie_val)+ "; path=/"+ ((expire_days==null) ? "" : "; expires="+expire_date.toUTCString());
}

