// samsung JavaScript Document

//<--------------------------------------------------------------------------//   Check Other Operator 2     //---------------------------------------------<<
function check_if_other2(cy) {
	var form = cy.form, country = cy.value;
	if(country != 'op') {
		document.getElementById('other_operatorx').className = 'other_operator';
		document.getElementById('other_operator').value = 'Opèrateur';
	} else {
		document.getElementById('other_operatorx').className = 'other_operator_none';
		document.getElementById('other_operator').value = 'Operators';
	}
}

//<--------------------------------------------------------------------------//   Check Other Operator      //---------------------------------------------<<
function check_if_other(op) {
	var form = op.form, manufacturer = op.value;
	if(manufacturer == 'AutreModel') {
		document.getElementById('other_brandx').className = 'other_brand';
		document.getElementById('other_brand').value = 'modèle';
	} else if(manufacturer.toLowerCase() == 'i900' || manufacturer.toLowerCase() == 'i900v' || manufacturer.toLowerCase() == 'omnia') {
		location.replace('samsung_i900.php');
	} else {
		document.getElementById('other_brandx').className = 'other_brand_none';
		document.getElementById('other_brand').value = 'Models';
	}
}

//<--------------------------------------------------------------------------//   Check IMEI     //---------------------------------------------<<
function CheckNum(imei)
{
	if (/^\.?$/.test(imei.value) || !/^-?\d*\.?\d*$/.test(imei.value))
	 {
		 alert('Ce champ exige un nombre.');
		 imei.value=''; 
		 imei.focus();
		 return true;
	 }
	 else 
	 {
	 	return false;
	 }
}

/** Check if string is number or decimal */
String.prototype.isNumeric = function(float) {
   var ValidChars = '0123456789';
   if(float) ValidChars += '.';
   var IsNumber=true;
   var Char;
   
   for (i = 0; i < this.length && IsNumber == true; i++) { 
      Char = this.charAt(i);
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

//<--------------------------------------------------------------------------//   Validate Form     //---------------------------------------------<<
function verif_form(form)
{
	try {
  	nom = form.nom.value;
	prenom = form.prenom.value;
	email = form.email.value;
	model = form.model.value;
	country = form.country.value;
	operator = form.operator.value;
	imei = form.imei.value;
	other_brand = form.other_brand.value;
  	} catch(e) {
		/*alert(e);	*/
	}
	var place = email.indexOf("@",1);
	var point = email.indexOf(".",place+1);
	var texte = "ERROR : \n";
	var texte = "ERROR : \n";
	var erreur= false;
	
	if (country.length<=2 || country.indexOf("'")!=-1 || country.indexOf("'")!=-1 || country.indexOf("#")!=-1 || country.indexOf("/")!=-1 || country.indexOf("\"")!=-1 || country.indexOf("\\")!=-1 ) {
		texte= texte + "- Error in your Country \n";
		erreur= true;
	}
	
	if (operator.length<=2 || operator.indexOf("'")!=-1 || operator.indexOf("'")!=-1 || operator.indexOf("#")!=-1 || operator.indexOf("/")!=-1 || operator.indexOf("\"")!=-1 || operator.indexOf("\\")!=-1 ) {
		texte= texte + "- Error in your Operator \n";
		erreur= true;
	}
	
	if (model.length<=2 || model.indexOf("'")!=-1 || model.indexOf("'")!=-1 || model.indexOf("#")!=-1 || model.indexOf("/")!=-1 || model.indexOf("\"")!=-1 || model.indexOf("\\")!=-1 ) {
		texte= texte + "- Error in your phone Model \n";
		erreur= true;
	}
	try	{
		if (imei.length != 15 || imei.indexOf("'")!=-1 || imei.indexOf("'")!=-1 || imei.indexOf("#")!=-1 || imei.indexOf("/")!=-1 || imei.indexOf("\"")!=-1 || imei.indexOf("\\")!=-1 || !imei.isNumeric() ) {
			texte= texte + "- Error in your imei, please enter the first 15 digits of your IMEI number \n";
			erreur= true;
		}
		else if(!luhn_check(imei)){
			texte += " - Invalid IMEI Number\n";
			erreur= true;
		}		
	} 
	catch(e){
		alert(e);	
	}
	
	if ((place <= -1)||(email.length<=2)||(point <= 1)) {
		texte= texte + "- You must put a valid email \n";
		erreur= true;
	}

	if (nom.length<=2 || nom.indexOf("'")!=-1 || nom.indexOf("'")!=-1 || nom.indexOf("#")!=-1 || nom.indexOf("/")!=-1 || nom.indexOf("\"")!=-1 || nom.indexOf("\\")!=-1 ) {
		texte= texte + "- Error in your name \n";
		erreur= true;
	}

	if (prenom.length<=2 || prenom.indexOf("'")!=-1 || prenom.indexOf("'")!=-1 || prenom.indexOf("#")!=-1 || prenom.indexOf("/")!=-1 || prenom.indexOf("\"")!=-1 || prenom.indexOf("\\")!=-1) {
		texte= texte + "- Error in your surname \n";
		erreur= true;
	}

	if (email.indexOf("'")!=-1 || email.indexOf("'")!=-1 || email.indexOf("/")!=-1 || email.indexOf("\"")!=-1 || email.indexOf("\\")!=-1 || email.indexOf(" ")!=-1) {
		texte= texte + "- Votre email n'est pas correcte \n";
		erreur= true;
	}
	
		if(email.match(/@aol./)) {
			texte += " - We do not accept order with AOL email address, please type another email address. Thank you. \n";
			erreur= true;
		}	

  if (cgv = form.condition.checked) {} 
  else {
    texte= texte + "You must accept the general terms of sales to order.\n";
    erreur= true;
  }
  
  //-----------------------
	if (erreur){
		alert(texte);
		 return false;
	}
	else {
		//form.submit();
		return(true);
	}
  
}

function CheckNum(austres3)
{
	if (/^\.?$/.test(austres3.value) || !/^-?\d*\.?\d*$/.test(austres3.value))
	 {
		 alert('This field requires a number.');
		 austres3.value=''; 
		 austres3.focus();
		 return true;
	 }
	 else 
	 {
	 	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;
  }
}