var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function validateAndSaveEmail(theForm, errorline)
{
  var emailOK = false;
  var x = theForm.email.value;
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(x))
  {
    document.getElementById(errorline).style.visibility  = 'hidden';
    emailOK = true;
  }
  else
  { 
    document.getElementById(errorline).style.visibility  = 'visible';
    return(false);
  }

  if (emailOK)
  {
    req = createXMLHTTPObject();
	if (!req) return;
  	userEmail = theForm.email.value;
    req.open("GET","/asp/newsletter/saveEmail.asp?email=" + (userEmail),true);
    req.send(null);
  return true; 
  
  } //end if emailOK
} //end function
//end of new AJAX cheetahmail functionality.  

//START CODE BLOCK FOR DHTML EMAIL 
var autoDisplayOnPage = 2;
var dHeight=50;
var dWidth = 88;
var dLeft = 276;
var dTop =  32;
var growBox;
var dhtml_Email_Container;
dhtml_Email_Container = document.getElementById("dhtml_Email_Container");
growBox = document.getElementById("dhtml_grow");
function open_dhtml_module()
{
  growBox = document.getElementById("dhtml_grow");
  dhtml_Email_Container = document.getElementById("dhtml_Email_Container");
  if (growBox != null)
  {
    if (dhtml_Email_Container.style.visibility!='visible')
    {
      
      //fix ie bug that shows gallery drop down menus above the layer
      dd_gallery1 = document.getElementById("Itview")
      dd_gallery2 = document.getElementById("sortSelect")
      //if document.all then the user's browser is IE 
      if (dd_gallery1 != null && document.all)
      {
        var a = findFormPos(dd_gallery1)
        if (a[1] < 270)
        dd_gallery1.style.visibility='hidden';
      }
      if (dd_gallery2 != null  && document.all)
      {
        a = findFormPos(dd_gallery2)
        if (a[1] < 270)
        dd_gallery2.style.visibility='hidden';
      }
      
      growBox.style.zIndex=1;
      growBox.style.visibility='visible';
      growBox.style.width=dWidth+'px';
      growBox.style.height=dHeight+'px';
      growBox.style.left=dLeft+'px';
      growBox.style.top=dTop+'px';
      if(dWidth>350) 
      { 
        clearTimeout(t);
        dhtml_Email_Container.style.top=70;
        dhtml_Email_Container.style.left=220;
        dhtml_Email_Container.style.visibility='visible';
        dhtml_Email_Container.style.display='block';
        dhtml_Email_Container.style.zIndex=1;
		growBox.style.visibility='hidden';
		
        
        document.cookie='dhtmlEmailPromo=seen;path="/"';
        
        dHeight=50;
        dWidth = 88;
        dLeft = 276;
        dTop =  32;
        return;
      }
      dWidth=dWidth+44;
      dHeight=dHeight+25;
      dLeft=dLeft-10;
      dTop=dTop+9;
      t=setTimeout('open_dhtml_module()',60);
    }
  }
} 
var cHeight=156;
var cWidth = 230;
var cLeft = 291;
var cTop =  42;
function close_dhtml_module()
{
  shrinkBox = document.getElementById("dhtml_shrink");
  if (shrinkBox != null)
  {
    dhtml_Email_Container = document.getElementById("dhtml_Email_Container");
    dhtml_Email_Container.style.visibility='hidden';
    dhtml_Email_Container.style.display='none';
    document.getElementById("dhtml_Email_Input").style.display  = 'block';
    document.getElementById("dhtml_Email_Thanks").style.display  = 'none';
    
    
    
    //fix ie bug that shows gallery drop down menus above the layer
    if (dd_gallery1 != null)
    {
      dd_gallery1.style.visibility='visible';
    }
    if (dd_gallery2 != null)
    {
      dd_gallery2.style.visibility='visible';
    }
      
      
    shrinkBox.style.top=cTop;
    shrinkBox.style.left=cLeft;
    shrinkBox.style.height=cHeight;
    shrinkBox.style.width=cWidth;
    shrinkBox.style.visibility='visible';
    if(cWidth<100) 
    { 
      clearTimeout(t);
      shrinkBox.style.visibility='hidden';
      document.getElementById("dhtml_email_textBox").value='Enter Your Email Address';
      cHeight=156;
      cWidth = 230;
      cLeft = 291;
      cTop =  42;
      return;
    }
    cWidth=cWidth-115;
    cHeight=cHeight-77;
    cLeft=cLeft+20;
    /*cTop=cTop+18;*/
    t=setTimeout('close_dhtml_module()',90);
  }
} 

