//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Open and close the div relating to login and registration 
//------------------------------------------------------------------------------   
 
function HideOrShowStuff(controlToHide) {
  if (document.getElementById) {
    // Hide all regions
    document.getElementById('txtShow1').style.display = 'none';
    document.getElementById('txtShow2').style.display = 'none';
    document.getElementById('txtShow1').disabled = 'disabled';
    document.getElementById('txtShow2').disabled = 'disabled';
    // Display the requested region
    document.getElementById
        ('txtShow' + controlToHide).style.display = 'block';
    document.getElementById
        ('txtShow' + controlToHide).disabled = '';
  } else {
    alert('Sorry, your browser does not support css2. Consider upgrading to a newer browser to take advantage of site functionality');
  }
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Validate the form input parameters
//------------------------------------------------------------------------------   

function ValidateInput(form) {
  if (form.create[0].checked) {
    if (CheckNameDetails(form)) {
      if (CheckEmail(form)) {
        if (CheckPassword(form)) {
          return true;
        }
      }
    }
  } else {
    return CheckLoginDetails(form);
  }
  return false;
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Check if the registration parameters are valid.
//------------------------------------------------------------------------------   

function CheckNameDetails(form) {
  if (form["firstname"].value == "") {
    alert("Please enter your First/Given Name.");
    form["firstname"].focus();
    return false;
  }
  if (form["lastname"].value == "") {
    alert("Please enter your Last/Surname Name.");
    form["lastname"].focus();
    return false;
  }
  return true;
}

function CheckEmail(form) {
  if (form["username1"].value != form["username2"].value) {
    alert("Email addresses don't match please check.");
    form["username1"].focus();
    return false; 
  }
  if (!emailCheck(form["username1"].value)) {
    form["username1"].focus();
    return false;
  }
  return true;
}

function checkPasswd(form) {
  if (CheckPassword(form)) {
    form.submit();
  }
}

function CheckPassword(form) {
  if (form["password1"].value.length > 20 ||
       form["password1"].value.length < 6) {
    form["password1"].value = "";
    form["password2"].value = "";
    form["password1"].focus;
    alert("Your password needs to be between 6 and 20 characters.");
    return false;
  }
  if (form["password1"].value != form["password2"].value) {
    form["password1"].value = "";
    form["password2"].value = "";
    form["password1"].focus;
    alert("Passwords do not match, Please try again");
    return false;
  }
  return true;
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Check for valid email address 
//------------------------------------------------------------------------------   

function emailCheck (emailStr) {
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  var matchArray=emailStr.match(emailPat)
  if (matchArray==null) {
    alert("Email address seems incorrect (check @ and .'s)")
    return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]
  if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
  }
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("Destination IP address is invalid!")
        return false
      }
    }
    return true
  }
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
    alert("The domain name doesn't seem to be valid.")
    return false
  }
  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>4) {
    alert("The address must end a two, three or four letter word.")
    return false
  }
  if (len<2) {
    alert("This address is missing a hostname!")
    return false
  }
  return true;
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Check if the login parameters are valid.
//------------------------------------------------------------------------------   

function CheckLoginDetails(form) {
  if (form["username"].value == "") {
    alert("Please enter your username.");
    form["username"].focus();
    return false;
  }
  if (form["password"].value == "") {
    alert("Please enter your password.");
    form["password"].focus();
    return false;
  }
  return true;
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Checks if the customer agrees to the terms and conditions before subscribing
//------------------------------------------------------------------------------   

function agree_terms_conditions(step) {
  //checks if the customer agrees to the terms and conditions before buying a product
  if (document.getElementById('agree_terms').checked) {
    if (step == 2) { window.location="/sign_up/step2_contact_details.html"; }
    else { window.location="/mywl/sign_up/step1_id_password.html"; }
  } else {
    alert("Please agree to the Terms & Conditions!");
  }
}  

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo Translations LLC
//  Purpose: Reads the username login cookie
//------------------------------------------------------------------------------   
function Get_Cookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )  {
    return null;
  }
  if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
    var value = document.cookie.substring( len, end );
  if (value) {
    document.getElementById('username').value = value;
    document.getElementById('remember_me').checked = true;
  }
}