function validateFormOnSubmit(theForm)
{
  var reason = "";

  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validateIMEI(theForm.IMEI);

  if (reason != "") {
    alert("Please correct the following information:\n" + reason);
    return false;
  }

  alert("Hi-N-Bye trial version will be sent to you by email. \n -You'll be redirected to our forum in a few moments");
  return true;
}

function validateBuyFormOnSubmit(buyForm)
{
  var reason = "";
  reason += validateName(buyForm.name);
  reason += validateEmail(buyForm.os0);
  reason += validateIMEI(buyForm.os1);
  
  if(buyForm.os2.options[0].selected)
  	reason += "-Please select your device";

  if (reason != "") {
    alert("Please correct the following information:\n" + reason);
    return false;
  }

  else{
  	setAction(buyForm);
	return EJEJC_lc(buyForm);
  }
  //return true;
}

function setAction(buyForm)
{   
   if(buyForm.formName.value == "HnBForm")
   {
   	//alert(buyForm.formName.value);
   	if(buyForm.os2.options[1].selected)
   	{
   		// FP1 models
      		buyForm.action = "https://www.e-junkie.com/ecom/gb.php?c=cart&i=194379&cl=24323&ejc=2";
   	}
   	else if(buyForm.os2.options[2].selected)
   	{
   		// FP2 models
      		buyForm.action = "https://www.e-junkie.com/ecom/gb.php?c=cart&i=194380&cl=24323&ejc=2";
	}
   }
   else if(buyForm.formName.value == "RnSForm")
   {
   	//alert(buyForm.formName.value);
   	if(buyForm.os2.options[1].selected)
	{
	   	// FP1 models
	      	buyForm.action = "https://www.e-junkie.com/ecom/gb.php?c=cart&i=182418&cl=24323&ejc=2";
	}
	else if(buyForm.os2.options[2].selected)
	{
	   	// FP2 models
	      	buyForm.action = "https://www.e-junkie.com/ecom/gb.php?c=cart&i=194665&cl=24323&ejc=2";
	}
   }
      
   return true;
}

function validateIMEI(fld)
{
	  var s = fld.value;
	  var error = "";

	  var etal = /^[0-9]{15}$/;
	  if (!etal.test(s)){
		fld.style.background = 'Yellow';
		return "-Invalid IMEI";
	  }
	  sum = 0; mul = 2; l = 14;
	  for (i = 0; i < l; i++) {
		digit = s.substring(l-i-1,l-i);
		tp = parseInt(digit,10)*mul;
		if (tp >= 10)
			 sum += (tp % 10) +1;
		else
			 sum += tp;
		if (mul == 1)
			 mul++;
		else
			 mul--;
		}
	  chk = ((10 - (sum % 10)) % 10);
	  if (chk != parseInt(s.substring(14,15),10)){
		fld.style.background = 'Yellow';
		return "-Invalid IMEI";
	  }
	  else{
	  	fld.style.background = 'White';
	  }
	  return "";

	/*
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   	if (fld.value == "") {
        error = "-Please enter your IMEI.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "-IMEI contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 15)) {
        error = "-Your IMEI is the wrong length.\n";
        fld.style.background = 'Yellow';
    }
    return error;
    */
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld)
{
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "-Please provide an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "-Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "-The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateName(fld) {
    var error = "";

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "-Please enter your name. \n";
    } else if (fld.value.length < 3){
        error = "-The name is too short. \n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }

   return error;
}