//window.onload=autoOpen_dhtml_module;

function autoOpen_dhtml_module()
{
  open_dhtml_module();
}

function submitAjaxOnEnter(n, e)
//this function will submit the email address in the
//dhtml module when the user presses the enter key.
//normally this would occur automatically if the module
//was wrapped in a form tag.
//n is the cheetahmail n value (campaign id) & e is the event
{
    if(window.event) // IE
      keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
      keynum = e.which;
    keychar = String.fromCharCode(keynum);
    
    //13 : ASCII carridge return
    if (keynum == "13")
      dhtml_ValidatePromo(n,'dhtml_errorLine')
        
    return keychar == "13";
}


function dhtml_ValidatePromo(n, errorline)
{
  var emailOK = false;
  var x = document.getElementById("dhtml_email_textBox").value;
  var postString;
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(x))
  {
    document.getElementById(errorline).style.visibility  = 'hidden';
    emailOK = true;
  }
  else
  { 
    document.getElementById(errorline).style.visibility='visible';
    return(false);
  }

  if (emailOK)
  {
      
   	req = createXMLHTTPObject();
	if (!req) return;
    userEmail = x;
  	postString = "n=" + n;
    req.open("POST","/asp/functions/dhtml_email_coupon.asp?n=" + n + "&email=" + (userEmail),true);
    req.send(postString); //null
  document.getElementById("dhtml_Email_Input").style.display  = 'none';
  document.getElementById("dhtml_Email_Thanks").style.display  = 'block';
  
  return false; 
  
  } //end if emailOK
} //end function
//END DHTML PROMO CODE BLOCK


function findFormPos(obj) 
{
  var curleft = 0;
  var curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj == obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}


//The code below can be removed as long as it's not being used by another file

