//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
var frmName
var rndm
//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
  
   var numRandom =  Math.random()


   
if (frmName.id == "frmCaptcha"){   
img = document.getElementById('imgCaptcha'); 
  document.getElementById('result').innerHTML = receiveReq.responseText;	
   img.src = 'create_image.php?' + numRandom;
   //Get a reference to CAPTCHA image  if (frmName.id == "frmQuote")

  } else{
	  //Change the image
  img2 = document.getElementById('imgCaptcha2'); 
  document.getElementById('result2').innerHTML = receiveReq.responseText;
   img2.src = 'create_image.php?' + numRandom;
   }  
     
	  


   
   rndBuild()
 }
}



function rand( min, max ) { 
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}

function strlen(string){
    return string.length;
}

function substr( f_string, f_start, f_length ) {
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}


function count( mixed_var, mode ) {
    var key, cnt = 0;
 
    if( mode == 'COUNT_RECURSIVE' ) mode = 1;
    if( mode != 1 ) mode = 0;
 
    for (key in mixed_var){
        cnt++;
        if( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
            cnt += count(mixed_var[key], 1);
        }
    }
 
    return cnt;
}


function randomCaptcha(){

	var code = '';
	nchar = 5;							// number of characters in image
	for(i=1;i<=nchar;i++){
		charOnumber = rand(1,2);
		if(charOnumber == 1){
			chars = 'abefhkmnrvwx';	// custom used characters
			n = strlen(chars)-1;
			x = rand(1,n);
			char = substr(chars,x,1);
			code = code+char;
		} else {
			//$number = rand(3,7);
			var numbers=new Array(1,2,3,4,7);	// custom used numbers
			n = count(numbers)-1;
			number = numbers[rand(1,n)];
			code = code+number;
		}
	}
	return code;
}



function rndBuild(){
	//alert("FRAFRA PLA");
var randCap = randomCaptcha();
rndm = randCap;

if(document.getElementById('imgCaptcha')){
img = document.getElementById('imgCaptcha'); 
img.src = 'create_image.php?key='+rndm 
}
try {
if(document.getElementById('imgCaptcha2')) {
img2 = document.getElementById('imgCaptcha2'); 
img2.src = 'create_image.php?key='+rndm 
}
} 
catch(e){}
//theForm.id + "=" + encodeURIComponent(theForm.id) + "&" + theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value);
// makeRequest(urlReq, postStrReq);	


}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 
 var url = 'captcha.php';
 frmName = theForm;
 
 //Set up the parameters of our AJAX call
if (theForm.id == "frmCaptcha"){
	
 var postStr = theForm.id + "=" + encodeURIComponent( theForm.id ) + "&" + theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value ) + "&" + theForm.name.name + "=" + encodeURIComponent( theForm.name.value ) + "&" + theForm.name2.name + "=" + encodeURIComponent( theForm.name2.value )  + "&" + theForm.dayphone.name + "=" + encodeURIComponent( theForm.dayphone.value ) + "&" + theForm.email.name + "=" + encodeURIComponent( theForm.email.value )  + "&" + theForm.comp_name.name + "=" + encodeURIComponent( theForm.comp_name.value ) + "&" + theForm.track.name + "=" + encodeURIComponent( theForm.track.value ) + "&key="+rndm;

}
 

if (theForm.id == "frmQuote"){
var postStr = theForm.id + "=" + encodeURIComponent(theForm.id) + "&" + theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value) + "&" + theForm.name.name + "=" + encodeURIComponent( theForm.name.value ) + "&" + theForm.name2.name + "=" + encodeURIComponent( theForm.name2.value)  + "&" + theForm.dayphone.name + "=" + encodeURIComponent( theForm.dayphone.value) + "&" + theForm.email.name + "=" + encodeURIComponent( theForm.email.value)  + "&" + theForm.movingDate.name + "=" + encodeURIComponent( theForm.movingDate.value)+ "&" + theForm.Origin_Street.name + "=" + encodeURIComponent( theForm.Origin_Street.value)+ "&" + theForm.Origin_City.name + "=" + encodeURIComponent( theForm.Origin_City.value)+ "&" + theForm.Origin_State.name + "=" + encodeURIComponent( theForm.Origin_State.value)+ "&" + theForm.Origin_Zip.name + "=" + encodeURIComponent( theForm.Origin_Zip.value)+ "&" + theForm.Destination_Street.name + "=" + encodeURIComponent( theForm.Destination_Street.value)+ "&" + theForm.Destination_City.name + "=" + encodeURIComponent( theForm.Destination_City.value)+ "&" + theForm.Destination_State.name + "=" + encodeURIComponent( theForm.Destination_State.value)+ "&" + theForm.Destination_Zip.name + "=" + encodeURIComponent(theForm.Destination_Zip.value)+ "&" + theForm.Auto_Make.name + "=" + encodeURIComponent(theForm.Auto_Make.value)+ "&" + theForm.Auto_Model.name + "=" + encodeURIComponent(theForm.Auto_Model.value)+ "&" + theForm.Auto_Year.name + "=" + encodeURIComponent(theForm.Auto_Year.value)+ "&" + theForm.Auto_Condition.name + "=" + encodeURIComponent(theForm.Auto_Condition.value)+ "&" + theForm.Auto_Type.name + "=" + encodeURIComponent(theForm.Auto_Type.value)+ "&" + theForm.Auto_Notes.name + "=" + encodeURIComponent(theForm.Auto_Notes.value)+ "&key="+rndm;
 

 
}

if (theForm.id == "frmContact"){
 alert("______________frmContact___+++++____."+theForm.id);
}
  //Call the function that initiate the AJAX request
 makeRequest(url, postStr);

} 