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];}
}

//------------------------------------------------------------------------------------------------//
// MAIN FUNCTIONS SECTION                                                                         //
//------------------------------------------------------------------------------------------------//
function GetMainFormName()
{
	var i;
	for (i=0;i<document.forms.length;i++)
	{
		// Framework v1.0.3705
		if (document.forms[i].name.indexOf("_ServerForm") != -1)
			return document.forms[i].name;
		// Framework v1.1.4322
		if (document.forms[i].name.indexOf("__aspnetForm") != -1)
			return document.forms[i].name;
		// Framework v2.0.50727
		if (document.forms[i].name.indexOf("aspnetForm") != -1)
		    return document.forms[i].name;	
	}
	return null;
}

function gosearch()
{
	var mf = GetMainFormName();		
	if (mf==null) return;
	var searchVal = document.forms[mf].elements["SearchKeywords"].value;	
	searchVal = searchVal.toLowerCase();
	location.href = "/search/results.aspx?SearchKeywords=" + searchVal;
}	

function SaveElement(ElementObject,ElementValue)
{
	var mf = GetMainFormName();					
	if (mf==null) return;
	var element  = GetElement(ElementObject,mf);
	if (element!=null){ 
		element.value = ElementValue;
	}
}

function GetElementName(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].name.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].name.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].name;
		}	
	}
	return null;
}

function GetElementID(n,mf)
{
	if (n==null || n=="") return null;
	n = n.toLowerCase();
	var i, s, j;
	for (i=0;i<document.forms[mf].elements.length;i++)
	{
		if (document.forms[mf].elements[i].id.toLowerCase().lastIndexOf(n) != -1)
		{
			var strform = document.forms[mf].elements[i].id.toLowerCase();
			var strformname = strform.substr(strform.lastIndexOf(n));
			if (strformname == n)
				return document.forms[mf].elements[i].id;
		}	
	}
	return null;
}

function GetElement(ElementName, formName)
{
	return document.forms[formName].elements[GetElementID(ElementName,formName)];
}

//------------------------------------------------------------------------------------------------//
// CONTACT US FUNCTIONS SECTION                                                                   //
//------------------------------------------------------------------------------------------------//
var subjectlist;

//-----------------------------------------------//
// This function add a subject to subjects list. //
//-----------------------------------------------//
function AddSubject(subject, subsubject)
{
	var index;
	var subjectfound = false;
	var currentsubject;
	
	if (!subjectlist)
	{
		// Initialize subject Array;
		 subjectlist = new Array();
	}

	// Search the subject in subjects Array
	for (index = 0; (index < subjectlist.length) && !subjectfound; index++)
	{
		currentsubject = subjectlist[index];
		
		// Test if subject and current subject are defined
		if (subject && currentsubject)
		{
			if (subject.code == currentsubject.code)
			{
				subjectfound = true;
			}
		}
	}

	if (!subjectfound)
	{
		// Add the subject to subjects Array
		subjectlist.push(subject);
		currentsubject = subject;
	}

	// Add the subsubject to the subject
	AddSubSubject(currentsubject, subsubject);
}

//---------------------------------------------//
// This function add a subsubject to a subject //
//---------------------------------------------//
function AddSubSubject(subject, subsubject)
{
	// Test if subject and subsubject are defined
	if (subject && subsubject)
	{
		if (!subject.subsubjects)
		{
			subject.subsubjects = new Array();
		}
		
		// Add subsubject
		subject.subsubjects.push(subsubject);
	}
}

//---------------------------------------------//
// This function fills a select with subjects. //
//---------------------------------------------//
function FillSubject()
{
	var index;
	var optionitem;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subjecthidden = GetElement("mail_subject", 0);
	
	if (subjectselect && subjectlist)
	{
		// Reset subjects
		ResetSelect(subjectselect);
		
		for (index = 0; index < subjectlist.length; index++)
		{
			// Add option item
			optionitem = document.createElement("OPTION");
			optionitem.text = subjectlist[index].text;
			optionitem.value = subjectlist[index].code;
			subjectselect.options[subjectselect.options.length] = optionitem;
			
			if (subjecthidden)
			{
				// Commented out to prevent selection as we're getting multi-selects in a non-multi dropdown.
				// optionitem.selected = (optionitem.value == subjecthidden.value);
			}
		}
	}
}

