/*
  $Id: general.js,v 1.2 2005/05/21 17:02:22 manmachine Exp $
+-----------------------------------------------------------------------+
|                  osCSS Open Source E-commerce                         |
+-----------------------------------------------------------------------+
| Copyright (c) 2005 The osCSS developers                               |
|                                                                       |
| http://www.counteractdesign.com                                       |
|                                                                       |
| Portions Copyright (c) 2003 osCommerce                                |
+-----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the GPL license,        |
| available at the following url:                                       |
| http://www.counteractdesign.com/license/2_0.txt.                      |
+-----------------------------------------------------------------------+
*/

function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=280,screenX=150,screenY=150,top=150,left=150')
}

function checkRadio(formObj, radioObj) {
	eval("myRadioObj = document.forms['"+formObj+"']."+radioObj);
	if(myRadioObj) {
		myRadioObj.checked=true;
	}
}

function del_check(field,id){
    for (i = 0; i < field.length; i++){
        field[i].checked = false ;
    }
    document.getElementById(id).checked = true;
    field.submit();
}

function captchaTest(theForm){

  
    var send = false;
    var stringUrl = 'captcha-ajax.php?action=check';
    var stringData= '&txtCaptcha='+encodeURIComponent( theForm.txtCaptcha.value );

    var objEmailDest = document.getElementById('mail_destinataire');
    var objEmailEnv = document.getElementById('mail_envoyeur');

    //verif email
    if (objEmailDest.value != ''){
        objEmailDest.style.borderColor = '';
        send = true;
    }else{
        objEmailDest.style.borderColor="#FF0000";
        send = false;
    }
    if (objEmailEnv.value != ''){
        objEmailEnv.style.borderColor = '';
        send = true;
    }else{
        objEmailEnv.style.borderColor="#FF0000";
        send = false;
    }

    $.ajax({
    type: "POST",
    url: stringUrl,
    data: stringData,
    success: function(msg){
        eval(msg);
        //Set the content of the DIV element with the response text
        //document.getElementById('captcha-result').innerHTML = txt;
        //Get a reference to CAPTCHA image
        img = document.getElementById('imgCaptcha');
        if (status != 'ok'){
            //Change the image
            img.src = 'create_image.php?' + Math.random();

        }else{
            if (send){
                disablePopup();
                close_popup();
                send_mail_ami();
                //theForm.submit();
                //Change the image - because of the next pop up click
                img.src = 'create_image.php?' + Math.random();
                //et effacer la precedente entree
                document.getElementById('txtCaptcha').value='';
            }
        }
    },
        error: ""
    });
}


function send_mail_ami(){
    var mail_destinataire = document.getElementById('mail_destinataire');
    var mail_envoyeur = document.getElementById('mail_envoyeur');
    var url_fiche = document.getElementById('url_fiche');
    var copie_mail = 'OFF';
     var out="";
    if(document.getElementById('array_result')){
        out = document.getElementById('array_result').innerHTML;
    }else{
        out ="";
    }

    if(document.getElementById('copie_mail').checked == true){
         copie_mail = 'ON';
    }
    // alert("herer2");

    stringUrl = 'ajax.php?action=send_mail_ami';
    stringData= '&mail_destinataire='+mail_destinataire.value+
    '&mail_envoyeur='+mail_envoyeur.value+
    '&url_fiche='+url_fiche.value+
    '&copie_mail='+copie_mail+
    '&result='+out;

    $.ajax({
        type: "POST",
        url: stringUrl,
        data: stringData,
        success: function(msg){
            if (msg == 'ok'){
            }else{
            }
        },
        error: ""
    });
}

