// #####################################
// # ecMerchant.js
// # ecMerchant(tm)
// # www.ecware.com
// #####################################
// rev = 1.0.0


// #####################################
// page opener
function page(url) {
   window.location = url;
   return true;
}

// #####################################
// child page jumper
function parentJumper(obj, url) {
   parent.location.href = url + obj.value;
}

// #####################################
// parent page jumper
function pageJumper(obj, url) {
   location.href = url + obj.value;   
}

// #####################################
// image swaper
function swapImg(id, newImage) {   
    document.getElementById(id).src = newImage;   
}

// #####################################
// button swapper    
function swapBtn(id, newImage) {   
    document.getElementById(id).src = newImage;   
}

// #####################################
// tooltip window   
function tooltipWindow(Message) {
   if (window.scope1) scope1.close();
   scope = window.open('about:blank','session','resizable=1,scrollbars=1,width=325,height=400,left='+(screen.availWidth-500));
   scope.document.open();
   scope.document.write(Message);
   scope.document.close();
   scope.focus();  
}

// #####################################
// multi-purpose window
function Window(strMode, strMessage) {
   if (window.scope) scope.close();
   if (strMode=='tooltip'){
      scope = window.open('',strMode,'resizable=1,scrollbars=1,width=325,height=400');
   }else if (strMode=='largepicture'){
      scope = window.open('',strMode,'resizable=1,width=300,height=300');
   }else if (strMode=='hugepicture'){
      scope = window.open('',strMode,'resizable=1,width=600,height=600');
   }
   scope.document.open();
   scope.document.write(strMessage);
   scope.document.close();
   scope.focus();   
}

// #####################################
// description expanders
function expander(id, action){
   if(action=='EXPAND'){
      document.getElementById('0_' + id).style.display='none';
      document.getElementById('1_' + id).style.display='';
   }else{
      document.getElementById('0_' + id).style.display='';
      document.getElementById('1_' + id).style.display='none';
   }
   return false;
}
function menuExpander(id, action){
   var elem = document.getElementsByTagName('tr');
   for (var i =0;i < elem.length;i++){
      if(action=='EXPAND'){
          if(elem[i].getAttribute('0_' + id)=='1') elem[i].style.display='none';
          if(elem[i].getAttribute('1_' + id)=='1') elem[i].style.display='';
      }else{
         if(elem[i].getAttribute('0_' + id)=='1') elem[i].style.display='';
         if(elem[i].getAttribute('1_' + id)=='1') elem[i].style.display='none';
      }
   }
}

// #####################################
// hinting
function hint(text) {
   window.status = text;
   //window.setTimeout('unHint()', 300000);
   HINTING = true;
   return true;
}
function unHint() {
   window.status = '';
   HINTING = false;
   return true;
}

// #####################################
// field clearing
function clearField(objFld, fldData) {
   if (objFld.value==fldData)
      objFld.value = '';
   else if (objFld.value == '')
      objFld.value = fldData
   return true;
}

// #####################################
// dynamic form
function formOption(strOption){
   var elem = document.getElementsByTagName('tr');
   
   for (var i =0;i < elem.length;i++){
      if(strOption=='FORM'){
         if (elem[i].getAttribute('form')=='1') elem[i].style.display='';
         if (elem[i].getAttribute('profile')=='1') elem[i].style.display='none';
         if (elem[i].getAttribute('logon')=='1') elem[i].style.display='none';
         if (elem[i].getAttribute('logononly')=='1') elem[i].style.display='none';
      }else if(strOption=='PROFILE'){
         if (elem[i].getAttribute('form')=='1') elem[i].style.display='';
         if (elem[i].getAttribute('profile')=='1') elem[i].style.display='';
         if (elem[i].getAttribute('logon')=='1') elem[i].style.display='';
         if (elem[i].getAttribute('logononly')=='1') elem[i].style.display='none';
      }else if(strOption=='LOGON'){
         if (elem[i].getAttribute('form')=='1') elem[i].style.display='none';
         if (elem[i].getAttribute('profile')=='1') elem[i].style.display='none';
         if (elem[i].getAttribute('logon')=='1') elem[i].style.display='';
         if (elem[i].getAttribute('logononly')=='1') elem[i].style.display='';
      }
   }
   var elem1 = document.getElementsByTagName('td');
   for (var i =0;i < elem1.length;i++){
      if(strOption=='FORM'){
         if (elem1[i].getAttribute('form')=='1') elem1[i].style.display='';
         if (elem1[i].getAttribute('profile')=='1') elem1[i].style.display='none';
         if (elem1[i].getAttribute('logon')=='1') elem1[i].style.display='none';
      }else if(strOption=='PROFILE'){
         if (elem1[i].getAttribute('form')=='1') elem1[i].style.display='none';
         if (elem1[i].getAttribute('profile')=='1') elem1[i].style.display='';
         if (elem1[i].getAttribute('logon')=='1') elem1[i].style.display='none';
      }else if(strOption=='LOGON'){
         if (elem1[i].getAttribute('form')=='1') elem1[i].style.display='none';
         if (elem1[i].getAttribute('profile')=='1') elem1[i].style.display='none';
         if (elem1[i].getAttribute('logon')=='1') elem1[i].style.display='';
      }
   }
   
   var elem2 = document.getElementById('Form');
   if (strOption=='PROFILE'){
      elem2["Mode"].value = 'PROFILE';
      elem2["PreSelector"].value = 'NEW';
   }   
   //var aMsg = '';
   var thisField;
   for (var i =0;i < elem2.length;i++){
      thisField = elem2.elements[i];
      if (thisField.type=='select-one' || thisField.type=='text' || thisField.type=='checkbox' || thisField.type=='password'){          
         if(strOption=='FORM'){
            if (thisField.getAttribute('form')=='1') thisField.disabled = false;
            if (thisField.getAttribute('profile')=='1') thisField.disabled = true;
            if (thisField.getAttribute('logon')=='1') thisField.disabled = true;
            if (thisField.getAttribute('logononly')=='1') thisField.disabled = true;
         }else if(strOption=='PROFILE'){
            if (thisField.getAttribute('form')=='1') thisField.disabled = false;
            if (thisField.getAttribute('profile')=='1') thisField.disabled = false;
            if (thisField.getAttribute('logon')=='1') thisField.disabled = false;
            if (thisField.getAttribute('logononly')=='1') thisField.disabled = true;
         }else if(strOption=='LOGON'){
            if (thisField.getAttribute('form')=='1') thisField.disabled = true;
            if (thisField.getAttribute('profile')=='1') thisField.disabled = true;
            if (thisField.getAttribute('logon')=='1') thisField.disabled = false;
            if (thisField.getAttribute('logononly')=='1') thisField.disabled = false;
         }
         //aMsg = aMsg + thisField.name + ' | ' + thisField.type + ' | ' + thisField.disabled + '\n';       
      }
   }
   //alert(strOption + ' TRIGGERED!!!');
}


