//######## Open Centered Window #############
function OpenWindow(url, width, height)
{
	var x, y = 0;
	if (screen) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	msgWindow=window.open(url,'new',options);		
}

//######## RLTV #############
function ralphlaurentv(mid)
{
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=792,height=530";
	if (mid==undefined) {		
		msgWindow=window.open("http://entertainment.ralphlauren.com/rltv/launch.asp?ab=footer_rltv","rltv1",options);
	} else {
		msgWindow=window.open("http://entertainment.ralphlauren.com/rltv/media.asp?media_id="+mid,"rltv1",options);	
	}
}
//######## Rollover Functions #############
function prl_preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=prl_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function prl_swapImgRestore() {
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function prl_findObj(n, d) {
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=prl_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function prl_swapImage() {
	var i,j=0,x,a=prl_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=prl_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function sideNavAttachJS(key) {
    if(key == undefined){ key='' };
    var innerTbl = returnObjById("sideNavTbl"); 
    if(innerTbl != undefined){
        var cells = innerTbl.getElementsByTagName("td");
        for (var i=0; i < cells.length; i++){
            if(cells[i].className=="title"+key || cells[i].className=="title"+key+"_on" || cells[i].className=="item"+key || cells[i].className=="item"+key+"_on"){
                cells[i].onmouseover = function(){ this.className+='_over' };
                cells[i].onmouseout = function(){ this.className=this.className.replace('_over','') };
            }
        }
    }
}


function paintDivSwap(outerDivId,selDivId) {
	var mainDiv = returnObjById(outerDivId); 
	if(mainDiv != undefined){
		var divs = mainDiv.getElementsByTagName("div");
		for (var i=0; i < divs.length; i++){
			if(divs[i].id == selDivId){
				getStyleObject(divs[i].id).display = "block";
			} else {
				getStyleObject(divs[i].id).display = "none";
			}
		}
	}
}

//############ Other Functions ##############

/* add method to JavaScript Array */
Array.find = function(ary, element){    
    for(var i=0; i<ary.length; i++){        
        if(ary[i] == element){            
            return i;        
        }    
    }    
    return -1;
}

function PaintPalette(secure)
{
	if(secure == '1') {
		OpenWindow('http://ralphlaurenhome.com/rlhome/products/paint/paint_palette_frameset.asp', 775, 595);
	} else {
		OpenWindow('/rlhome/products/paint/paint_palette_frameset.asp', 775, 595);
	}
}
function PaintQualitySide(URL)
{
	var width = 572;
	var height = 369;
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }

	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	msgWindow=window.open(URL,"PaintQualitySide",options);
}
function SitePopup(URL)
{
	var width = 320;
	var height = 240;
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	msgWindow=window.open(URL,"RLPOPUP",options);
}
function PrintItemS7(rlitemid)
{
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=10,height=10";
	msgWindow=window.open("/rlhome/item_print.asp?item_id="+rlitemid,"RLHomePrint",options);
}
function PrintPagepopup(rlitemid)
{
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=10,height=10";
	msgWindow=window.open("/rlhome/pdf_gen.asp?item_id="+rlitemid,"RLHomePrint",options);
}
function S7Popup(URL)
{
	var width = 662;
	var height = 448;
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	msgWindow=window.open(URL,"S7Pop",options);
}
function S7ZoomPopup(URL)
{
	var width = 390;
	var height = 515;
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	msgWindow=window.open(URL,"S7ZoomPop",options);
}

function Pricelist()
{
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=300";
	msgWindow=window.open("/rlhome/products/pricelist.asp","PriceList",options);
}

function OpenCare() {
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=320,height=470";
	msgWindow=window.open("care_popup.asp","Care",options);
}


function setDetailsPosition(detailsObj, posObj) {
	var locObj = returnObjById(posObj);
	var leftX, topY;
	if((browserDetect('opera'))||(browserDetect('firefox')))
	{
		leftX = findPosX(locObj) + 91;
		topY = findPosY(locObj) + 21;
	}
	else if(browserDetect('msie'))
	{
		leftX = findPosX(locObj) + 111;
		topY = findPosY(locObj) + 21;
	}
	getStyleObject(detailsObj).left = leftX + 'px';
	getStyleObject(detailsObj).top = topY + 'px';
	toggleVisibility(detailsObj);
}

function showPaintCart() {
	getStyleObject("paintcartimg").visibility = "visible";
}
			
//##########################

function WriteInnerHTML(divId,strHtml){
 if (document.getElementById) {
       document.getElementById(divId).innerHTML= strHtml;
 } else {
     with (document.layers[divId].document) {
     open();
     write(strHtml);
     close();
     }
 }
}

function getStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) { // W3C DOM	    
	    return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) { // MSIE 4 DOM	    
	    return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) { // NN 4 DOM.. note: this won't find nested layers	    
	    return document.layers[objectId];
    } else {
	    return false;
    }
}