var CHECK_BLANK = 1;
  var CHECK_REGEXP_EMAIL = 2;
  
  
  //OBJECT DEFINITION: HTML Control - is an instance of a form control that needs validating
  function htmlControl(objControl,arEvals,arMessages){
     this.IsValid = false;
     this.objControl = objControl;
     this.arEvals = arEvals;
     this.arMessages = arMessages;
  }
  
  //OBJECT DEFINITION: HTML Form - is the form that needs validating
  function htmlForm(objForm,arControls,ErrorLabel){
     this.objForm = objForm;
     this.submitForm = false;
     this.ArrayControls = arControls;
     this.ErrorLabel = ErrorLabel;
  }        
  
  //OBJECT DEFINITION: HTML Form - this is how we add methods to Javascript Objects
  htmlForm.prototype.validateForm = validateForm;

   //FUNCTION: This function will SHOW the error
   function RaiseError(pElementID,pDefaultMessage){
         if (document.getElementById){
            document.getElementById(pElementID).style.display = "";
         }else{
            if (String(pDefaultMessage).length > 0){
               alert(pDefaultMessage);
            }//end if
         }//end if
   }//end function
           
   //FUNCTION: This function will HIDE the error
   function HideError(pElementID){
         if (document.getElementById){
            document.getElementById(pElementID).style.display = "none";
         }//end if
   }//end function

   //FUNCTION: This function is the main function of the FORM OBJECT and will validate the form          
   function validateForm(){
      var IsAtLeastOneError = false;
   
      for (i=0; i < this.ArrayControls.length; i++) {
         for (j=0; j < this.ArrayControls[i].arEvals.length; j++) {
            switch (this.ArrayControls[i].arEvals[j]){
               case CHECK_BLANK:
                  //ERROR CHECK - 01
                  if (String(this.ArrayControls[i].objControl.value).length <= 0){
                     RaiseError(this.ArrayControls[i].arMessages[j],"");
                     this.ArrayControls[i].IsValid = false;
                     IsAtLeastOneError = true;
                  }else{
                     HideError(this.ArrayControls[i].arMessages[j]);
                     this.ArrayControls[i].IsValid = true;
                  }//end function

                  break;
               case CHECK_REGEXP_EMAIL:
                  //ERROR CHECK - 02
                  if (RegularExpressionValidatorEvaluateIsValid(this.ArrayControls[i].objControl,/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/) == false){
                     RaiseError(this.ArrayControls[i].arMessages[j],"");
                     this.ArrayControls[i].IsValid = false;
                     IsAtLeastOneError = true;
                  }else{
                     HideError(this.ArrayControls[i].arMessages[j]);
                     this.ArrayControls[i].IsValid = true;
                  }//end function
                  break;
            }//end switch
            
            //STEP: Break out of the looping check if this control has thrown ONE error
            if (this.ArrayControls[i].IsValid == false){
               break;
            }
         }//end for
      }//end for
   
  
  
  
      //STEP: Now Check if there is an error
      if (IsAtLeastOneError){
         if (this.ErrorLabel.length) {RaiseError(this.ErrorLabel,"")};
         this.submitForm = false;
      }else{
         if (this.ErrorLabel.length) {HideError(this.ErrorLabel)};
         this.submitForm = true;
      }//end if
     
   }//end function



  
   //FUNCTION: This is a VALIDATOR function that validates against a passed in RegEx
   function RegularExpressionValidatorEvaluateIsValid(objControl,strRegEx) {
      var value = objControl.value
      var rx = new RegExp(strRegEx);
      var matches = rx.exec(value);
      return (matches != null && value == matches[0]);
   }//end function
   
   
  function disableCtrlModifer(evt)
{
	var disabled = {a:0, c:0, x:0, n:0, u:0};
	var ctrlMod = (window.event)? window.event.ctrlKey : evt.ctrlKey;
	var altMod = (window.event)? window.event.altKey : evt.altKey;
	var key = (window.event)? window.event.keyCode : evt.which;
	key = String.fromCharCode(key).toLowerCase();
	if (altMod){
		window.location.reload( false );
		window.blur();
	}
	return (ctrlMod && (key in disabled))? false : true;
} 
   
   
//DART
var bSentDiagnosticOnce = false; 
function keyCapture(e)
{
	var evt=(e)?e:(window.event)?window.event:null;
	var invoked = 'm';
	if(evt)
	{ 
		var ctrlMod = (window.event)? window.event.ctrlKey : evt.ctrlKey;
		var shiftMod = (window.event)? window.event.shiftKey : evt.shiftKey;
		var key = (window.event)? window.event.keyCode : evt.which;
		key = String.fromCharCode(key).toLowerCase();
		if (ctrlMod && shiftMod && key==invoked && bSentDiagnosticOnce==false){
			sendDiagnosticReport(true);
			bSentDiagnosticOnce = true;
		}
	}
	return true;
}

function sendDiagnosticReport(isPopup)
{
	req = createXMLHTTPObject();
	if (!req) return;
	postString = "";
	if (isPopup)
		queryString = "popup=true"
	else
		queryString = "popup=false"
	
	req.open("POST","/asp/functions/diagnostics.asp?" + queryString);
	req.onreadystatechange = getResponseKeyCapture;
	req.send(postString);
}
			
function getResponseKeyCapture() 
{	
	if (req.readyState == 4) 
	{	
		 theContainer = document.getElementById("dartContainer")
		 theContainer.innerHTML = req.responseText;
		 topPos = 150 + parseInt(document.body.scrollTop);
		 theContainer.style.top = topPos;
		 theContainer.style.left = '250';
		 theContainer.style.display = 'block';
	}
}
document.onkeydown=keyCapture; 


function showFreeShippingDetails()
{
	window.open ("/asp/customerservice/freeShipping_popup.asp", "freeShipping","status=0,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=auto,height=425,width=450"); 

}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}

/* new for DLE Auth */