function VerifChamps(){

    var produit = document.getElementById('products_id_sign').value;
   
    var largeur = document.getElementById('largeur').value;
    var longueur = document.getElementById('longueur').value;

   largeur= largeur .replace(',','.');
   longueur= longueur .replace(',','.');
   document.getElementById('largeur').value = largeur;
   document.getElementById('longueur').value = longueur;



    var finition = document.getElementById('finition_imp').value;
    var qty = document.getElementById('qty').value;
    var validation_bat = document.getElementById('validation_bat').value;


    var function_test_dimension=document.getElementById('function_test_dimension').value;
    var dimension=document.getElementById('dimension').value;
    var dimension_block_sales=document.getElementById('dimension_block_sales').value;

    var function_test_dimension_max=document.getElementById('function_test_dimension_max').value;
    var dimension_max=document.getElementById('dimension_max').value;
    var dimension_block_sales_max=document.getElementById('dimension_block_sales_max').value;


    var dimension_check_message=document.getElementById('dimension_check_message').value;


    var errormsg = 'Afin d’afficher votre tarif, merci de remplir le(s) champ(s) suivant(s) :';
   
    var resultat = true;
     if(produit ==null){
         resultat = false;
     }
     if(largeur ==null || largeur ==0){
         errormsg =errormsg + '<br /> <strong>- hauteur</strong> (en cm)';

       document.getElementById("largeur").style.backgroundColor="#ecdff0";
         document.getElementById("largeur").style.borderColor="#9150a4";
        
         resultat = false;
     }else{
         document.getElementById("largeur").style.backgroundColor="#F0F9FF";
         document.getElementById("largeur").style.borderColor="#B4C9DE";
     }
     
     if(longueur ==null || longueur ==0){
        errormsg =errormsg + '<br /> <strong>- longueur</strong> (en cm)';
         

 document.getElementById("longueur").style.backgroundColor="#ecdff0";
         document.getElementById("longueur").style.borderColor="#9150a4";
         resultat = false;
     }else{
          document.getElementById("longueur").style.backgroundColor="#F0F9FF";
         document.getElementById("longueur").style.borderColor="#B4C9DE";
     }

   var errormsg2="";
   var dimension_error=0;
   
   if(function_test_dimension=="min"){

        if(parseFloat(longueur) < dimension){
            dimension_error=1;
            if(dimension_block_sales==1){
               resultat=false;
               document.getElementById("longueur").style.backgroundColor="#ecdff0";
               document.getElementById("longueur").style.borderColor="#9150a4";
            }else{
               document.getElementById("longueur").style.backgroundColor="#F0F9FF";
               document.getElementById("longueur").style.borderColor="#B4C9DE";
            }
            
        }else{
           // document.getElementById("longueur").style.backgroundColor="#F0F9FF";
           // document.getElementById("longueur").style.borderColor="#B4C9DE";

        }

        if(parseFloat(largeur) < dimension){
            dimension_error=1;


            if(dimension_block_sales==1){
               resultat=false;
               document.getElementById("largeur").style.backgroundColor="#ecdff0";
               document.getElementById("largeur").style.borderColor="#9150a4";
            }else{
               document.getElementById("largeur").style.backgroundColor="#F0F9FF";
               document.getElementById("largeur").style.borderColor="#B4C9DE";
            }

            //document.getElementById("largeur").style.backgroundColor="#ecdff0";
             //document.getElementById("largeur").style.borderColor="#9150a4";
             //resultat = false;
        }else{
            //document.getElementById("largeur").style.backgroundColor="#F0F9FF";
            //document.getElementById("largeur").style.borderColor="#B4C9DE";

        }
        

    }
    
    if(function_test_dimension_max=="max"){

        if(parseFloat(longueur) > dimension_max){
            dimension_error=1;

            if(dimension_block_sales_max==1){
               resultat=false;
               document.getElementById("longueur").style.backgroundColor="#ecdff0";
               document.getElementById("longueur").style.borderColor="#9150a4";
            }else{
               document.getElementById("longueur").style.backgroundColor="#F0F9FF";
               document.getElementById("longueur").style.borderColor="#B4C9DE";
            }
             //resultat = false;
        }else{
           // document.getElementById("longueur").style.backgroundColor="#F0F9FF";
            //document.getElementById("longueur").style.borderColor="#B4C9DE";

        }

        if(parseFloat(largeur) > dimension_max){
            dimension_error=1;

            if(dimension_block_sales_max==1){
               resultat=false;
               document.getElementById("largeur").style.backgroundColor="#ecdff0";
               document.getElementById("largeur").style.borderColor="#9150a4";
            }else{
               document.getElementById("largeur").style.backgroundColor="#F0F9FF";
               document.getElementById("largeur").style.borderColor="#B4C9DE";
            }


             //resultat = false;
        }else{
            //document.getElementById("largeur").style.backgroundColor="#F0F9FF";
            //document.getElementById("largeur").style.borderColor="#B4C9DE";

        }
        
    }

    if(dimension_error==1){
            errormsg2="<div style='height:12px;'></div>"+dimension_check_message;
        }


     if(finition ==null){
         resultat = false;
     }
     if(qty ==null || qty ==0){
          errormsg =errormsg + '<br /> <strong>- quantit&eacute;</strong> ';
           
      
         document.getElementById("qty").style.backgroundColor="#ecdff0";
         document.getElementById("qty").style.borderColor="#9150a4";
         resultat = false;
     }else{
          document.getElementById("qty").style.backgroundColor="#F0F9FF";
         document.getElementById("qty").style.borderColor="#B4C9DE";
     }


     if(validation_bat=="OUI"){
                        //msg[0]+="<br />sous réserve que votre fichier soit exploitable";
                        document.getElementById("validation-info").style.display="block";
                    }else{
                        document.getElementById("validation-info").style.display="none";
                    }
       if(resultat==true){
       
       $.ajax({
            type: "POST",
            url: "ajax_calculate_tarrif.php",
            data: "produit="+produit+"&longueur="+longueur+"&largeur="+largeur+"&finition="+finition+"&qty="+qty+"&validation_bat="+validation_bat,
            success: function(msg){
                    msg=msg.split('/');
                    
                    $("#text_merci").hide();
                    $("#delai_liv_imp").html(msg[0]);
                    $("#tarrif_signaletiques").html(msg[1]);
                    $("#text_info").hide();
                    $("#bouton_refresh").show();
                    $("#link_add_cart").show();
                    
                     $("#allOptionComplete").val(1);
                     $('#submitForm').focus();

                   
                   // var t=setTimeout("addCart()",5000);
                  

                    
                     
            }
        });

     }else{
         // errormsg = errormsg + "<br> Merci de compl&eacute;ter tous les champs afin d\'afficher votre tarif";
        $("#text_merci").hide();
         $("#bouton_refresh").hide();
          $("#text_info").show();
          $("#text_info").html(errormsg+errormsg2);

         $("#delai_liv_imp").html("");
          $("#tarrif_signaletiques").html("");

                    $("#bouton_refresh").hide();
                    $("#link_add_cart").hide();
          
          
     }

}

function addCart()
{

document.getElementById('calcultarrif').submit();
}

function disableEnterKey(e,test)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13){
         
          return false;
     }
     else{
          return true;
     }
}
function changeproducts(){
  
    var url = document.getElementById('produit_imp').options[document.getElementById('produit_imp').selectedIndex].value;
    if(url !=""){
       // document.location.replace(window.location.href=url);
       window.location.href=url
    }

}

function valSubmit(){
    
    if($("#allOptionComplete").val()==1){
       return true;
    }else{
        return false;
    }



}


function checkEnterKey(e,idClick)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13){
         
          if(idClick=="largeur"){
              //alert(1);
              $('#longueur').focus();
               //alert(2);
          }else if(idClick=="longueur"){
              //alert(1);
              $('#qty').focus();
               //alert(2);
          }else{
              VerifChamps();
          }


          return false;
     }
     else{
          return true;
     }
}

function checkOnFocus(elem){

if(elem.value!=""){
    $("#allOptionComplete").val(0);
}

}
