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

function ValidateInput(form) {
  if (CheckEmail(form)) {
    if (CheckPassword(form)) {
      return true;
    } else { return false; }
  } else {  return false; }
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo
//  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 = "";
    alert("Your password needs to be between 6 and 20 characters.");
    form["password1"].focus;
    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
//  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
//  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
//  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;
  }
}
//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo
//  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="/en/mywl/sign_up/step2_contact_details.html"; }
    else { window.location="/en/mywl/sign_up/step1_id_password.html"; }
  } else {
    alert("Please agree to the Terms & Conditions!");
  }
}  
//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo
//  Purpose: Asks the cutomer one more time if he wants to cancel his/her mywl account.
//------------------------------------------------------------------------------   
function r_u_sure() {
var answer = confirm("Are you sure you want to cancel your account? Press OK to continue or Cancel to keep your account active.")
  if (answer) { return true; } else { return false; }  
}

//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo
//  Purpose: Asks the cutomer one more time if he wants to upgrade his/her mywl account.
//------------------------------------------------------------------------------   
function r_u_sure_upgrade() {
var answer = confirm("Are you sure you want to upgrade your account? Press OK to continue or Cancel to not upgrade.")
  if (answer) { return true; } else { return false; }  
}



//------------------------------------------------------------------------------    
//  Copyright (c) WorldLingo
//  Purpose: Asks the cutomer one more time if he wants to cancel his/her mywl account.
//------------------------------------------------------------------------------   
function validate_langpair(form) {
  selIndex1 = form["src_lang"].selectedIndex;
  selIndex2 = form["trg_lang"].selectedIndex;
  if (form["src_lang"].options[selIndex1].value == "") {
    alert("Please select the source language.");
    form["src_lang"].focus();
    return false;
  }
  if (form["trg_lang"].options[selIndex2].value == "") {
    alert("Please select the target language.");
    form["src_lang"].focus();
    return false;
  }
  if (!(form["src_lang"].options[selIndex1].value == "*" && form["trg_lang"].options[selIndex2].value == "*")){
    if (form["src_lang"].options[selIndex1].value == form["trg_lang"].options[selIndex2].value) {
      alert("Please select 2 different languages.");
      form["src_lang"].focus();
      return false;
    }
  }
  return true;
}
