

function hideShowDiv(index) {
	content = document.getElementById("divContent" + index);
	
	if (content.style.display == "none") {
		content.style.display = "inline";
	} else {
		content.style.display = "none";
	}
}


//checkout functions	
function focusNextField() {}

//this function is set by phone fields for auto-tabbing
function setWait(c,n) {
	 
	c.focus();			//insures focus of the current field
	c.select();			//selects what was previously there for editing
	 
	currValue = c.value;  //previous value, so we can check for it being changed
	currField = c;		   //current field	
	nextField = n;		   //field to jump to
	 
	setTimeout("checkForNum()",100);
}

//auto tab when we have a value of the correct length and different from before
function checkForNum() {  
	if(currField.value.length == 3 && currField.value != currValue) {nextField.focus();}
   
	else{setTimeout("checkForNum()",1);}
	}
	String.prototype.toInt = function() {
    var a = new Array();
    for (var i = 0; i < this.length; i++) {
        a[i] = this.charCodeAt(i);
    }
    return a;
}

function openWindow(winurl,winname,winfeatures){
	newwin = window.open(winurl,winname,winfeatures);
	newwin.focus();
}


// checkout function for copying billing address to shipping address
function copyAddress() { 
	var orderForm = document.billShipForm;		
	
	if(orderForm.billShipFlag.checked == true ) { 
		orderForm.shipFirstName.value = orderForm.billFirstName.value;
		orderForm.shipLastName.value = orderForm.billLastName.value;
		orderForm.shipAddress1.value = orderForm.billAddress1.value;
		orderForm.shipAddress2.value = orderForm.billAddress2.value;
		orderForm.shipCity.value = orderForm.billCity.value;
		orderForm.shipState.value = orderForm.billState.value;
		orderForm.shipZip.value = orderForm.billZip.value;
		//orderForm.ship_to_zip4.value = orderForm.bill_to_zip4.value;
	} else {
		orderForm.shipFirstName.value = '';
		orderForm.shipLastName.value = '';
		orderForm.shipAddress1.value = '';
		orderForm.shipAddress2.value = '';
		orderForm.shipCity.value = '';
		orderForm.shipState.value = '';
		orderForm.shipZip.value = '';
		//orderForm.ship_to_zip4.value = '';
	}
}


	function setCatg(inform,indesc,incode)
	{
		inform.action.value = indesc;
		inform.catg_code.value = incode;
	}
	function getCatg(inform)
	{
		inform.submit();
	}
	function setSubCatg(inform,indesc,incode,insubcode)
	{
		inform.action.value = indesc;
		inform.catg_code.value = incode;
		inform.subcatg_code.value = insubcode;
	}

	function getSubCatg(inform)
	{
		inform.submit();
	}

	function chgDisp(id){
	  if (document.all){
		if(document.all[id].style.display == 'none'){
		  document.all[id].style.display = 'block';
		} else {
		  document.all[id].style.display = 'none';
		}
	  } else if (document.getElementById){
		if(document.getElementById(id).style.display == 'none'){
		  document.getElementById(id).style.display = 'block';
		} else {
		  document.getElementById(id).style.display = 'none';
		}
	  }
	}

	function showDropDown(id)
	{
		if (document.all)
		{
			document.all[id].style.display = 'block';
		} 
		else if (document.getElementById)
		{
			document.getElementById(id).style.display = 'block';
		}
	}

	function hideDropDown(id)
	{
		if (document.all)
		{
			document.all[id].style.display = 'none';
		} 
		else if (document.getElementById)
		{
			document.getElementById(id).style.display = 'none';
		}
	}
	
	function MM_jumpMenu(targ,selObj,restore){ //v3.0
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
	}

	function jump_to_loca(loca)
	{
		document.location=loca;
	}
	function jump_to_place(frm,ctrl)
	{
		eval("document."+frm+"."+ctrl+".focus()");
	}
	function ways_to_shop(selObj,restore)
	{
		eval(selObj.options[selObj.selectedIndex].value);
		if (restore) selObj.selectedIndex=0;
	}

//----------------------------------------------------------------------------
//------------------------- CC Validation ------------------------------------
//----------------------------------------------------------------------------
// Form Field Validation Functions:
//
// isValidExpDate(formField,fieldLabel,required)
//   -- checks for date in the format MM/YY or MM/YYYY against the current date
// isValidCreditCardNumber(formField,ccType,fieldLabel,required)
//   -- checks for valid credit card format using the Luhn check and known digits about various cards
//

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		//formField.focus();
		result = false;
	}
	return result;
}

function allDigits(str) { return inValidCharSet(str,"0123456789"); }