//------------------------------------------------//
// This function fills a select with subsubjects. //
//------------------------------------------------//
function FillSubSubject()
{
	var optionitem;
	var index;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subsubjectselect = GetElement("mail_subsubject_list", 0);
	var subsubjecthidden = GetElement("mail_subsubject", 0);

	if (subjectselect && subsubjectselect)
	{
		// Reset subsubjects					
		ResetSelect(subsubjectselect);
		
		currentsubject = subjectlist[subjectselect.options.selectedIndex - 1];
		
		if (currentsubject && currentsubject.subsubjects)
		{		
			for (index = 0; index < currentsubject.subsubjects.length; index++)
			{
				// Add option item
				optionitem = document.createElement("OPTION");
				optionitem.text = currentsubject.subsubjects[index].text;
				optionitem.value = currentsubject.subsubjects[index].code;
				subsubjectselect.options[subsubjectselect.options.length] = optionitem;
				
				if (subsubjecthidden)
				{
					// Select the current value
					optionitem.selected = (optionitem.value == subsubjecthidden.value);
				}
			}
		}
	}
}

//------------------------------------------------//
// This function resets a select.                 //
//------------------------------------------------//
function ResetSelect(selectlist)
{
	// Remove options
	while (selectlist.options.length != 1)
	{
		selectlist.options[1] = null;
	}
}

//------------------------------------------------//
// This function gets a value from a list using   //
// its index and return the default value if      //
// index is out of range or list is null.         //
//------------------------------------------------//
function GetListValue(list, index, defaultvalue)
{
	if (list && list[index])
	{
		return list[index];
	}
	else
	{
		return defaultvalue;
	}
}	

//------------------------------------------------//
// This function sets mailto and subject values   //
// to hidden fields. It also rebuild subsubjects  //
// list.                                          //
//------------------------------------------------//
function OnSubjectListChange()
{
	var subsubject;
	var subjectselect = GetElement("mail_subject_list", 0);
	var subject = GetListValue(subjectlist, subjectselect.selectedIndex - 1, null);    
	    
	if (subject)
	{
		// Get the first subsubject for current subject
		subsubject = subject.subsubjects[0];
		
		SaveElement('mail_subject', subject.text);
		SaveElement('mail_subject_code', subject.code);
		
		if (subsubject){
			SaveElement('mail_to', subsubject.emailto);
			SaveElement('mail_bcc', subsubject.emailbcc);
		}
	}
	else
	{
		SaveElement('mail_subject', '');
		SaveElement('mail_subject_code', '');
		SaveElement('mail_to', '');
		SaveElement('mail_bcc', '');
	}
	
	if(GetElement("mail_subsubject_list", 0))
	{
		FillSubSubject();
	}
}

//------------------------------------------------//
// This function sets mailto and subject values   //
// to hidden fields. It also rebuild subsubjects  //
// list.                                          //
//------------------------------------------------//
function OnSubSubjectListChange()
{
	var subjectselect;
	var subsubjectselect;
	var subject;
	var subsubject;
	
	subjectselect = GetElement("mail_subject_list", 0);
	subsubjectselect = GetElement("mail_subsubject_list", 0);
	subject = GetListValue(subjectlist, subjectselect.selectedIndex - 1, null);
	
	if (subject)
	{
		subsubject = GetListValue(subject.subsubjects, subsubjectselect.selectedIndex - 1, null);
		
		if (subsubject)
		{
			SaveElement('mail_subsubject', subsubject.text);
			SaveElement('mail_subsubject_code', subsubject.code);
			SaveElement('mail_to', subsubject.emailto);
			SaveElement('mail_bcc', subsubject.emailbcc);
		}
		else
		{
			SaveElement('mail_subsubject', '');
			SaveElement('mail_subsubject_code', '');
			SaveElement('mail_to', '');
			SaveElement('mail_bcc', '');
		}
	}
}