function toggleDisplay(obj) {
    var currentDisplay = (getStyleObject(obj).display == 'none') ? 'block' : 'none';
    getStyleObject(obj).display = currentDisplay;
}

function displayObj(obj) {
    getStyleObject(obj).display = 'block';
}

function toggleVisibility(obj) {
	var currentVis = (getStyleObject(obj).visibility == 'hidden') ? 'visible' : 'hidden';
	getStyleObject(obj).visibility = currentVis;
}


function returnObjById(id) { 
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
}

function browserDetect(text) {
   str = (navigator.userAgent.toLowerCase()).indexOf(text) + 1;
   data = text;
   return str;
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


//##########################
var agent = navigator.userAgent.toLowerCase(); 
var mac = agent.indexOf('mac') != -1;
var w3c = document.getElementById ? true : false;
var iex = document.all ? true : false;
var ns4 = document.layers ? true : false;

function fixNetscape(){
	if(origWidth != window.innerWidth || origHeight != window.innerHeight){
		window.location.reload();
	}	
}
if(ns4){
	origWidth = window.innerWidth;
	origHeight = window.innerHeight;
	window.onresize = fixNetscape;
}

var w3c = (document.getElementById) ?true:false;
var iex = (document.all)            ?true:false;
var ns4 = (document.layers)         ?true:false;
var supported = (w3c || iex || ns4) ?true:false;

var active = false;
var curObj,curNest;

function mousemoved (evt){
	if(iex){
		mousex = window.event.clientX+document.body.scrollLeft;
		mousey = window.event.clientY+document.body.scrollTop;
	}
	else if(ns4){
		mousex = evt.pageX+window.pageXOffset;
		mousey = evt.pageY+window.pageYOffset;
	}
	else{
		mousex = evt.pageX;
		mousey = evt.pageY;
	}
	if(active){
		shiftTo(curObj, mousex-140, mousey-150, curNest);
	}
	return true;
}

function getStyle (objstr, nest){
	nest = (nest) ? "document."+nest+"." : "";
	return (w3c) ? document.getElementById(objstr).style : (iex) ? document.all[objstr].style : (ns4) ? eval(nest+"document."+objstr) : false;
}

function shiftTo (objstr, x, y, nest){
var x = x + 155;
var y = y + 80;
	var obj = getStyle(objstr,nest);
	if(iex){
		obj.pixelLeft = x;
		obj.pixelTop = y;
	}
	else if(ns4){
		obj.moveTo(x,y);
	}
	else if(w3c){
		obj.left=x+"px";
		obj.top=y+"px";
	}
}
function show (objstr,nest){
	curObj = objstr;
	curNest = (nest) ? nest : null;
	getStyle(objstr,nest).visibility = "visible";
	active = true;
}
function hide (){
	getStyle(curObj,curNest).visibility = "hidden";
	active = false;
}

if(supported){
	if(ns4){
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = mousemoved;
}

$(document).ready(function() {
	
	if($.cookie("rlhome%5Fpaint%5Fcart") != null && $.cookie("rlhome%5Fpaint%5Fcart") != "") {
		showPaintCart();
	}

	if($.cookie("RLH%5FOrderINQ") != null && $.cookie("RLH%5FOrderINQ") != "") {
		
		var orderInq = $.cookie("RLH%5FOrderINQ").toString().split("|");
		
		orderInqCount = jQuery.grep(orderInq, function(a) {
			return a.toString() != "";
		});
		
		orderInqCount = orderInqCount.length;
		
		if(orderInqCount > 0) {
			var orderInqView = $("#tdOrderInq a").text(orderInqCount.toString() + " Item(s) in Order Inquiry");
			$("#tdOrderInq").show();
		}
	}
	
	if($.cookie("RLH%5FOrderINQ%5FCollection") != null && $.cookie("RLH%5FOrderINQ%5FCollection") != "") {
	
		var orderInqCollCount = $.cookie("RLH%5FOrderINQ%5FCollection").toString().split("|");
		
		orderInqCollCount = jQuery.grep(orderInqCollCount, function(a) {
			return a.toString() != "";
		});
		
		orderInqCollCount = orderInqCollCount.length;
		
		if(orderInqCollCount > 0) {
			var orderInqView = $("#tdOrderInqColl a").text(orderInqCollCount.toString() + " Images(s) in Order Inquiry");
			$("#tdOrderInqColl").show();
		}
	}
});