function SymError()
        {
          return true;
        }
        
        window.onerror = SymError;
        
        var SymRealWinOpen = window.open;
        
        function SymWinOpen(url, name, attributes)
        {
          return (new Object());
        }
window.open = SymWinOpen;

/*** function that will create the request object ***/
  var request = null;
  function createRequest() 
  {
     try 
     {
        request = new XMLHttpRequest();
     } 
     catch (trymicrosoft) 
     {
        try 
        {
           request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (othermicrosoft) 
        {
           try 
           {
              request = new ActiveXObject("Microsoft.XMLHTTP");
           } 
           catch (failed) 
           {
              request = null;
           }
       }
    }
  }
/** global variables: to be used in submitChange and updatePage **/
var cd_new_fname = null;
var cd_new_lname = null;
var cd_new_street = null;
var cd_new_city = null;
var cd_new_province = null;
var cd_new_country = null;
var cd_new_postal = null;
var cd_new_phone = null;
var cd_new_email = null;
var cd_new_password = null;

/**** function that submits the new informations***/
 function submitChange() 
 {
     createRequest();
		 //check request object
     if (request == null)
     {
         alert("Could not create XMLHttpRequest Object");
         return;  //end the function
     }

     //get data from the form
		var cd_new_password1=  document.getElementById("password1").value;
		var cd_new_password2=  document.getElementById("password2").value;
		 if(cd_new_password1 != cd_new_password2){
		 		alert("You should re-type the  same password");
        return;
				 }  //end the function
				 else{
				 
         cd_new_fname = document.getElementById("firstname").value;
		 
         cd_new_lname = document.getElementById("lastname").value;
		 
		 cd_new_address = document.getElementById("address").value;
		 
		 cd_new_city = document.getElementById("city").value;
		
		 cd_new_province = document.getElementById("province").value;
		 
		 cd_new_country = document.getElementById("selectCountry").value;
		 
		 cd_new_postal = document.getElementById("codepostal").value;
		 
		 cd_new_phone = document.getElementById("phone").value;
		
		 cd_new_email= document.getElementById("email").value;
		 
		 cd_new_password = document.getElementById("password1").value;
		 
     //validate the data
		 var email_pattern=/^[a-z0-9][a-z0-9\.\-_]*@[a-z0-9][a-z0-9\.\-_]*\.[a-z]{2,6}$/;
		 var phone_pattern=/^[0-9]*$/;
		 //var phone_pattern=/^[0-9]{3}[ -]*[0-9]{3}[ -]*[0-9]{4}$/;
		 
     if (cd_new_email =="" || cd_new_password =="" || cd_new_fname=="" ||cd_new_lname=="" || cd_new_address=="" || cd_new_city=="" ||cd_new_province=="" ||cd_new_country=="" || cd_new_postal=="" ) 
     {
	     alert("All fields must be filled");
	     return;
     }
		  if(!email_pattern.test(cd_new_email))
			{
			 alert("Your email address is not valid");
				return;
			}
			if(!phone_pattern.test(cd_new_phone))
			{
			 if(cd_new_phone==""){}
			 else{
			  alert("You phone number should  be at least seven numbers");
				return;
			}
			}

     //prepare connection (POST)
     request.open("POST", "add_client.php", true);
		 
     //set the content type 
     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

     //Tell the browser which function to call when the state of the request changes 
     request.onreadystatechange = updatePage;
		
     //encode the form data and put it into variable (to be sent to the server)
     var data = " cd_new_fname=" + encodeURIComponent( cd_new_fname) +
                "&cd_new_lname=" + encodeURIComponent(cd_new_lname)+
								"&cd_new_address=" + encodeURIComponent(cd_new_address)+
								"&cd_new_city=" + encodeURIComponent(cd_new_city)+
								"&cd_new_province=" + encodeURIComponent(cd_new_province)+
								"&cd_new_country=" + encodeURIComponent(cd_new_country)+
								"&cd_new_postal=" + encodeURIComponent(cd_new_postal)+
								"&cd_new_phone=" + encodeURIComponent(cd_new_phone)+
								"&cd_new_email=" + encodeURIComponent(cd_new_email)+
								"&cd_new_password=" + encodeURIComponent(cd_new_password);
     //Send the request
     request.send(data);
		
     //reset the message
		 document.getElementById("password1").value = "";
		 document.getElementById("password2").value = "";
         document.getElementById("fname").value = "";
		 document.getElementById("lname").value = "";
		 document.getElementById("address").value = "";
		 document.getElementById("city").value = "";
		 document.getElementById("province").value = "";
		 document.getElementById("selectCountry").value = "";
		 document.getElementById("codepostal").value = "";
		 document.getElementById("phone").value = "";
		 document.getElementById("email").value = "";
    }
	}

  /*** the function that reads the response and updates the page ***/
  function updatePage() 
  {
     //check if a response has been received
     if (request.readyState == 4) 
     {
         // get the response
         var response = request.responseText;
				 var okPattern = /^ok/;
				 //alert ("\'" + response + "\'");
				  if (okPattern.test(response)) // != "ok")
         {
	         //if everything is ok 
					 window.location='login.php';
					 
	      }else
        {
				 	 //if there is a problem
           document.getElementById("msg").innerHTML = "We couldn't save your data. This Email address already exists in our database";
        }
     }
  }
