function getXHTMLRequest(){
	var req= false;
	
	try{
		req= new XMLHttpRequest();
	}
	catch(err1){
		try{
			req= new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(err2){
			try{
				req= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(err3){
				req=false;
			}
		}
	}
	return req;
}

var http=getXHTMLRequest();

//code verification
function generateRandomString(strLength){

    var text = "";

    var possible = "0123456789";

    for( var i=0; i < strLength; i++ ){

        text += ' ' + possible.charAt(Math.floor(Math.random() * possible.length));

    }

    return removeSpaces(text);

}

//remove spaces
function removeSpaces(string)

{

	return string.split(' ').join('');

}

function justNumbers(e)
{
  var keynum = window.event ? window.event.keyCode : e.which;
  if ((keynum == 8) || (keynum == 46))
  return true;
   
  return /\d/.test(String.fromCharCode(keynum));
  }

function validateEmailregistered()
{

    var emailReg = document.getElementById("email");
    var param = "email="+emailReg.value;
    var modurl="emailVerification.php?"+param;
    http.open("GET", modurl, true);
    http.onreadystatechange=function(){
    if(http.readyState==4){
        if(http.status==200){    
                       //alert(http.responseText);

                   if(http.responseText == "1"){
                           // alert('entro1');
                        document.getElementById("Emailregistered").style.display = "block";
                    }else{
                        document.getElementById("Emailregistered").style.display = "none";
                    }

                }else{
                    //alert("ha ocurrido un error: " + http.statusText);
                    }
            }

        };

    http.send(null);
  
  return true;
   
  }

function phone_verifi() {        //alert('hk');
    
    var firstName = document.getElementById("firstname");
    var lastName = document.getElementById("lastname");
    var phoneNo = document.getElementById("phone_no");
    var emailReg = document.getElementById("email");
    var regPassword = document.getElementById("password");
    var regPasswordConfirm = document.getElementById("confirmPassword");
    var provinceDropDown = document.getElementById("provinces");
    var cityDropDown = document.getElementById("cities");
    var regPostalCodePartOne = document.getElementById("postcod");
    var regPostalCodePartTwo = document.getElementById("postcod1");
    var streetNumber = document.getElementById("street_nro");
    var streetName = document.getElementById("street_name");

    var phoneNumber = document.getElementById("secondphone_no"); 
    //var max = phoneNumber.value.substring(0,10);         

    if(firstName.value.length==0)
    {
        alert("Enter firstName");
        firstName.focus();
        return false;
    }
    else if(lastName.value.length==0)
    {
        alert("Enter lastName");
        lastName.focus();
        return false;
    }
    else if(phoneNo.value.length==0)
    {
        alert("Enter phone");
        phoneNo.focus();
        return false;
    }
    else if(phoneNo.value.length<10)
    {
        alert("Enter a 10-digit phone number");
        phoneNo.focus();
        return false;
    }
    else if(emailReg.value.length==0)
    {
        alert("Enter email");
        emailReg.focus();
        return false;
    }
    else if(regPassword.value.length==0)
    {
        alert("Enter Password");
        regPassword.focus();
        return false;
    }
    else if(regPasswordConfirm.value.length==0)
    {
        alert("Enter Password Confirmation");
        regPasswordConfirm.focus();
        return false;
    }
    else if(regPassword.value != regPasswordConfirm.value)
    {
        alert("The password and its confirmation are not the same");
        regPasswordConfirm.focus();
        return false;
    }
    else if(regPostalCodePartOne.value.length==0)
    {
        alert("Enter Postal Code");
        regPostalCodePartOne.focus();
        return false;
    }
    else if(regPostalCodePartTwo.value.length==0)
    {
        alert("Enter Postal Code");
        regPostalCodePartTwo.focus();
        return false;
    }
    else if(streetNumber.value.length==0)
    {
        alert("Enter street Number");
        streetNumber.focus();
        return false;
    }
    else if(streetName.value.length==0)
    {
        alert("**Enter street name");
        streetName.focus();
        return false;
    }
    else if(phoneNumber.value.length==0)
    {
        alert("Enter phone number for verification code");
        phoneNumber.focus();
        return false;
    }
    else if(phoneNumber.value.length<10)
    {
        alert("Enter a 10-digit phone number");
        phoneNumber.focus();
        return false;
    }else{
            document.getElementById("Loading").style.display = 'block';
            var section_phone_verifi = document.getElementById("section_phone_verifi");
            var type_canal = document.getElementsByName("type_canal");
            var section_code_verifi = document.getElementById("section_code_verifi");
            var idop = 1;
    
            //generate random string ....    
            var CodeRandom = generateRandomString(4);
            
            for(var i=0;i<type_canal.length;i++)
            {
                if(type_canal[i].checked)
                    var type_canal_value=type_canal[i].value;
            }
            //alert(type_canal_value);
            /*section_phone_verifi.style.display = "none";*/
            section_code_verifi.style.display = "block";
            document.getElementById("codeVerification").value = CodeRandom;
            
            ////////////////////////
            //para definir el tipo de msj que se mostrara de acuerdo a cada seleccion y cada idioma
            if(document.getElementById("lang").value=="en" && document.getElementById("url_sms").checked == true){
                var msj='<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><strong>Sending...</strong> <br/></div>';
            }else if(document.getElementById("lang").value=="fr" && document.getElementById("url_sms").checked == true){
                    var msj='<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><strong>Envoi en cours...</strong> <br/></div>';
            }            
            if(document.getElementById("url_call").checked == true){
                var msj='<div class="alert alert-success alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><strong>Calling...</strong> <br/></div>';
            }
            //fin msj
            ////////////////////////

                var param = "code="+CodeRandom+"&phoneNumber="+phoneNumber.value+"&page="+type_canal_value+"&idop="+idop;
            	var modurl="phoneVerification.php?"+param;
                http.open("GET", modurl, true);
            	http.onreadystatechange=function(){
            	//alert('entro');
            	if(http.readyState==4){
                    document.getElementById("Loading").style.display = 'none';
                    document.getElementById("resultado").style.display = 'block';
            		if(http.status==200){			
            				//alert(http.responseText);
                			document.getElementById("resultado").innerHTML = msj;
                            }else{
                				//alert("ha ocurrido un error: " + http.statusText);
                				document.getElementById("resultado").innerHTML = '<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><strong>An Error Has Occurred</strong> <br/></div>';
                                }
                		}
                	};           
                http.send(null);    
    }
            return true;
}
    
$(document).ready(function() {
    $('#registrationForm').bootstrapValidator();
    $('#changePasswordForm').bootstrapValidator();

     $("#UpdateRegisterSubmit").click(function() {        
    
              var provinceDropDown = document.getElementById("provinces");
              var cityDropDown = document.getElementById("cities");

              if(provinceDropDown.value==0)
              {
                alert("Select Province");
                provinceDropDown.value="";
                return false;
              }
              
              
              if(cityDropDown!=null)
              {
                if(cityDropDown.value==0)
                {
                  alert("Select City");
                  cityDropDown.value="";
                  return false;
                }
              }

        }); 

  
    $("#RegisterSubmit").click(function() {
    //var $btn = $(this);
    //$btn.button('loading');
    //contactForm.clearErrors();

        
    var valPhone = document.getElementById("valPhone");
    var code = document.getElementById("code");
    var codeVerification_res = document.getElementById("codeVerification_res");
    var codeVerification = document.getElementById("codeVerification");


    
    var firstName = document.getElementById("firstname");
    var lastName = document.getElementById("lastname");
    var phoneNo = document.getElementById("phone_no");
    var emailReg = document.getElementById("email");
    var regPassword = document.getElementById("password");
    var regPasswordConfirm = document.getElementById("confirmPassword");
    var provinceDropDown = document.getElementById("provinces");
    var cityDropDown = document.getElementById("cities");
    var regPostalCodePartOne = document.getElementById("postcod");
    var regPostalCodePartTwo = document.getElementById("postcod1");
    var streetNumber = document.getElementById("street_nro");
    var streetName = document.getElementById("street_name");

    var phoneNumber = document.getElementById("secondphone_no");          

    if(firstName.value.length==0)
    {
        alert("Enter firstName");
        firstName.focus();
        return false;
    }
    else if(lastName.value.length==0)
    {
        alert("Enter lastName");
        lastName.focus();
        return false;
    }
    else if(phoneNo.value.length==0)
    {
        alert("Enter phone");
        phoneNo.focus();
        return false;
    }
    else if(phoneNo.value.length<10)
    {
        alert("Enter a 10-digit phone number");
        phoneNo.focus();
        return false;
    }
    else if(emailReg.value.length==0)
    {
        alert("Enter email");
        emailReg.focus();
        return false;
    }
    else if(regPassword.value.length==0)
    {
        alert("Enter Password");
        regPassword.focus();
        return false;
    }
    else if(regPasswordConfirm.value.length==0)
    {
        alert("Enter password confirmation");
        regPasswordConfirm.focus();
        return false;
    }
    else if(regPassword.value != regPasswordConfirm.value)
    {
        alert("The password and its confirmation are not the same");
        regPasswordConfirm.focus();
        return false;
    }
    else if(regPostalCodePartOne.value.length==0)
    {
        alert("Enter postal code");
        regPostalCodePartOne.focus();
        return false;
    }
    else if(regPostalCodePartTwo.value.length==0)
    {
        alert("Enter postal code");
        regPostalCodePartTwo.focus();
        return false;
    }
    else if(streetNumber.value.length==0)
    {
        alert("Enter street number");
        streetNumber.focus();
        return false;
    }
    else if(streetName.value.length==0)
    {
        alert("Enter street name");
        streetName.focus();
        return false;
    }
    else if(valPhone.value==1 && phoneNumber.value.length>=0 && phoneNumber.value.length<=9)
    {
        alert("Enter phone number for verification code");
        phoneNumber.focus();
        return false;
    }
    else if(valPhone.value==1 && codeVerification_res.value.length==0)
    {
        alert("Enter verification code");
        phoneNumber.focus();
        return false;
    }
    else if(valPhone.value==1 && code.value!=codeVerification.value && code.value!="BOOM")
        {            
                alert("Incorrect verification code");
                code.focus();
                return false;
        }
        else if(provinceDropDown.value==0)
          {
            alert("Select Province");
            provinceDropDown.value="";
            return false;
          }
          else if(cityDropDown!=null)
          {
            if(cityDropDown.value==0)
            {
              alert("Select City");
              cityDropDown.value="";
              return false;
            }
          }

    });   

});
