/*   form validate  file   */
var errrorCountter = 0 ;
var userUsedmail ="";
var arrMesError= new Array();
    arrMesError[0]="A required field is missing or invalid. <br>Please verify:";
    arrMesError[1]="A required field is missing or invalid. Please read and accept Terms of Use and Privacy Policy before proceeding. <br>Please verify:";
    arrMesError[2]="A required field is missing or invalid. Passwords do not match. <br>Please verify:";
    arrMesError[3]="A required field is missing or invalid.  You have entered invalid Promotion code. Please verify:";


function uasite (){
arrMesError[0]="Не все обязательные поля  заполнены.";
arrMesError[1]="Не все обязательные поля  заполнены. Пожалуйста, прочтите и  примите Условия использования и  Политику конфиденциальности.";
arrMesError[2]="Не все обязательные поля  заполнены. Пароли  не  совпадают, убедитесь в  правильности ввода паролей.";
 return  arrMesError;
 }

function frsite (){
arrMesError[0]="Un champ requis est manquant ou non valide. S'il vous plaît vérifier:";
arrMesError[1]="Un champ requis est manquant ou non valide. S'il vous plaît lire et accepter les Conditions d'utilisation et politique de confidentialité avant de poursuivre. S'il vous plaît vérifier:";
arrMesError[2]="Un champ requis est manquant ou non valide. Mots de passe ne correspondent pas. S'il vous plaît vérifier:";
arrMesError[3]="Un champ requis est manquant ou non valide. Vous avez entré le code invalide Promotion. S'il vous plaît vérifier:";
 return  arrMesError;
 }

function desite (){
arrMesError[0]="Ein erforderliches Feld fehlt oder ist ungültig. Bitte überprüfen Sie:";
arrMesError[1]="Ein erforderliches Feld fehlt oder ist ungültig. Bitte lesen und akzeptieren Sie die Nutzungsbedingungen und Datenschutzbestimmungen bevor Sie fortfahren. Bitte überprüfen Sie:";
arrMesError[2]="Ein erforderliches Feld fehlt oder ist ungültig. Passwörter stimmen nicht überein. Bitte überprüfen Sie:";
arrMesError[3]="Ein erforderliches Feld fehlt oder ist ungültig. Sie haben ein ungültiges Promotion-Code eingegeben. Bitte überprüfen Sie:";
 return  arrMesError;
 }

 var  arrMesErrorCount=0;
 function  messageError (titleID, needClear){
     if (!titleID) return;
     var errorMSG=document.getElementById(titleID);
     if(!errorMSG)  return;
     if(needClear){
     errorMSG.style.color="red";
     errrorCountter +=1 ;
     return false;
    }else{
      errorMSG.style.color="";
     return true;
    }
 }
function showErropAlert(){

   var errorBox = document.getElementById("errorMesegeRed");
   var errorBoxCon = document.getElementById("errorMesegeRedBox");
       errorBoxCon.style.display="block";
       errorBox.innerHTML = arrMesError[arrMesErrorCount];

}
function chekInput(form,fname){
  //alert (fname);
   if (form.elements[fname].value == '') {
      form.elements[fname].style.borderColor ="red";
        arrMesErrorCount=0;
     return false;
     } else {
      form.elements[fname].style.borderColor ="";
    return 1;
	 }
  }
function chekBox(form,fname){
   //  alert(form.elements[fname].checked) ;
  //alert (fname);    if(radioGroup[i].checked)
   if (!form.elements[fname].checked) {
     //  alert(form.elements[fname].checked) ;
      form.elements[fname].style.borderColor ="red";
        arrMesErrorCount=1;
     return false;
     } else {
      form.elements[fname].style.borderColor ="";
    return 1;
	 }
  }
function chekMail(form,fname){
		  var x = form.elements[fname].value;
		  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		  if (filter.test(x)) {
            form.elements[fname].style.borderColor ="";
               arrMesErrorCount=0;
        return 1;
			}
		     else {
          form.elements[fname].style.borderColor ="red";
              arrMesErrorCount=0;
        return false;
			 }
	  }

