var ns4 = document.layers;
var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1)||(navigator.userAgent.indexOf("Opera/5")!=-1);
var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1);
var agt=navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);
var ie = (agt.indexOf("msie") != -1); 
var mac_ie = mac && ie;

var imageWin = null;

if (document.images){
	preload_image_object = new Image();
	// set image url
	image_url = new Array();
	image_url[0] = "images/headers/english/GH-headerbg.jpg";
	image_url[1] = "content/skins/ModernVista/styles/menu/six_0.gif";
	image_url[2] = "content/skins/ModernVista/styles/menu/six_0a.gif";
	image_url[3] = "images/custom/hp-banner_2.jpg";

	 var i = 0;
	 for(i=0; i<=3; i++) 
		 preload_image_object.src = image_url[i];
}

function getRealLeft(el) {
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(el) {
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while(tempEl != null){
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function showHideMenuNode(node_id, image_id){
	n = document.getElementById(node_id);
	i = document.getElementById(image_id);
	if(n){
		n.style.display = n.style.display == "none" ? "block" : "none";
		if(i){
			i.src = n.style.display == "none" ? skin_images + "/menu_tree_plus.gif" : skin_images + "/menu_tree_minus.gif";
		}
	}
}

function getElementHeight(Elem) {
	if(ns4){
		var elem = document.getElementById(Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = document.getElementById(Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}


if(document.layers){
	_browser = "nn";
}
if(document.all){
	_browser = "ie";
}
if(navigator.userAgent.toLowerCase().match("gecko")){
	_browser= "gecko";
}
function isEmail(entry){
	var rex= /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/	
	return rex.test(entry); /* " */
}


function delay(gap){ /* gap is in millisecs */
	var then, now; 
	then = new Date().getTime();
	now = then;
	while((now-then) < gap){
		now=new Date().getTime();
	}
}

function showImage(image_url, image_width, image_height){
	image_width = image_width == "" ? 400 : image_width;
	image_height = image_height == "" ? 380 : image_height;

	var w = screen.width;
	var h = screen.height;
	var ww = image_width * 1 + 30;
	var wh = image_height * 1 + 45;
	var wx = (w - ww)/2;
	var wy = (h - wh)/2;
	
	if(imageWin != null){
		imageWin.close();
	}
	imageWin = null;
	imageWin = window.open(
		"", 
		"ProductImageWindow", 
		"titlebar=yes, toolbar=no, menubar=no, status=no, directories=no, resizable=yes, scrollbars=yes, top=" + wy.toString() + ", left=" + wx.toString() + ", width=" + ww.toString() + ", height=" + wh.toString() + ""
	);
	while(imageWin==null);
	imageWin.focus();
	
	imageWin.document.body.innerHTML = "";
	imageWin.document.write('<body style="padding:5px;margin:0px">');
	imageWin.document.write('<div align="center"><img hspace="0" vspace="0" src="' + image_url + '"></div><br/>');
	imageWin.document.write('<div align="center" style="font-family:arial;font-color:black;font-size:11px;"><a href="javascript:window.close();" style="color:#0000AA;">Close Window</a></div>');
	imageWin.document.write('</body>');
	imageWin.width = ww;
	imageWin.height = wh;
}

function showPrinterPage(url){
	var prWin = null;
	prWin = window.open(
		url,
		"PrintVer", 
		"titlebar=yes, toolbar=no, menubar=yes, status=yes, directories=no, resizable=yes, scrollbars=yes, top=20, left=20, width=810, height=600"
	);
	while(prWin==null);
	prWin.focus();
}

function OnButton(bt){
	document.images[bt].src = skin_images + bt + "_on.gif";
	
}
function OffButton(bt){
	document.images[bt].src = skin_images + bt + "_off.gif";
}

function OnMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_on.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_on.gif";
}
function OffMenu(cid){
	document.images["menul_" + cid].src = skin_images + "catl_bg_off.gif";
	document.all["menur_" + cid].background = skin_images + "catr_bg_off.gif";
}

function OnMenu(img){
	document.images[img].src = skin_images + "menu_arrow_on.gif";
}
function OffMenu(img){
	document.images[img].src = skin_images + "menu_arrow.gif";
}

function ShowPopup(src){
	var bWin = null;
	bWin = window.open(
		src, 
		"PopupWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=320, height=240"
	);
	while(bWin==null);
	bWin.focus();
}

function PopUpImage(image_source, image_width, image_height){
	var bWin = null;
	bWin = window.open(
		image_source, 
		"ImageWind", 
		"titlebar=no, toolbar=no, menubar=no, status=no, directories=no, resizable=no, scrollbars=no, top=20, left=20, width=" + (image_width + 20) + ", height=" + (image_height + 20)
	);
	while(bWin==null);
	bWin.focus();
}

function ConfirmLogout(){
	if(orderItemsCount > 0){
		if(confirm("You have items in your cart. Logging out will empty your cart\nAre you sure want to continue?")){
			document.location = urlLogout;
		}
	}
	else{
		if(confirm("Do you really want to logout?")){
			document.location = urlLogout;
		}
	}
}

function CartConfirmDeleteItem(ocid){
	if(confirm(msg_confirm_delete_item)){
		document.location = CartDeleteItemUrl + '&ocid=' + ocid;
	}
}
function CartConfirmEmpty(){
	if(confirm(msg_confirm_empty_cart)){
		document.location = CartEmptyUrl;
	}
}

function getViewportSize() {
	var viewportSize = [];
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7)
	// use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportSize[0] = window.innerWidth,
		viewportSize[1] = window.innerHeight
	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'
			&& typeof document.documentElement.clientWidth !=
			'undefined' && document.documentElement.clientWidth != 0) {
		viewportSize[0] = document.documentElement.clientWidth,
		viewportSize[1] = document.documentElement.clientHeight
	}
 
	// older versions of IE 
	else {
		viewportSize[0] = document.getElementsByTagName('body')[0].clientWidth,
		viewportSize[1] = document.getElementsByTagName('body')[0].clientHeight
	}
	
	return viewportSize;
}

function partnerPromoPopup(partner_promo, w, h, t) {
	$("body").append('<div id="divPartnerPromo"></div>');
	var d = $("#divPartnerPromo");
	d.after('<div id="divMask"></div>');
	$("#divMask").css("display", "block");
	d.html(partner_promo);
	d.width(w);
	d.height(h);
	d.css("top", t+"px");
	d.css("left", "50%");
	d.css("marginLeft", "-"+((w+60)/2)+"px");
	d.fadeIn("slow");
}
function closePartnerPromoPopup() {
	$("#divPartnerPromo").fadeOut("slow", function(){$("#divMask").css("display", "none")});
}

// Additional string methods trim(), left(), right()
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
String.prototype.right = function (n) {
	if (n <= 0)     // Invalid bound, return blank string
		 return "";
	else if (n > this.length)   // Invalid bound, return
		 return this;                     // entire string
	else  // Valid bound, return appropriate substring
		 return this.substring(this.length, this.length - n);
}
String.prototype.left = function (n) {
	if (n <= 0)
		return "";
	else if (n > this.length)
		return this;
	else
		return this.substr(0, n);
}
String.prototype.stripChars = function (charsToStrip) {
	s = this;
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
		var c = s.charAt(i);
		if (charsToStrip.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}
String.prototype.stripInvalidCharsFromCC = function () {
	return this.toLowerCase().stripChars('abcdefghijklmnopqrstuvwxyz `~!@#$%^&*()-_=+[{]}\|;:",<.>/?'+"'");
}
String.prototype.cleanProductDescription = function () {
	var temp = this.split("[");
	var prodDesc = temp[0].trim();
	prodDesc = prodDesc.right(2) == " -" ? prodDesc.substr(0, prodDesc.length - 2) : prodDesc;
	return prodDesc;
}

function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]) {
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++) {
			if(urlVars[i]) {
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					// Found match, load value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}