function inValidCharSet(str,charset)
{
	var result = true;
	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

/*function isValidExpDate(formField,fieldLabel,required)
{
	var result = true;
	var formValue = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result && (formField.value.length>0))
 	{
 		var elems = formValue.split("/");

 		result = (elems.length == 2); // should be two components
 		var expired = false;
 		
 		if (result)
 		{

 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[1],10);

 			if (elems[1].length == 2)
 				year += 2000;
 			
 			var now = new Date();
 			
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			
 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
 			
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}
 		
  		if (!result)
 		{
 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
			//formField.focus();
		}
		else if (expired)
		{
 			result = false;
 			alert('The date for "' + fieldLabel +'" has expired.');
			//formField.focus();
		}
	} 
	
	return result;
} */

function isValidExpDate2(formField,fieldLabel,required)
{
	var result = true;
	var formValue = formField;

	var elems = formValue.split("/");
	result = (elems.length == 2); // should be two components
	var expired = false;
	var month = parseInt(elems[0],10);
	var year = parseInt(elems[1],10);
	
	var now = new Date();
 	var nowMonth = now.getMonth() + 1;
 	var nowYear = now.getFullYear();
			
 	if (elems[1].length == 2)
 	year += 2000;
	expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
	result = allDigits(elems[0]) && (month > 0) && (month < 13) && allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));

	if (!result) {
		alert('Please select the Expiration Month & Year on your credit card.');
	} else {
		if (expired) {
			result = false;
			alert('The Expiration date for your credit card has expired.  Please check your expiration date and try again.');
		}
	}
	
	return result;
}

function isValidCreditCardNumber(formField,in_ccType,fieldLabel,required)
{
	var result = true;
 	var in_ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
 
  	if (result && (formField.value.length>0))
 	{ 
 		if (!allDigits(in_ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			//formField.focus();
			result = false;
		}	

		if (result)
 		{ 
 			if (!LuhnCheck(in_ccNum) || !validateCCNum(in_ccType,in_ccNum))
 			{
 				alert('Please enter a valid Credit Card number or type for the "' + fieldLabel +'" field.');
				result = false;
			}	
		} 
	} 
	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}

function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}
	
	return null;
}


function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);
	switch (cardType)
	{
		case "VI":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MC":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DI":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DC":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "none":
			result = false;
			break;
	}
	return result;
}


function getCurrYear()
{
	var currdate = new Date();
	var thisyear = currdate.getFullYear();
	
	return thisyear;
}

	/*
	function UpdateCartItems(html) {
	  var cartitems = document.getElementById('cart_items_aj');
	  cartitems.innerHTML = html;
	}
	
	function UpdateLoggedUser(html) {
	  var loggeduser = document.getElementById('logged_user');
	  loggeduser.innerHTML = html;
	}
	
	function UpdateLoggedStatus(html) {
	  var loggedstatus = document.getElementById('logged_status_aj');
	  loggedstatus.innerHTML = html;
	}
	
	function UpdateRecentlyViewed(html) {
	  var recviewed = document.getElementById('rec_viewed_aj');
	  recviewed.innerHTML = html;
	}
	
	function UpdateCkoutStatus(html) {
		var ckoutstatus = document.getElementById('ckout_status_aj');
		ckoutstatus.innerHTML = html;
	}

	*/
	function checkSNP_TC(inFrm)
	{
		if (this.GetRadioValue(inFrm.payType)=="FC" && inFrm.ppl_agree.checked == false) {
			alert('Please read and accept the Terms and Conditions of the ShopNow Pay Plan.');
			return false;
		}
	}	
/***** hide/show nav functions ****/
function hideShowSearchFilter(navID) {
	arrow = ("navArrow_" + navID);
	filter = document.getElementById("nav_" + navID);			
	
	if (filter.style.display == "none") {
		document[arrow].src = "/images/common/blueArrowDown.gif";
		filter.style.display = "inline"; 
	} else {
		document[arrow].src = "/images/common/blueArrowRight.gif";
		filter.style.display = "none";
	}
}

function open_popup(cURL){
	var pop = window.open(cURL, "hc_terms", "resizable=1,toolbar=1,location=1,directories=0,statusbar=1,menubar=0,scrollbars=1,titlebar=1,width=850,height=715");
	pop.focus();
}

function showCatalogMsg(type) {
	var nNumberText = "Enter Account Number (ex. N030 4240 041)";
	var keyCodeText = "Enter Key Code (ex. 12345678)";
	if (type == 'key') {
		document.all.cqsType.innerHTML = keyCodeText;
		document.all.cqsType.style.visibility = "visible";
	}
	else 
		if (type == 'nNumber') {
			document.all.cqsType.innerHTML = nNumberText;
			document.all.cqsType.style.visibility = "visible";
	} else {
		document.all.cqsType.style.visibility = "hidden";
	}
}

/******************* FS.COM js ************************/