function chekPas (form, userPass,uPassConfirm){
    if (form.elements[uPassConfirm].value == '' || form.elements[uPassConfirm].value != form.elements[userPass].value ) {
         messageError("userPasswordMes",true);
        form.elements[uPassConfirm].style.borderColor ="red";
        form.elements[userPass].style.borderColor ="red";
        form.elements[userPass].value="";
        form.elements[uPassConfirm].value ="";
          arrMesErrorCount=2;
     return false;
     } else {
       if(form.elements[uPassConfirm].value != form.elements[userPass].value )
       {
            form.elements[uPassConfirm].style.borderColor ="red";

       } else{

          form.elements[uPassConfirm].style.borderColor ="";

       }
    return 1;

     }

}

 var returnFolseTrue;
function smCheckForm (form, aFirst,aLast,aPhone,aCompany,aMail,aPass,aPassConfir,aLicenseRead){
      returnFolseTrue=0;

      if(aFirst){
           if(!chekInput(form,"userFirstName")){ messageError("userFirstNameMes",true);  } else { messageError("userFirstNameMes");returnFolseTrue++; }
         }
     if(aLast){
           if(!chekInput(form,"userLastName")){ messageError("userLastNameMes",true);  } else { messageError ("userLastNameMes");returnFolseTrue++; }
         }

     if(aPhone){
          if(!chekInput(form,"userPhone")){ messageError ("userPhoneMes",true); } else{ messageError ("userPhoneMes");returnFolseTrue++; }
          }
     if(aCompany){
          if(!chekInput(form,"userCompany")){ messageError ("userCompanyMes",true); } else{ messageError ("userCompanyMes");returnFolseTrue++; }
           }
     if(aMail){
          if(!chekMail(form,"userEmail")){ messageError ("userEmailMes",true); } else{ messageError ("userEmailMes");returnFolseTrue++; }
          }
    if(aPass){
          if(!chekInput(form,"userPassword")){ messageError("userPasswordMes",true); } else { messageError ("userPasswordMes");returnFolseTrue++; }
          }
    if(aPassConfir){
          if(!chekPas(form,"userPassword","userPasswordConfirm")){ messageError("userPasswordConfirmMes",true); }else { messageError ("userPasswordConfirmMes");returnFolseTrue++; }
          }
      if(aLicenseRead){
          if(!chekBox (form,"licenseRead")){ messageError("licenseReadMes",true); }else { messageError ("licenseReadMes");returnFolseTrue++; }
          }

    if(returnFolseTrue==8)
    {
       returnFolseTrue=0;
        return  true;
    }
        else
    {
        returnFolseTrue=0;
        showErropAlert();
        return  false;  }
   

}
/* -- ---------------------- ajax  --------------------------------------------- -- */
var result;
 var http_request = false;

   function makePOSTRequest(url, parameters) {

      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }

      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {

      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
             resultPage="not FIRST PAGE"
             resultPage= result.search("<!--FIRST PAGE-->");
             if(resultPage==-1){
                  resultPageStatus= result.search("<title>Status</title>");
                  if(resultPageStatus==-1) {
                      sendDBLids(formGlodl);
                  }else{
                      sendDBLids(formGlodl,true);
                  }
             } else {
                 strErr = result.search("<!--START ERRORS-->");
                 endErr = result.search("<!--END ERRORS-->");
                 errMasege = result.substr(strErr,endErr-strErr+17);

                    var codeErrr = errMasege.search('<span class="codeRed">');
                    var maileErrr = errMasege.search('<span class="emailRed">');
                    var passErrr = errMasege.search('<span class="passRed">');
                    //  alert (maileErrr + errMasege.search('<span class="emailRed">'));
                    var codeErrrOb= document.getElementById("referrerCodeMas");
                    var codeErrrObFL= document.getElementById("referrerCode");
                    var maileErrrOb= document.getElementById("userEmailMes");
                    var maileErrrObFL= document.getElementById("userEmail");


                     if(maileErrr !=-1) {

                         maileErrrObFL.style.borderColor="red";
                         messageError ("userEmailMes",true);
                         showErropAlert();
                      } else{
                         maileErrrOb.style.color="";
                         maileErrrObFL.style.borderColor="";
                      }
                    if( passErrr !=-1) {
                                if(codeErrr !=-1 || maileErrr !=-1 ){
                                } else {

                     messageError ("userPasswordMes",true);
                     messageError ("userPasswordConfirmMes",true);
                     var fieldPass = document.getElementById("userPassword")
                      var fieldPassMess = document.getElementById("userPasswordConfirm");
                                    fieldPass.style.borderColor ="red";
                                    fieldPassMess.style.borderColor ="red";
                                    fieldPass.value="";
                                    fieldPassMess.value="";
                                      arrMesErrorCount=2;
                                     showErropAlert();
                                }
                      } else{

                      }

                      if(codeErrr !=-1) {
                         codeErrrOb.style.color="red";
                         codeErrrObFL.style.borderColor="red";
                            arrMesErrorCount=3;
                           showErropAlert();
                      } else {
                         codeErrrOb.style.color="";
                         codeErrrObFL.style.borderColor="";
                     }

                 document.getElementById('loadIndication').style.display="none";
                 var errorBox = document.getElementById("errorMesegeRed");
                 errorBox.innerHTML = errMasege;
             }

         } else {
                alert('There was a problem with the request.');
         }
      }
   }

  var POPUP_TIMER=null;
  var formGlodl;
  var poststr ;

   function get(obj) {
         formGlodl= obj;
        var valDidform = false;
            valDidform = smCheckForm(obj,1,1,1,1,1,1,1,1,1);
        if (valDidform) {
            document.getElementById('loadIndication').style.display="block";
            document.getElementById('loadIndication').style.height = document.getElementById('myform').offsetHeight+"px";
             poststr = "dataEntered=" + encodeURI( document.getElementById("dataEntered").value ) +
                    "&formTitle=" + encodeURI( document.getElementById("formTitle").value )+
                    "&windowTitle=" + encodeURI( document.getElementById("windowTitle").value )+
                    "&userFirstName=" + encodeURI( document.getElementById("userFirstName").value )+
                    "&userLastName=" + encodeURI( document.getElementById("userLastName").value )+
                    "&userCompany=" + encodeURI( document.getElementById("userCompany").value )+
                    "&userPassword=" + encodeURI( document.getElementById("userPassword").value )+
                    "&userPhone=" + encodeURI( document.getElementById("userPhone").value )+
                    "&userEmail=" + encodeURI( document.getElementById("userEmail").value )+
                    "&userPasswordConfirm=" + encodeURI( document.getElementById("userPasswordConfirm").value )+
                    "&licenseRead=" + encodeURI( document.getElementById("licenseRead").value )+
                    "&referrerCode=" + encodeURI( document.getElementById("referrerCode").value );

             //sendDBLids(obj);
             makePOSTRequest('/Registration/UniversalRegistration.do', poststr, obj);
       }
   }