// #####################################
// autoload shipping fields
function autoShip(objFld) {
   if (objFld.checked) {
      if (strShowCompany == '1') 
         document.forms.Form.ShipCompany.value = document.forms.Form.CustCompany.value;
      document.forms.Form.ShipFName.value = document.forms.Form.CustFName.value;
      document.forms.Form.ShipLName.value = document.forms.Form.CustLName.value;
      document.forms.Form.ShipAddress.value = document.forms.Form.CustAddress.value;
      document.forms.Form.ShipAddress2.value = document.forms.Form.CustAddress2.value;
      document.forms.Form.ShipCity.value = document.forms.Form.CustCity.value;
      document.forms.Form.ShipRegion.value = document.forms.Form.CustRegion.value;
      document.forms.Form.ShipCountry.value = document.forms.Form.CustCountry.value;
      document.forms.Form.ShipPostalCode.value = document.forms.Form.CustPostalCode.value;
      document.forms.Form.ShipPhone.value = document.forms.Form.CustPhone.value;
   }else{
      if (strShowCompany == '1') 
         document.forms.Form.ShipCompany.value = '';           
      document.forms.Form.ShipFName.value = '';
      document.forms.Form.ShipLName.value = '';
      document.forms.Form.ShipAddress.value = '';
      document.forms.Form.ShipAddress2.value = '';
      document.forms.Form.ShipCity.value = '';
      document.forms.Form.ShipRegion.value = '';
      document.forms.Form.ShipCountry.value = '';
      document.forms.Form.ShipPostalCode.value = '';
      document.forms.Form.ShipPhone.value = '';
   }
}

// #####################################
// autopopulate fields
function autoPop(objFld) {
   if (objFld.value == 'scr' || objFld.value == 'mmm') {      
      if (strShowCompany == '1') 
         document.forms.Form.CustCompany.value = 'ACME';            
      if (objFld.value=='scr') {
         document.forms.Form.CustEmail.value = 's.test@ecware.com';
         document.forms.Form.CustFName.value = 'Greg';
         document.forms.Form.CustLName.value = 'Arious';
         document.forms.Form.CustLogon.value = 'password';
         document.forms.Form.CustLogonConfirm.value = 'password';
      }else if (objFld.value=='mmm') {
         document.forms.Form.CustEmail.value = 'm.test@ecware.com';
         document.forms.Form.CustFName.value = 'Emma';
         document.forms.Form.CustLName.value = 'Grant';
         document.forms.Form.CustLogon.value = '123';
         document.forms.Form.CustLogonConfirm.value = '123';
      }      
      document.forms.Form.CustAddress.value = '1212 Boogie Woogie Ave.';
      document.forms.Form.CustAddress2.value = 'Down by the Seaside';
      document.forms.Form.CustCity.value = 'Carlsbad';
      document.forms.Form.CustRegion.value = 'CA';
      document.forms.Form.CustCountry.value = 'US*';
      document.forms.Form.CustPostalCode.value = '92008';
      document.forms.Form.CustPhone.value = '877.548.8777';      
      document.forms.Form.CustLogonMode.checked = true;
   }
}

// #####################################
// dcp page jumper
function optionJumper(oForm, mSKU){   
   var x = 0;   
   var xSKU = mSKU;
   var xExt = '';
   var strExt = '';
   var sel;      
   for (var i = 0;i < 20;i++){   
      sel = document.getElementById('ext' + i);    
      if (sel) {            
         if (sel.value != '') {
            xSKU = xSKU + sel.value;
            xExt = xExt + '&ext' + i + '=' + sel.value;
            strExt = strExt + sel.value;
         }else{
            x = x + 1;
         }     
      }       
   }            
   if (x == 0)
      window.location.href = strExecutableURL + '/dcp/seo/' + strQS + '&msku=' + mSKU + '&Ext=' + strExt + '&SKU=' + xSKU + xExt + strWishListQS; 
   return false;     
}