
// Various validation etc. functions for all 3 brands
// plus Online Promotion Code handling

function updateOPC(opc, sourcecode, season, domain) ///// removed, now redundant
{
    return;
}


function expireOPC() ///// removed after being called accidentally on CSS. Function redundant, to be removed eventually.
{
  return;
}

// Set OPC form value if cookie has one
function setOPC(opc)
{
  if (document.location.href.indexOf('24ace') > -1 || document.location.href.indexOf('24studio') > -1 ) 
  {
    if (document.forms.frmPromotion)// only if page has an OPC form
    {
      formObj = document.forms.frmPromotion;
      if (formObj.txtOPC) 
      {
        if (document.cookie.indexOf('cssurn=') > -1)
        {
          formObj.txtOPC.value = document.cookie.match('cssurn=[^;]*')[0].substr(7);
        }
      }
    }
  }
    return;
}


function checkOPC()
{
    var strOPC = "";
    formObj = document.forms.frmPromotion;
    if (formObj == null)
        return false;
    if (formObj.txtOPC.value == '' || formObj.txtOPC.value == 'Enter your Online Price Code')
    {
        alert('Please enter your Online Price Code.');
        return false;    
    }
    regSpace = /(\s)/g;
    // NB there is an onchange() to force the field to uppercase but we do it here too
    formObj.txtOPC.value = formObj.txtOPC.value.toUpperCase().replace(regSpace, '');// uppercase & remove spaces
    strOPC = formObj.txtOPC.value;
    if (strOPC.length != 11)
    {
        alert("Sorry - the Online Price Code you entered is not valid. Please click Ok, then check the Code printed on your advert.");
        return false;
    }

    else 
    {
      //regular expression for OPC
      var regExp=/^[A-Z]{3}\d{8}|\d{8}[A-Z]{3}|\d{4}[A-Z]{3}\d{4}$/ 
  
      if (strOPC.search(regExp)==-1) //if match failed
      {
          alert("Sorry - the Online Price Code you entered is not valid. Please click Ok, then check the Code printed on your advert.");
          return false;
      }
    }
    return true; // submit OPC
}


function submitOPC(opc)
{
    document.frmPromotion.txtOPC.value = opc;
    if (checkOPC())
        document.frmPromotion.submit();
    
}

// Bounce homepage if OPC cookie is set
// *** functionality has now moved to js/seo_sources.js ***



// Validate new sign-in for customer identifying themselves on logon page
// returns boolean: true = form valid, false = form problem
function validateIdentify()
{
  formObj = document.forms.orderRef;
  strError = '';

  regexpSurname  = "^[A-Za-z]{1}[A-Za-z\- ']+$";
  regexpHouse  = "^[A-Za-z0-9\- /]{1,30}$"; 
  regexpPostcode = "^([A-Z]{1,2})([0-9]{1,2})([A-Z]{0,1})[ ]*([0-9])([A-Z]{2}$)";
  regexpBFPO = "^BFPO[ ]*\\d{1,4}$";

  // Surname
  if (formObj.surname_id.value != '')
  {
    if (formObj.surname_id.value.length > 1 && formObj.surname_id.value.length < 51)
    {
      var matchArray = formObj.surname_id.value.match(regexpSurname);
      if (!matchArray) strError += "\nYour Surname can only contain letters, single quotes, hyphens and spaces.";
    }
    else strError += "\nPlease enter your Surname.";
  }     
  else strError += "\nPlease enter your Surname.";

  // House no.
  if (formObj.house_no_id.value != '')
  { 
    var matchArray = formObj.house_no_id.value.match(regexpHouse);
    if (!matchArray) strError += "\nYour House or Flat number / Name can only contain letters, hyphens, digits and spaces.";
  }
  else strError += "\nPlease enter your House or Flat number / Name.";

  // Postcode or BFPO
  if (formObj.postcode_id.value != '')
  { 
    var matchArray = formObj.postcode_id.value.toUpperCase().match(regexpPostcode);
    var matchArray2 = formObj.postcode_id.value.toUpperCase().match(regexpBFPO);
    if (!matchArray && !matchArray2) 
    { 
      strError += "\nPlease enter a valid Postcode / BFPO.";
    }
  }
  else strError += "\nPlease enter a valid Postcode / BFPO.";

  if (strError != '')
  {
    strError = "Sorry, we found a problem with your entries:\n" +strError +"\n\nPlease click OK and check your entries."
    alert(strError);
    return false;  
  }

  else 
  {
    return true;
  }
}

// Set OPT-IN hidden fields according to the OPT-OUT checkbox state (i.e the reverse of each other)
// Passed checkbox form obj
// returns nothing
function changeOptIn(optOutObj) {
  if (optOutObj.name.indexOf('email') > -1)
  {
    document.getElementById('emailOptIn').value = (optOutObj.checked) ?'' :'ON' ;
  }
  else if (optOutObj.name.indexOf('sms') > -1)
  {
    document.getElementById('smsOptIn').value = (optOutObj.checked) ?'' :'ON' ;
  }
}

///////////////