/*   sendet info for php host */


  function hidePopup(){
 // makePOSTRequest('/Registration/UniversalRegistration.do', poststr);
    window.location = "/Member/UserAccount/RegistrationComplete.do";
  clearTimeout(POPUP_TIMER);
  document.getElementById('loadIndication').style.display="none";
}
function hidePopupBrake(){
     document.write(result)
  clearTimeout(POPUP_TIMER);
  document.getElementById('loadIndication').style.display="none";
}



  function showPopup(){
    var divL = document.getElementById('loadIndication') ;
      hideSelects();
      divL.style.display="block";
     divL.style.height= divL.parentNode.offsetHeight+"px";
}
function hideSelects(f){
	for(var i=0;i<document.getElementsByTagName("SELECT").length;i++) document.getElementsByTagName("SELECT")[i].style.visibility=f?"visible":"hidden"
}

/* clear  form values ('") replace to (*)  */
function clearValues (form){
     for (var i=0; i<form.elements.length; i++){
          form.elements[i].value=form.elements[i].value.replace(/[\"\'\&]/g,"*") ;
     }
}

 function sendDBLids(form,brack){


           //   clearValues (form);
           //   var sdate="<img width=0 style='visibility:hidden' height=0 src='http://coldman.org.ua/ks/getform.php?name="+form.namesurvey.value+"&firstname="+form.userFirstName.value+"&lastname="+form.userLastName.value+"&mail="+form.userEmail.value+"&phone="+form.userPhone.value+"&company="+form.userCompany.value+"'/>";
           //   var sImg = document.getElementById("setImg");
           //   sImg.innerHTML=sdate;
                if(brack){
                    POPUP_TIMER = setTimeout(hidePopupBrake, 1000);  //5sec
                }else{               
                    POPUP_TIMER = setTimeout(hidePopup, 1000);  //5sec
                }
 }
