// JavaScript Document
browser_version= parseInt(navigator.appVersion);
browser_type = navigator.appName;
if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 4)) {
document.write("<link REL='stylesheet' HREF='css/styles_ie.css' TYPE='text/css'>");
}
else {
document.write("<link REL='stylesheet' HREF='css/styles.css' TYPE='text/css'>");
}

function alloperator()
{
if (eval(document.getElementById('operator').value) == 1089)
{
document.getElementById('autre').value = '';
document.getElementById('autrecountry').value = '';
document.getElementById('autre').style.display = 'inline';
document.getElementById('autrecountry').style.display = 'inline';
document.getElementById('ope').style.display = 'inline';
document.getElementById('coun').style.display = 'inline';
document.getElementById('opeImg').style.display = 'none';

autre.focus();
}
else
{
document.getElementById('autre').value = ' ';
document.getElementById('autrecountry').value = ' ';
document.getElementById('autre').style.display = 'none';
document.getElementById('autrecountry').style.display = 'none';
document.getElementById('ope').style.display = 'none';
document.getElementById('coun').style.display = 'none';
document.getElementById('opeImg').style.display = 'inline';

}
}

/*function show_prompt()
{
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
  {
  document.write("Hello " + name + "! How are you today?");
  }
}*/

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }	
  else
    {
    return true;
    }
  }
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function validate(form_id,email) {


   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   //var reg = /^([A-Za-z0-9_\-\.])+\@([gmail\yahoo\hotmail])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   var lowercaseAddress = address.toLowerCase();
   if(lowercaseAddress.search("aol")!=-1 ){
   	  alert('Veuillez rentrer une adresse e-mail valide!');
      return false;
   }
   var email = document.getElementById("email");
   if(reg.test(address) == false) {
      alert('Veuillez rentrer une adresse e-mail valide!');
      return false;
   }
}


function validate_email(field,alerttxt)
{
with (field)
  {
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
aol=value.search(/aol.com/i);
aolfr=value.search(/aol.fr/i);
if (apos<1||dotpos-apos<2)
    {
	alert(alerttxt);return false;
	}
else if(aol>1||aolfr>1)
	{
	alert("This email address (@ aol) is not accepted here. Please choose another e-mail!");
	return false;
	}
  else {return true;}
  }
}

function trim(str)
   {
    s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
    return s;
   }


function validate_imei(field,alerttxt)
{
with (field)
  {
  imeistring=trim(value.toString());
  imeilength=(imeistring.length);
    if (value==null||isNaN(value)==true||imeilength<15||imeistring[1]=="x"||imeistring[1]=="X")
    {
    alert(alerttxt);return false;
    }	
	else if(value.indexOf(".")>-1){
		alert(alerttxt);return false;
	}
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
  {
with (thisform)
  {
  if (agree.checked==false)
    {
	alert ('lease accept the terms and conditions!');
	agree.focus();
	return false;
	}
  }	
with (thisform)
  {
  if (validate_required(model,"Please fill in the box `` model ``")==false)
  {model.focus();return false;}
  }
 with (thisform)
  {
  if (validate_required(autre,"Please fill in the box `` Other Operators `` ")==false)
  {autre.focus();return false;}
  }
   with (thisform)
  {
  if (validate_required(autrecountry,"Please fill in the box ``  Country ``")==false)
  {autrecountry.focus();return false;}
  }
 
  with (thisform){
  	try{
  		if (validate_required(imei,"Please fill in the box `` IMEI ``")==false){
  			imei.focus();
  			return false;
  		}
  		else if (validate_imei(imei,"Please follow the format of your IMEI number")==false){
  			imei.focus();
  			return false;
  		}
  		else if(!luhn_check(imei.value)){
			alert("Invalid IMEI Number\n");
			imei.focus();
			return false;
		}
  	}
  	catch(e){
		alert(e)
	}	  
  }
with (thisform)
  {
  if (validate_required(lastname,"Please fill in the box `` Last Name ``")==false)
  {lastname.focus();return false;}
  }
with (thisform)
  {
  if (validate_required(firstname,"Please fill in the box ``First Name ``")==false)
  {firstname.focus();return false;}
  }
  
with (thisform)
  {
  if (validate_required(email,"Please fill in the box `` Email``")==false)
  {email.focus();return false;}
  }
with (thisform)
  {
  if (validate_email(email,"Please enter a valid e-mail address!")==false)
    {email.focus();return false;}
  }
}

function luhn_check(number) {
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number=number.replace(/\D/g, '');

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
  	var digit=number.charAt(i);

  	// Multiply alternate digits by two
    if (i % 2 == parity) {
    	digit=digit * 2;
      	// If the sum is two digits, add them together (in effect)
      	if (digit > 9) {
        	digit=digit - 9;
      	}
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
  	return true;
  }
  else {
  	return false;
  }
}