//------------------------------------------------//
// Forum functions								  //
//------------------------------------------------//
	function afficheEtoiles(nb)
	{
		for(i=0;i<nb && nb<=5;i++)
			document.write("<img src=\"/img/img_ref/common/forum/pict_etoile.gif\" border=\"0\">");
	}

	var OpenedDiv = "";
	
	function DisplayPost(divname)
	{
		if(OpenedDiv != "")
			document.getElementById(OpenedDiv).style.display="none";

		if(OpenedDiv != divname)
		{
			document.getElementById(divname).style.display="block";
			OpenedDiv = divname;
		}
		else
			OpenedDiv = "";
	}
	
	function forumsearch() 
	{
		var mf = GetMainFormName();
		if (mf==null) return;
		var sfield = document.forms[mf].searchfield.value;
		var criteria = document.forms[mf].selectsearch.selectedIndex;
		if (criteria=="0")
		{
			alert("no critaria");
			return;
		}
		if (sfield=="")
		{
			alert("no field");
			return;
		}
			else window.location ="search.aspx?searchid="+ criteria +"&search=" + sfield
	}

var newWindow;

function openPopup(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 2;
	var wint = (screen.height - popupHeight) / 2;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}

function openPopup2(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 2;
	var wint = (screen.height - popupHeight) / 4;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}

function openPopupWithScrollbars(url, popupWidth, popupHeight) {
	if (newWindow && !newWindow.closed)	{
		newWindow.close();
	}	
		
	var winl = (screen.width - popupWidth) / 2;
	var wint = (screen.height - popupHeight) / 2;
		
	newWindow = open(url, 'popup', ("toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,top=" + wint + ",left=" + winl + ",width=" + popupWidth + ",height=" + popupHeight + "\""));
	newWindow.focus();
}

function NavigateTo(_url) {
	if (_url!="") document.location.href= _url;
}

//---------------------------------------------------------//
// Reset Errors: Resets all Error Messages on Page
//---------------------------------------------------------//
function ResetErrors() 
{    
    var objZipError = document.getElementById('spanZipError');
    if(objZipError != null)
	    objZipError.style.display = "none";
	             
    if(document.getElementById("PwordMatch") != null)
        document.getElementById("PwordMatch").style.display = 'none';
    if(document.getElementById("PwordLength") != null)
        document.getElementById("PwordLength").style.display = 'none';
        
    var objPhoneError = document.getElementById('spanPhoneError');
    if(objPhoneError != null)
        objPhoneError.style.display = "none";
    
    if(document.getElementById("StateError") != null)
        document.getElementById("StateError").style.display = 'none';
}  

//---------------------------------------------------------//
// Validate zipcode: Zip should have 5 numeric characters  //
//---------------------------------------------------------//
function ValidateZip(zipTextbox)
{		
	//Check if zip code length is 5
	if(zipTextbox.value.length != 5)
	{	    
		return false;
	}				
	else			
	{
		//If zipcode length is 5, only allow 0-9 to be entered as the characters
		var checkOK = "0123456789";
		var allValid = true;
		for (var i=0; i < zipTextbox.value.length; i++) 
		{ 
			ch = zipTextbox.value.charAt(i);
			
			for (var j = 0;  j < checkOK.length;  j++)
			{							
				if (ch == checkOK.charAt(j))
					break;							
			}
			
			if (j == checkOK.length)
			{							
				allValid = false;
				break;
			}
		}
		if (!allValid) 
		{
			return false;
		}	
		else
		{
			return true;
		}						
	}
}

//---------------------------------------------------------//
// Validate password: Passwords should match and must have 6 characters//
//---------------------------------------------------------//
function ValidatePassword() 
{
    var mf = GetMainFormName();
    
    var objPwd1 = GetElementID('password', mf);
    var objPwd2 = GetElementID('password_conf', mf);
    
    var strPwd1 = document.getElementById(objPwd1).value;
    var strPwd2 = document.getElementById(objPwd2).value;
    
    document.getElementById("PwordMatch").style.display = 'none';
    document.getElementById("PwordLength").style.display = 'none';
    
    if (strPwd1 != strPwd2) {
      document.getElementById("PwordMatch").style.display = 'inline';
      return false;
    }
    
    if (strPwd1.length < 6) {
      document.getElementById("PwordLength").style.display = 'inline';
      return false;
    }
    
    return true;
}     

