//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
function SUC(champ) //~~ initialisation ~~ Saisir Uniquement des Chiffres
//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
{
   this.champ=champ;
   var Lui=this;
   var ie = false; /*@cc_on ie = true; @*/
   if ( ie ) {
	this.champ.onkeypress = Lui.IE;
   }
   else  { 
      this.champ.onkeyup = function(e) {
	 Lui.FF(this, e);
      }
   }
}

SUC.prototype.IE = function() {
   if ( event.keyCode<0x30 || event.keyCode>0x39 ) {
      event.returnValue= false;
   }
}

SUC.prototype.FF = function(zone,evt) {
   if ( evt.which<0x30 || evt.which>0x39 ) { 	
      zone.value=zone.value.replace(/[^0-9]/g,"");
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
function SUC1(champ) //~~ initialisation ~~ Saisir Uniquement des Chiffres
//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
{
   this.champ=champ;
   var Lui=this;
   var ie = false; /*@cc_on ie = true; @*/
   if ( ie ) {
      this.champ.onkeypress = Lui.IE;
   }
   else  { 
      this.champ.onkeyup = function(e) {
	 Lui.FF(this, e);
      }
   }
}

SUC1.prototype.IE = function() {
   if (event.keyCode<0x27 || (event.keyCode>0x27 && event.keyCode<0x41) || (event.keyCode>0x59 && event.keyCode<0x61) || (event.keyCode>0x79 && event.keyCode<0xE0)) {
      event.returnValue= false;
   }
}

SUC1.prototype.FF = function(zone,evt) {
   if (evt.which<0x27 || (evt.which>0x27 && evt.which<0x41) || (evt.which>0x59 && evt.which<0x61) || (evt.which>0x79 && evt.which<0xE0)) { 	
      zone.value="";
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
function SUC2(champ) //~~ initialisation ~~ Saisir Uniquement des Chiffres
//~~~~~~~~~~~~~~~~~~~~~~~~~~ #        #                             #
{
 this.champ=champ;
 var Lui=this;
 var ie = false; /*@cc_on ie = true; @*/
 if ( ie ) {
     this.champ.onkeypress = Lui.IE;
    }
 else  { 
     this.champ.onkeyup = function(e)
      {
       Lui.FF(this, e);
      }
    }
}

SUC2.prototype.IE=function() 

{
 if (event.keyCode>0x30 && event.keyCode<0x39 )
 {
  event.returnValue= false;
 }
}

SUC2.prototype.FF=function(zone,evt)

{
 if (evt.which>0x30 && evt.which<0x39)
 { 	
  	zone.value="";
 }

}


/*************** Test de l'adresse mail tapé page contact*****************/
function isEmail(email,lang) { 
   // var reg = /^[a-zA-Z0-9]+[a-z0-9._-]*@[a-z0-9_-a-z0-9_.a-z0-9]{2,}[.][a-z]{2,3}$/
	var val=email.value;
   //var reg = /[^\[\]()<>@\.,;:\\"\s]+(\.[^\[\]()<>@\.,;:\\"\s]+)*@[^\[\]()<>@\.,;:\\"\s]+(\.[^\[\]()<>@\.,;:\\"\s]+)+\s*$/;
	//var regexp = new RegExp("^[a-zA-Z0-9_\\-\\.]{3,}@[a-zA-Z0-9\\-_]{2,}\\.[a-zA-Z]{2,4}$", "g");
	var regexp = new RegExp("^[a-zA-Z0-9_\\-\\.]{3,}@[a-zA-Z0-9_\\-\\.]+\\.[a-zA-Z]{2,4}$", "g");
   if(!regexp.test(val)) {
		(lang=='fr')? msg = "Il y a une erreur de saisie." : msg = "An error while typing.";
		alert(msg);
		email.focus();
		return false;
		}
	return true;
   //return (reg.test (email));
}

function is_Email (email) { 
   var val=email;
   var regexp = new RegExp("^[a-zA-Z0-9_\\-\\.]{3,}@[a-zA-Z0-9_\\-\\.]+\\.[a-zA-Z]{2,4}$", "g");
   if(!regexp.test(val)) {
      return false;
   }
   return true;
}

function testEmail (email, lg){
   var is_email = isEmail (email);
   if (!is_email) {		
      if(lg == 'fr')
	 alert ('E-mail incorrect.');
      else
	 alert ('The mail address is not correct.');
   }

   return is_email;
}

var regex_name = '[^\\[\\]()<>@;:\\*\\+\\"\\s0-9\\{\\}\\\\]';

var test_name = {
   'en': 'The name is not correct.',
   'fr': 'Nom incorrect.',
   'reg' : '^\\s*(' + regex_name + '+|\\s*)+$'
};
var test_email = {
   'en': 'The mail address is not correct.',
   'fr': 'Email incorrect.',
   'reg': '^\\s*[\\w-]+(\\.\\w+)*@\\w+(\\.\\w+)+\\s*$'
}
var test_firstname = {
   'en': 'The first name is not correct.',
   'fr': 'Prénom incorrect.',
   'reg' : '^\\s*(' + regex_name + '+|\\s*)+$'
}
var test_address = {
   'en': 'The address is not correct.',
   'fr': 'Adresse incorrecte.',
   'reg' : '\\S+'
}
var test_city = {
   'en': 'The city is not correct.',
   'fr': 'Ville incorrecte.',
   'reg' : '^\\s*(' + regex_name + ')+(' + regex_name + '|\\s|[0-9])+$'
}
var test_zip = {
   'en': 'The ZIP is not correct.',
   'fr': 'Code postal incorrect.',
   'reg' : '^\\s*[0-9]+\\s*$'
}
var test_phone = {
   'en' : 'The phone is not correct.',
   'fr' : 'Numéro de téléphone incorrect.',
   'reg' : '\\s*\\+?[0-9\\s()]+\\s*$'
}
var test_prjtitle = {
   'en' : 'The project title is empty.',
   'fr' : 'Veuillez mettre le titre du projet.',
   'reg' : '^\\s*\\w(' + regex_name + '|\\s|[0-9])+$'
}
var test_description = {
   'en' : 'You should describe the project.',
   'fr' : 'Veuillez mettre la description du projet.',
   'reg' : '\\S+'
}

function test_field (field_id, reg, lg) {
   var field = document.getElementById (field_id);
   var regex = new RegExp (reg);
   var result = regex.test (field.value);

   if (!result) {
      alert (reg);
   }

   return result;
}

function validateForm (lg) {
   if (document.quoteForm.devis_1demand.checked) {
      return true;
   }

   var result;
   // result = test_field ('devis_email', test_email['reg'], lg);
   result = test_field ('devis_name', test_name['reg'], lg);

   return false;
}

function validateContact (lang) {
   var emailOK, nomOK, paysOK, prenomOK, telOK;
   emailOK = false;
   nomOK = false;
   paysOK = false;
   prenomOK = false;
   telOK = false;

   var inEmail = document.getElementById ('email');
   var inNom = document.getElementById ('nom');
   var inPays = document.getElementById ('pays');
   var inPrenom = document.getElementById ('prenom');
   var inTel = document.getElementById ('tel');

   emailOK = is_Email (inEmail.value);
   nomOK = is_alpha (inNom.value);
   paysOK = is_alpha (inPays.value);
   prenomOK = is_alpha (inPrenom.value);
   telOK = is_integer (inTel.value);

   inEmail.style.backgroundColor = (emailOK) ? "white" : "#FFDAB9";
   inNom.style.backgroundColor = (nomOK) ? "white" : "#FFDAB9";
   inPays.style.backgroundColor = (paysOK) ? "white" : "#FFDAB9";
   inPrenom.style.backgroundColor = (prenomOK) ? "white" : "#FFDAB9";
   inTel.style.backgroundColor = (telOK) ? "white" : "#FFDAB9";

   var formCheck = emailOK && nomOK && paysOK && prenomOK && telOK;

   return formCheck;
}


/*********************************************************************************/
function is_alphanum(value)
{
re = /\W/
return !re.test(value)
}

function is_alpha(value)
{
//re = /[^A-Za-z]/
re = /^(\W*[A-Za-z]+\W*)+$/
return re.test(value)
}

function is_integer(value)
{
//re = /[^ 0-9\+]/
re = /^[\+]?(\s|[0-9])+$/
return re.test(value)
}

function test_alphanum(value,lang)
{
	(lang=='fr')? msg = "Il y a une erreur de saisie." : msg = "An error while typing.";
	if(!is_alphanum(value)){ alert(msg);input.focus();input.select();}
}

function test_alpha(value,lang)
{
	var msg = "";
	(lang=='fr')? msg = "Il y a une erreur de saisie." : msg = "An error while typing.";
	var checkAlpha = is_alpha (value);
	if (!checkAlpha) { alert(msg);input.focus();input.select();}
	return checkAlpha;
}

function test_numeric(input,lang)
{
	(lang=='fr')? msg = "Il y a une erreur de saisie." : msg = "An error while typing.";
	var checkNumeric = is_integer (input);
	if (!checkNumeric){ alert(msg);input.focus();input.select();}
	return checkNumeric;
}

/********************************************************/

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* Concatenation du nom de domaine avec la zone */

function assembleFQDN () {
   var domain;
   var fqdn;
   var zone, zone_code;

   domain = trim (document.getElementById('nom_dns').value);

   if (domain == '') {
      return false;
   }
   
   zone = document.getElementById ('zone');
   zone_code = zone.options[zone.selectedIndex].text;
   
   fqdn = document.getElementById ('fqdn');
   fqdn.value = domain + '.' + zone_code;

   return true;
}

function trim (str) {
   var str = str.replace(/^\s\s*/, ''),
   ws = /\s/,
   i = str.length;
   if ( i ) {
      while (ws.test(str.charAt(--i)));
      return str.slice(0, i + 1);
   }
   return str;
}

function validTransfer () {
   var f = document.forms[0];
   var accepted = false;

   var i;
   for (i = 0; i < f.elements.length; i++) {
      id_val = f.elements[i].getAttribute ('id');
      if ( id_val == 'accept_transfer' ) {
         accepted = f.elements[i].checked;
         break;
      }
   }
   if ( !accepted ) alert ("Cocher s'il agit bien de votre nom de domaine");
   return accepted;
}


function afficherCondition(w, h, link) {
	var large = (screen.width-w)/2 ;
	var haut = (screen.height-h)/2 ;
	var position = "left="+large+",top="+haut ;
	awPopUpWin = window.open(link, "AWPopWin", "width="+w+",height="+h+",fullscreen=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,maximize=0,toolbar=0,alwaysRaised=yes,"+position);
}

function setExpDate() {
	var moiscb = document.getElementById ('moisCB');
	var anneecb = document.getElementById ('anneeCB');
	var expdate = document.getElementById ('EXPDATE');
	var mois = moiscb.value;
	var annee = anneecb.value;
	expdate.value = mois + annee;
}

function same_address (unchecked_value, checked_value) {
   var addr_deposant = document.getElementById ('addr_deposant');
   var deposant = addr_deposant.getElementsByTagName ('input');

   var addr_facturation = document.getElementById ('addr_facturation');
   var facturation = addr_facturation.getElementsByTagName ('input');

   var i;
   for (i = 0; i < facturation.length; i++) {
      f_id = facturation[i].getAttribute ('id');
      if (f_id == 'copy') {
         if ( !facturation[i].checked )
            return;
      }
      if ( f_id != null ) {
         d_id = f_id.replace (/^Fact/, 'deposant');
         d_input = document.getElementById (d_id);
         if ( d_input != null ) {
            facturation[i].value = d_input.value;
         }
      }
   }

   checkbox_status ('copy', unchecked_value, checked_value);
}

function checkbox_status (chk_box, unchecked_value, checked_value) {
   if ( unchecked_value == checked_value)
      return;

   var _inputs = document.getElementsByName (chk_box);
   
   var hidden_box, _checkbox;
   var i;
   for (i = 0; i < 2; i++) {
      if ( _inputs[i].getAttribute ('type') == 'hidden'  ) {
         hidden_box = _inputs[i];
      }
      else if ( _inputs[i].getAttribute ('type') == 'checkbox' ) {
         _checkbox = _inputs[i];
      }
   }
   
   if ( _checkbox.checked == true ) {
      hidden_box.value = checked_value;
   }
   else {
      hidden_box.value = unchecked_value;
   }

   return hidden_box.value;
}

function show_filename () {
   var fake_upload = document.getElementById ('fake_upload');
   var real_upload = document.getElementById ('real_upload');
   fake_upload.value = real_upload.value;
   if ( fake_upload.value != '')
      fake_upload.style.visibility = 'visible';
}

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

function get_radio_value (radio) {
   var i;
   for (i = 0; i < radio.length; i++) {
      if ( radio[i].checked ) {
         a = i;
         return radio[i].value;
      }
   }
   return;
}

function validate_radio (radio) {
   if ( get_radio_value (radio) === undefined ) {
      if ( arguments.length > 1 )
         alert (arguments[1]);

      return false;
   }

   return true;
}

function validate_desc (thisform) {
   var valid = true;
   with (thisform) {
      if ( validate_required (titre, "Spécifier le titre du devis") == false ) {
         titre.style.backgroundColor = '#f6a311';
         titre.style.color = '#000';
         titre.focus ();
         valid = false;
      }
      if ( validate_required (description, "Decrivez votre commande") == false ) {
         description.style.backgroundColor = '#f6a311';
         description.style.color = '#000';
         description.focus ();
         valid = false;
      }
      if ( validate_radio (prod, "Choisissez le type de devis") == false ) {
         valid = false;
      }
   }
   return valid;
}


function validate_payment (route, host) {
   var review = document.getElementById ('review');
   var payment = get_radio_value (review.Fact_payment);
   if ( payment === undefined ) {

      return false;
   }

   var proto = route[payment];
   var cur_action = review.action;
   var hostname;

   if ( proto == 'https' )
      hostname = host[1];
   else
      hostname = host[0];

   review.action = cur_action.replace (/^[a-z]+:\/\/[^/]+\//, proto + '://' + hostname + '/');

   return true;
}