// function to handle JS validation & SNP validation for the order form
function validCCForm(inFrm) {
	var result = true;
	var shipCost = inFrm.shipTotal.value;
	
	if (this.GetRadioValue(inFrm.payType) == "FC" ) {
		
		if (inFrm.ppl_agree.checked == false) {
			alert('Please read and accept the Terms and Conditions of the ShopNow Pay Plan.');
			return false;
		}
		
		if (inFrm.snpPrivacy.checked == false) {
			alert('Please read and accept the Privacy Policy for the ShopNow Pay Plan.');
			return false;
		}
	} else {
		var ccTypeField = inFrm.ccType;
		var ccNumField = inFrm.ccNumber;
		var ccExpField = inFrm.ccMonth.value + '/' + inFrm.ccYear.value;

		//result = isValidExpDate(ccExpField,"Expiration Date",true);
		//result = isValidCreditCardNumber(ccNumField, ccTypeField.value, "Card Number or Card Type", true);
		
		// validate the cc type field
		if (ccTypeField.value == "")	{
			alert('Please select a credit card type');
			ccTypeField.focus();
			result = false;
			return false;
		}
		// validate the cc number field
		if (ccNumField.value == "")	{
			alert('Please enter your credit card number');
			ccNumField.focus();
			result = false;
		} else {
			result = isValidCreditCardNumber(ccNumField, ccTypeField.value, "Credit Card Number", true);
		}
		// if we made it successfully, check the exp date
		if (result) {
			result = isValidExpDate2(ccExpField,"Expiration Date",true);	
		} 
				
		if (result) {
			// if the shipping total is gt 0, send different pop up offer
			if (shipCost > 0) {
				winFreeShipping("https://www.freeshipping.com/join.asp?PID=1188");
			} else {
				winFreeShipping("https://www.freeshipping.com/join.asp?PID5=1188");
			}
			return true;
		} else {
			return false;
		}
	}	
}


window.size = function () {
    var w = 0;
    var h = 0;
    //IE
    if (!window.innerWidth) {
        //strict mode
        if (!(document.documentElement.clientWidth == 0)) {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    }
    //w3c
    else {
        w = window.innerWidth;
        h = window.innerHeight;
    }
    return { width: w, height: h };
}
window.center = function () {
    var hWnd = (arguments[0] != null) ? arguments[0] : { width: 0, height: 0 };
    var _x = 0;
    var _y = 0;
    var offsetX = 0;
    var offsetY = 0;
    //IE
    if (!window.pageYOffset) {
        //strict mode
        if (!(document.documentElement.scrollTop == 0)) {
            offsetY = document.documentElement.scrollTop;
            offsetX = document.documentElement.scrollLeft;
        }
        //quirks mode
        else {
            offsetY = document.body.scrollTop;
            offsetX = document.body.scrollLeft;
        }
    }
    //w3c
    else {
        offsetX = window.pageXOffset;
        offsetY = window.pageYOffset;
    }
    _x = ((this.size().width - hWnd.width) / 2) + offsetX;
    _y = ((this.size().height - hWnd.height) / 2) + offsetY;
    return { x: _x, y: _y };
}

//function to open the free shipping offer window
function winFreeShipping(URL) {
    if (navigator.appName  != 'Microsoft Internet Explorer') {
        var ht = window.innerHeight;
        //var wt = window.innerWidth;
    }
    else {
        var ht = document.body.clientHeight;
        //var wt = document.body.clientWidth;
    }
    var point = window.center({ width: 815, height: 500 });
    window.open(URL,"FreeShipping","top=0,width=815,height=" + ht + ",scrollbars=yes,left=" + point.x + ",resizable=yes,toolbar=yes,location=yes");
}

function getEdpPrice(inJson, inItem, inColor, inSize, inFldEdp,inDivPrice){
	var t_col = "";
	var t_siz = "";
	if (inColor) {	
		t_col = inColor;
	}
	if (inSize) {
		t_siz = inSize;
	}
	inFldEdp.value = inJson["sku_"+inItem]["COLOR_"+t_col]["size_"+t_siz]["edp"];
	inDivPrice.innerHTML = inJson["sku_"+inItem]["COLOR_"+t_col]["size_"+t_siz]["price"];
	if (document.all) {
		document.all[inDivPrice].innerHTML = inJson["sku_"+inItem]["COLOR_"+t_col]["size_"+t_siz]["price"];
	} 
	else if (document.getElementById) {
		document.getElementById(inDivPrice).innerHTML = inJson["sku_"+inItem]["COLOR_"+t_col]["size_"+t_siz]["price"];
	}
}


/**** function flip images on product page *********/
function flipImage(imageName) {
	document.mainImage.src = imageName;
}

function openFSPop(imagePath) {
	window.open(imagePath,"winInfo","width=285,height=435,left=150,top=100,menubar=0,toolbar=0,status=0,titlebar=0,resizable=1");
}