//---------------------------------------------------------//
// Validate phone: Phone should be xxx-xxx-xxxx format where x=digit//
//---------------------------------------------------------//
function ValidatePhone()
{
    var mf = GetMainFormName();				
    var phoneTextboxRef =  GetElementName("phone", mf);
    var phoneTextbox = document.forms[mf].elements[phoneTextboxRef];
    var testPhone=/^[0-9]{3}\-{1}[0-9]{3}\-{1}[0-9]{4}/;
    // Intuitively replace spaces with dashes.
    phoneTextbox.value = phoneTextbox.value.toString().replace(/\ /gi, '-');
    if(testPhone.test(phoneTextbox.value))
      {
          var obj=document.getElementById('spanPhoneError');
          obj.style.display = "none";
          return true;
      }
      else
      {
          var obj=document.getElementById('spanPhoneError');
          obj.style.display = "block";
          return false;
      }
} 

//---------------------------------------------------------//
// Validate State: First option should not be selected.
//---------------------------------------------------------//
function ValidateState() 
{
    var mf = GetMainFormName();
    
    var objState = GetElementID('address_state_choice', mf);
        
    var stateIndex = document.getElementById(objState).selectedIndex;
        
    if (stateIndex == 0) {
      document.getElementById("StateError").style.display = 'inline';
      return false;
    }
    
    return true;
}     

//Add To Favorites
function AddToFavorites(prd)
{		
    //For some weird reason the PRD CODE needs to be passed twice in the URL...
    //When we pass it just as "?productcode"...the TpFavoritUpdate doesn't seem to be able to read it.
    //So we are passing it as "?productcode=<prd>&productcode=<prd>"
	openPopup("/myshu/popup_addToFavorites.aspx?successurl=%2Fmyshu%2Fpopup_addToFavorites.aspx%3Fproductcode=" + prd + "&productcode=" + prd, 390, 250);
}

//Delete Favorites
function DeleteFavorites(prd)
{	
	openPopup("/myshu/members/popup_deleteFavorites.aspx?productcode=" + prd, 390, 125);
}

//Add To Favorites
function AddToMyShuStyle(topicCode)
{		
    //For some weird reason the TOPIC CODE needs to be passed twice in the URL...
    //When we pass it just as "?topiccode"...the TpFavoritUpdate doesn't seem to be able to read it.
    //So we are passing it as "?topiccode=<prd>&topiccode=<prd>"
	openPopup("/myshu/popup_addToMyShuStyle.aspx?successurl=%2Fmyshu%2Fpopup_addToMyShuStyle.aspx%3Ftopiccode=" + topicCode + "&topiccode=" + topicCode, 390, 250);
}

//Delete Favorites
function DeleteMyShuStyle(topicCode)
{	
	openPopup("/myshu/members/popup_deleteMyShuStyle.aspx?topiccode=" + topicCode, 390, 125);
}


//Send To A Friend
function SendToFriend(prd)
{	
	openPopup("/myshu/popup_product_tellafriend.aspx?productcode=" + prd, 415, 500);
}

// Checkout Recommended Products Display & Navigation
function BuildElement_Recommendations(strDisplayName, strPSKU, strVSKU, strProdURL, IsBackOrdered, BackOrder_Max, ProdStock, BackOrder_Date ) {
  var strElement = '';
  
  strElement += '\n<div style="width:150px;height:150px" class="float_L" id="' + strPSKU + '_item">';
  strElement += '\n <input type="hidden" name="URL_' + strVSKU + '" id="URL_' + strVSKU + '" value="/' + strProdURL + '" />';
  strElement += '\n <div style="width:150px;height:100px;"><a href="javascript:InvokeQS_BackOrder(\'' + strPSKU + '\',false,false,\'\',\'' + IsBackOrdered + '\',\'' + BackOrder_Max + '\',\'' + ProdStock + '\',\'' + BackOrder_Date + '\');"><img src="/img/product/65x90/' + strPSKU + '.jpg" border="0" class="reflect rheight33 ropacity33" /></a></div>';
  strElement += '\n <div style="margin-top:0px" class="padding25_R LH_12">';
  strElement += '\n  <a href="javascript:InvokeQS_BackOrder(\'' + strPSKU + '\',false,false,\'\',\'' + IsBackOrdered + '\',\'' + BackOrder_Max + '\',\'' + ProdStock + '\',\'' + BackOrder_Date + '\');" class="ALtGREY11_BTT">' + strDisplayName + '</a>';
  strElement += '\n </div>';
  strElement += '\n</div>';
  document.write(strElement);
}

function NavigateTo(strURL) {
  // alert(document.getElementById(strURL).value);
  location.href = document.getElementById(strURL).value;
}