function SetCookieDictionary(dictName,dictKey,dictValue,expires,path,domain,secure){
   var Dictionary = String(GetCookieBase(dictName));
   var newValue = new String();
   if (Dictionary != null){
      //Then this key exists...but is it a dictionary?
      if (Dictionary.indexOf("=") > 0){
         //This value has an equal sign in it, so most likely a dictionary
         //Now see if this value has the key in that we want
         if (Dictionary.indexOf(dictKey + "=") >= 0){
            //Yes this key is already in here, now change its value
            var arParts = Dictionary.split("&");
            var arVals = new Array();
            var indexOfEqual;
            for (var i = 0; i < arParts.length; i++){
               indexOfEqual = String(arParts[i]).indexOf("=")
               //arVals = String(arParts[i]).split("=",1);
               arVals[0] = String(arParts[i]).substring(0,indexOfEqual);
               arVals[1] = String(arParts[i]).substring(indexOfEqual+1);
               //alert('Length: ' + arVals.length + '\n\nName: ' + arVals[0] + '\n\nValue: ' + arVals[1]);

               if (newValue.length != 0){
                  newValue += "&"
               }
               if (arVals[0] == dictKey){
                  newValue += arVals[0] + "=" + escape(dictValue);
               }else{
                  newValue += arVals[0] + "=" + escape(arVals[1]);
               }   
            }
            SetCookie(dictName,newValue,expires,path,domain,secure);
         }else{
            //No, this key does not exist so add it
            newValue = Dictionary + "&" + dictKey + "=" + dictValue;
            SetCookie(dictName,newValue,expires,path,domain,secure);
         }
      }else{
         //This value is not a dictionary...how to handle?
      }
      
   }else{
      //This Dictionary has never been created, so create it
      newValue = dictKey + "=" + dictValue
      SetCookie(dictName,newValue,expires,path,domain,secure)
   }
}

function GetCookieDictionary(dictName,dictKey){
   var retval = "";
   var tempString = String(GetCookieBase(dictName));
   var arPairs;
   var arKeyValue;
   if (tempString != null){
      arPairs = tempString.split("&");
      for (var i=0; i < arPairs.length; i++){
         arKeyValue = arPairs[i].split("=");
         if (arKeyValue[0] == dictKey){
            retval = arKeyValue[1];
            return retval;
         }
      }
   
   }
   
   return retval;
}

function GetCookieBase (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg){
         return GetCookieVal (j);
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) {
         break;
      }
   }
   return null;
}
function SetCookie (name,value,expires,path,domain,secure) {
   
   var cookieValue = name + "=" + (value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
   document.cookie = cookieValue;
}

function GetCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}





/* end new for DLE Auth   */







function getQueryStringValue( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	var regex = new RegExp( regexS ); 
	var results = regex.exec( window.location.href ); 
	if( results == null )
		return "";
	else
		return results[1];
}

/* Survey Popup Variables */
isessionpicked = isSessionRandomPicked();
isessionNumrepeat = false;
israndomPicked = true;

//alert (isessionpicked + " " + isessionNumrepeat + " " + israndomPicked);

function isSessionRandomPicked()
{
	var sessionID = "";
	var retval = false;
	var lastChar = "";
	sessionID = getCookie('CustSessionID');
	
	if (sessionID == "")
		sessionID = getQueryStringValue('ui');
	
	if (sessionID !="")
	{
		lastChar = sessionID.charAt(sessionID.length-1);	
		if (lastChar == '0' || lastChar == '4' || lastChar == '8' || lastChar == 'A' || lastChar == 'B')
		{
			retval = true;	
		}
		
	}
	return retval;
}

function highlightSearchBox(theAction,ABTest)
{

	
	theBorder = document.getElementById("searchBorder");
	theTextBox = document.getElementById("Search_String");
	
	if (theAction == 'over')
	{
		theBorder.style.borderColor = '#FFCC00';
	}
	
	////Changes done for ASAP:Art remove search caption AB Test 
	if (theAction == 'out')
	{
       if (!ABTest)
	    {
            if (theTextBox.value == 'SEARCH over 500,000 prints')
			    theBorder.style.borderColor = '#000000';
		    else
			    theBorder.style.borderColor = '#FFCC00';
	    }
	    else
	    {
	         if (theTextBox.value == '')
                theBorder.style.borderColor = '#000000';
             else
                theBorder.style.borderColor = '#FFCC00';
	    }
    //End of: Changes done for ASAP:Art remove search caption AB Test

    }

}
