//  Legacy Javascript for enforcing maxlength 
function maxlengthCheck(txt, maxlength){
    if(txt.value.length > maxlength){
        txt.value = txt.value.substring(0,maxlength);
    }
}

/** Img rollover function. **/
function rollover(name,source){
    document.images[name].src=source;
}


/**
 * CSS rollover function. Swaps out one css className for another, based on it's On or Off state.
 * @param {Object} oElement This is a DOM element ref.
 */
function cssRollOver(oElement){
    var sCSSClass = oElement.className;
    if(sCSSClass.indexOf("On") != -1 ) {
      oElement.className = sCSSClass.replace(/On/, "Off");
    } else if (sCSSClass.indexOf("Off") != -1 ) {
      oElement.className = sCSSClass.replace(/Off/, "On");
    }
}

/** Preload image function: takes an array of img srouces. **/
function preload(aImgSrc){
    for (var i=0; i<aImgSrc.length; i++){
        var newImg = new Image();
        newImg.src = aImgSrc[i];
        aImgSrc["img_"+i] = newImg;
     }
}


//Global function for the search box in the top nav
function checkKeyword(keyword, defaultText) {
    var minKeywordLength = 0;
    var maxKeywordLength = 0;
    minKeywordLength += 3;
    maxKeywordLength += 30;
    var errMsg = '';
    //alert("minKeywordLength " + minKeywordLength);
    var searchTerms = '';
    searchTerms = keyword.value;
    //alert("searchTerms.length " + searchTerms.length);

	var invalidSearchTermMessage = 'Search term needed to be re-entered.'; // probably want to change this message! 
	var invalidChars = /[!?~`%^&*()+={}\[\]|\\:;\"<>/]/; // add all illegal characters here. 
	var matches_array = searchTerms.match(invalidChars);

    var noSearchTerm = 'Please enter a keyword or item number';
    var shortSearchTerm = 'Your keyword or item number must be at least 3 characters long';

    if (searchTerms == defaultText) {
        alert(noSearchTerm);
        return false;
    }
    if (searchTerms == '') {
        alert(noSearchTerm);
        return false;
    } else if ( searchTerms.length < minKeywordLength ) {
        alert(shortSearchTerm);
        return false;
    } else if (matches_array != null) { 
		// show the user a dialog if illegal characters are found in the keyword. 
		alert(invalidSearchTermMessage); 
		return false; 
    } else {
      return true;
    }
  }
  
function flyopen(width, height){
    var ieIncrement = ((navigator.appName+"").indexOf("Netscape") == -1)? 15:0;
    width = (width && !isNaN(width))? width:null;
    height = (height && !isNaN(height))? height:null;
    var winURL = (arguments[2])? arguments[2]:null;
    var winName = (arguments[3])? arguments[3]:"generic";
    var winParms = (arguments[4])? arguments[4]+",width="+(width + ieIncrement)+",height="+(height + ieIncrement):"resizable=yes,scrollbars=yes,width="+(width + ieIncrement)+",height="+(height + ieIncrement)+",top=5,left=75";

	//only launch the window if we've got a width, height and url
	if (width && height && winURL){
        eval(winName+"=window.open('"+ winURL +"','"+ winName +"','" + winParms + "')");
        eval("window."+ winName +".focus()");
	}
}

// Dreamweaver Generic Rollover code

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_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 MM_swapImgRestore() { //v3.0
  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 MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// ** Virid Nav Code **
var prevTarg,prevRollon,prevRolloff,prevNum,prevLay;
var defRollon,defRolloff,defIndex;

function LayerOff()  {
	var inaSwitch = 'sp'+[prevNum];
	var inbSwitch = 'sp'+[prevNum+1];
	
	document.images[prevTarg].src = prevRolloff;
	if (prevNum >1) {
		document.images[inaSwitch].src = '/images/mnav_sp_line.gif';
	} else {
		document.images[inaSwitch].src = '/images/mnav_sp_off.gif';
	}
////////  Change the test Value below if you change the number of nav items!!!!	
	if (prevNum == 11) {
		document.images[inbSwitch].src = '/images/mnav_sp_off.gif';
	} else {
		document.images[inbSwitch].src = '/images/mnav_sp_line.gif';	
	}
	MM_showHideLayers(prevLay,'','hide');
}

var blnNavTimer = false;
var curMenu = "";
function ClearAllSubnavs(restore) {
	if (blnNavTimer) {
		//alert('clearing');
		if(prevLay) { LayerOff(); }
		
		MM_showHideLayers('dvMenu_1','','hide','dvMenu_2','','hide','dvMenu_3','','hide','dvMenu_4','','hide','dvMenu_5','','hide','dvMenu_6','','hide','dvMenu_7','','hide','dvMenu_8','','hide','dvMenu_9','','hide','dvMenu_10','','hide','dvMenu_11','','hide');

		//restore the default, if present
		if (restore && defIndex) {
			var nv,imgSrc,imgOldSrc,lay;
			nv='nv'+defIndex;
			lay='nav'+defIndex;
			imgSrc = defRollon;
			imgOldSrc = defRolloff;
			popup(nv,imgSrc,imgOldSrc,lay)
		}
		
		blnNavTimer = false;
	} else {
	}
}
function IsIE6()
{
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    // In Internet Explorer, the true version is after "MSIE" in userAgent
    if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
         var fullVersion  = parseFloat(nAgt.substring(verOffset+5));
         return (parseInt(''+fullVersion)>6)?false:true;
    }else return false;

}

function getCoords(object){
    var position = new Object;
    position.x = 0;
    position.y = 0;

    if( object ) {
        position.x = object.offsetLeft;
        position.y = object.offsetTop;

        //alert ("Looping X: "+position.x+"\nY: "+position.y);
        if( object.offsetParent ) {
            var parentpos = getCoords(object.offsetParent);
            position.x += parentpos.x;
            position.y += parentpos.y;
        }
    }

    position.cx = object.offsetWidth;
    position.cy = object.offsetHeight;

    return position;
}

//var to hold the iframe for the IE6Hack
var g_PopupIFrame; 
function vShowMenu(menu) {
    blnNavTimer = false;
     // Close quick cart
     /*
     var divQS = document.getElementById("showCart");
     if (divQS.style.display != 'none') {
        closeQuickCart();
        MM_showHideLayers('showCart','','hide');
     }
     */  
    
    //if old menu exists, clear it
    if (curMenu) {
        obj=document.getElementById(curMenu); 	
        if (obj) {
            obj.style.visibility='hidden';
            if (IsIE6())
            {
                if (g_PopupIFrame) document.body.removeChild(g_PopupIFrame);
                g_PopupIFrame=null;
            }
	   	}
    }
    
    var divPopup=document.getElementById(menu); if (!divPopup) return;
	
	curMenu=menu;
    
    if (!IsIE6())
    {
        //Just display the div
        divPopup.style.visibility ="visible";
        return;
    }

    //Increase default zIndex of div by 1, so that DIV appears before IFrame
    divPopup.style.zIndex=divPopup.style.zIndex+1;

    var iFrame = document.createElement("IFRAME");
    iFrame.setAttribute("src", "javascript:''");
    iFrame.frameBorder="0";
    iFrame.style.filter = "alpha(opacity=0)";
	iFrame.style.opacity = "0.80";
	//iFrame.style.-moz-opacity = "0.80";

    //Match IFrame position with divPopup
    
    iFrame.style.position="absolute";
    var objXY = getCoords(divPopup);
    iFrame.style.left = objXY.x + 'px';
    iFrame.style.top = objXY.y + 'px';
    iFrame.style.width =divPopup.offsetWidth + 'px';
    iFrame.style.height =divPopup.offsetHeight + 'px';

    document.body.appendChild(iFrame);

    //Store iFrame in global variable, so it can get removed when divPopup is hidden 
    g_PopupIFrame=iFrame;
    divPopup.style.visibility ="visible";
	
}
function vHideMenu(menu) {
    //window.status = 'current-'+menu;
	blnNavTimer = true;
	setTimeout("vClearMenu('"+menu+"')",350);
}

function vClearMenu(menu) {
	if (blnNavTimer) {
	    //window.status ='clearing...';

	   	obj=document.getElementById(menu); if (obj) {
            obj.style.display='block';
	        if (IsIE6())
            {
                 if (g_PopupIFrame) document.body.removeChild(g_PopupIFrame);
                g_PopupIFrame=null;
            }
	   	}
   		MM_showHideLayers('dvMenu_1','','hide','dvMenu_2','','hide','dvMenu_3','','hide','dvMenu_4','','hide','dvMenu_5','','hide','dvMenu_6','','hide','dvMenu_7','','hide','dvMenu_8','','hide','dvMenu_9','','hide','dvMenu_10','','hide','dvMenu_11','','hide');
	   	MM_showHideLayers(menu,'','hide');
	    blnNavTimer=false;
	    //window.status ='done clearing...'+menu;
	}
}
function MM_findObj(n, d) { //v4.0
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=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) {obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }

obj.visibility=v; }


if (!IsIE6())
    {
        //Just display the div
        //divPopup.style.visibility ="visible";
        return;
    }
    obj=MM_findObj(args[0]);

    //Increase default zIndex of div by 1, so that DIV appears before IFrame
    obj.zIndex=obj.zIndex+1;

    var iFrame = document.createElement("IFRAME");
    iFrame.setAttribute("src", "javascript:''");
    iFrame.frameBorder="0";
    iFrame.style.filter = "alpha(opacity=0)";
	iFrame.style.opacity = "0.80";
	//iFrame.style.-moz-opacity = "0.80";

    //Match IFrame position with divPopup
    
    iFrame.style.position="absolute";
    var objXY = getCoords(obj);
    iFrame.style.left = objXY.x + 'px';
    iFrame.style.top = objXY.y + 'px';
    iFrame.style.width =obj.offsetWidth + 'px';
    iFrame.style.height =obj.offsetHeight + 'px';

    document.body.appendChild(iFrame);

    //Store iFrame in global variable, so it can get removed when divPopup is hidden 
    g_PopupIFrame=iFrame;
    obj.visibility ="visible";
	

}

function MM_showHideLayers1() { //v3.0

var i,p,v,obj,args=MM_showHideLayers1.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) {obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }

obj.visibility=v; }

if (!IsIE6())
    {
        //Just display the div
        //divPopup.style.visibility ="visible";
        //ddMenu('one',1);
        return;
    }
    obj=MM_findObj(args[0]);

    //Increase default zIndex of div by 1, so that DIV appears before IFrame
    obj.zIndex=obj.zIndex+1;

    var iFrame = document.createElement("IFRAME");
    iFrame.setAttribute("src", "javascript:''");
    iFrame.frameBorder="0";
    iFrame.style.filter = "alpha(opacity=0)";
	iFrame.style.opacity = "0.80";
	//iFrame.style.-moz-opacity = "0.80";

    //Match IFrame position with divPopup
    
    iFrame.style.position="absolute";
    var objXY = getCoords(obj);
    iFrame.style.left = objXY.x + 'px';
    iFrame.style.top = objXY.y + 'px';
    iFrame.style.width =obj.offsetWidth + 'px';
    iFrame.style.height =obj.offsetHeight + 'px';

    document.body.appendChild(iFrame);

    //Store iFrame in global variable, so it can get removed when divPopup is hidden 
    g_PopupIFrame=iFrame;
    obj.visibility ="visible";
	

}


/* Quick Shop Functions */

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    //return y;
    return [x, y];
}

 function launchQuickLook(oid,index,cid) {
    var e=window.event;
    var frmQL = document.getElementById("fraQuickLook");
    if (frmQL) frmQL.src="/product_quickshop.aspx?p=" + oid + "&c=" + cid;
    var divQS = document.getElementById("quickShop");
    var dh = getScrollXY();
    divQS.style.top=(dh[1] + 100) + 'px';
    divQS.style.display = 'block';
}

 
 function closeQuickLook() {
    var frmQL = document.getElementById("fraQuickLook");
    if (frmQL) frmQL.src="about:blank";
    var divQS = document.getElementById("quickShop");
    divQS.style.display = 'none';
}

 function closeQuickCart() {
    /*var frmQL = document.getElementById("fraQuickCart");
    if (frmQL) { 
        frmQL.style.display = 'none';
        //frmQL.src="about:blank";
    }*/
    var divQS = document.getElementById("cartDrop");
    divQS.style.display = 'none';
    var b = document.getElementById('bagHolder');
    if (b) {b.style.display = 'none';}
    /*
    divQS.style.opacity = 1;
    divQS.style.filter = 'alpha(opacity=0)';
    divQS.style.height = 0 + 'px';
    divQS.style.display='none';
    divQS.maxh = null;
    */
}

var mHeight = 0;
var moffSetHeight = 0;

function Resize_iFrame1(strFrameName,strFrameBody) { 

    fn = strFrameName?strFrameName:"fraContent";
    fb = strFrameBody?strFrameBody:"FrameBody";
    x = document.getElementById(fn); 
    y = x.contentWindow.document.getElementById(fb); 
    if (y) {
        //if (document.all) // IE problem; wont show clientHeight if style height hasn't been set 
        //    y.style.height = "1px"; 
        margin = 35; 
        x.style.height = parseInt(y.scrollHeight + margin) + "px"; 
    }
   
     c.style.display = 'block';
     c.style.height = parseInt(x.style.height)+ "px";
     windonw.status = 'c.style.height=' + mHeight;
     
     mHeight = parseInt(x.style.height);
     
 }
function cancelClick() {return false;}
function enableClick() {return true;}

function Resize_iFrame(strFrameName,strFrameBody) { 

    fn = strFrameName?strFrameName:"fraContent";
    fb = strFrameBody?strFrameBody:"FrameBody";
    x = document.getElementById(fn); 
    y = x.contentWindow.document.getElementById(fb); 
    if (y) {
        //if (document.all) // IE problem; wont show clientHeight if style height hasn't been set 
        //    y.style.height = "1px"; 
        margin = (strFrameName=='cartDrop'?2:35); 
        x.style.height = parseInt(y.scrollHeight + margin) + "px"; 
        x.height = x.style.height;
    }
}
var clearCart = false;
function test() {
    Resize_iFrame('cartDrop');
    var c = document.getElementById('cartDrop');
    //alert("lc="+c.height);
    //alert("lsc="+c.style.height);
    window.status = '         ';//'c.style.height=' + c.style.height;
    if (c.style.height) { 
        if (parseInt(c.style.height) > 100) { 
            ddMenu('one',1,c);
            if (clearCart) {setTimeout(function(){ddMenu('one',-2)},8500);clearCart=false;}
        }
    }
}

function launchQuickCart(o) {
    var c = document.getElementById('cartDrop');
    if (c) {
        c.style.display = 'block';
        c.src="/basket_mini.aspx";
        return;
    }
}

function launchAddCart(oid,qty,cid) {
    closeQuickCart();
    var c = document.getElementById('cartDrop');
    var b = document.getElementById('bagHolder');
    if (c) {
        if (b) {b.style.display = 'block';}
        c.style.display = 'block';
        c.height = 200;
        c.style.height = 200;
        c.src="/basket_add.aspx?OID=" + oid + "&QTY="+qty+"&cid="+cid;
        clearCart = true;
        return;
    }
}

function UpdateCartTotals(amt,qty) {
    var q = document.getElementById('dvTotalItems');
    var a = document.getElementById('dvTotalCost'); 
    if (q && a) {
        q.innerHTML = qty + ' Items';
        a.innerHTML = '$' + amt + ' Subtotal';
        return;
    }
    //Halston section
    var h = document.getElementById('dvHTotalItems');
    if (h) {h.innerHTML = '(' + qty + ') view bag';}
}

var DDSPEED = 5;
var DDTIMER = 10;
var DDCOUNT = 1;
var c_maxh = 0;

// main function to handle the mouse events //
function ddMenu(id,d,o){
 
  var h = document.getElementById('cartDrop');
  var c = document.getElementById('cartDrop');
  clearInterval(c.timer);
  
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){
        return
    } else if(!c.maxh){
      
      c.style.display = 'block';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    if (!clearCart) {
        h.timer = setTimeout(function(){ddCollapse(c)},(d==-1?3000:10));
    }
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById('cartDrop');
  var c = document.getElementById('cartDrop');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  //alert('Math.round((c.maxh - currh) / DDSPEED)=' + c.maxh + '-' + currh + '/' + DDSPEED);
  
  if(d == 1){
    if ((Math.round(currh/c.maxh))*100 > 85) {currh=c.maxh};
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  //c.style.opacity = Math.round(currh / c.maxh);
  //c.style.filter = 'alpha(opacity=' + Math.round(currh * 100 / c.maxh) + ')';
  c.style.opacity = 100;
  c.style.filter = 'alpha(opacity=100)';
  if((currh < 40 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
    
       if (d != 1){
        //window.status = 'clearing...';
        c.style.opacity = 1;
        c.style.filter = 'alpha(opacity=0)';
        c.style.height = 0 + 'px';
        c.style.display='none';
        c.maxh = null;
        var a = document.getElementById("btnAddToCart");
        if (a) {a.onclick = enableClick;}
        //
    }
  }
}


