var my_http_request = false;
var my_http_request_2 = false;


function makePOSTRequest(url, parameters) {

    my_http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
         my_http_request = new XMLHttpRequest();
         if (my_http_request.overrideMimeType) {
            my_http_request.overrideMimeType('text/html');
         }
    }else if (window.ActiveXObject) { // IE
         try {
            my_http_request = new ActiveXObject("Msxml2.XMLHTTP");
         }catch(e){
            try {
               my_http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){}
         }
    }
    if (!my_http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
    }

    my_http_request.onreadystatechange = alertContents;
    my_http_request.open('POST', url, true);
    my_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    my_http_request.setRequestHeader("Content-length", parameters.length);
    my_http_request.setRequestHeader("Connection", "close");
    my_http_request.send(parameters);
}

function alertContents() {
    //alert('alert');
    if (my_http_request.readyState == 4) {
	if (my_http_request.status == 200) {
         //   alert("haho");
            var result = my_http_request.responseText;
           // alert(my_http_request.responseText);
            document.getElementById('my_maincontent').innerHTML = result;
	}else{
            alert('There was a problem with the request.');
        }
    }
}


function makePOSTRequest_no(url, parameters) {

    my_http_request_no = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
         my_http_request_no = new XMLHttpRequest();
         if (my_http_request_no.overrideMimeType) {
            my_http_request_no.overrideMimeType('text/html');
         }
    }else if (window.ActiveXObject) { // IE
         try {
            my_http_request_no = new ActiveXObject("Msxml2.XMLHTTP");
         }catch(e){
            try {
               my_http_request_no = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){}
         }
    }
    if (!my_http_request_no) {
         alert('Cannot create XMLHTTP instance');
         return false;
    }

    my_http_request_no.onreadystatechange = alertContents_no;
    my_http_request_no.open('POST', url, true);
    my_http_request_no.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    my_http_request_no.setRequestHeader("Content-length", parameters.length);
    my_http_request_no.setRequestHeader("Connection", "close");
    my_http_request_no.send(parameters);
}

function alertContents_no() {
    //alert('alert');
    if (my_http_request_no.readyState == 4) {
	if (my_http_request_no.status == 200) {
         //   alert("haho");
            var result = my_http_request_no.responseText;
            //document.getElementById('my_maincontent').innerHTML = result;
	}else{
            alert('There was a problem with the request.');
        }
    }
}






function get_1(obj) {
    //alert('itt: '+document.getElementById("first_name").value);
      var poststr = "first_name=" + escape(encodeURI( document.getElementById("first_name").value )) +
                    "&last_name=" + escape(encodeURI( document.getElementById("last_name").value ))+
	            "&address=" + escape(encodeURI( document.getElementById("address").value ))+
	            "&state=" + escape(encodeURI( document.getElementById("state").value ))+
                    "&zip=" + escape(encodeURI( document.getElementById("zip").value ))+
                    "&email=" + escape(encodeURI( document.getElementById("email").value ))+
                    "&day_phone=" + escape(encodeURI( document.getElementById("day_phone").value ))+
                    "&work_phone=" + escape(encodeURI( document.getElementById("work_phone").value ))+
	            "&usr_note_1=" + escape(encodeURI( document.getElementById("usr_note_1").value ))+
                    "&city=" + escape(encodeURI( document.getElementById("city").value ))+
		    "&B_name="+escape(encodeURI(document.getElementById("B_name").value))+
    		    "&B_company="+escape(encodeURI(document.getElementById("B_company").value))+
    		    "&B_principal="+escape(encodeURI(document.getElementById("B_principal").value))+
    		    "&B_state="+escape(encodeURI(document.getElementById("B_state").value))+
    		    "&B_zip="+escape(encodeURI(document.getElementById("B_zip").value))+
    		    "&B_address="+escape(encodeURI(document.getElementById("B_address").value))+
    		    "&B_email="+escape(encodeURI(document.getElementById("B_email").value))+
    		    "&B_phone="+escape(encodeURI(document.getElementById("B_phone").value))+
		    "&B_city="+escape(encodeURI(document.getElementById("B_city").value))+
		    "&B_license="+escape(encodeURI(document.getElementById("B_license").value));
window.scroll(0,0);
//      makePOSTRequest('referral_form_1_ajax', poststr);

document.getElementById('my_maincontent').innerHTML="<img style='margin-top:10px;margin-left:290px;width:32px;height:32px;margin-bottom:18px;' src='referral_anim.gif'/><br><div style='font-size:16px; font-family: arial, sans-serif; font-weight:bold;display:inline;margin-left:177px;margin-bottom:36px;'>  Buyer Referral Agreement Loading </div>";
//" <img style='margin-top:30px;margin-left:240px' src='referral_anim.gif'/>";

      makePOSTRequest('referral_pdfmailer_ajax',poststr);
//"&select=" + encodeURI( document.getElementById("select").value )+
}


function my_check_1(obj){
    var form=document.getElementById("myform");
    var msg = '';
    var B_msg = '';
    var blame = "Please enter ";
    var emOption = 'Please select';

    var first_name=document.getElementById("first_name").value;
    var last_name=document.getElementById("last_name").value;
    var address=document.getElementById("address").value;
    var state=document.getElementById("state").value;
    var zip=document.getElementById("zip").value;
    var email=document.getElementById("email").value;
    var day_phone=document.getElementById("day_phone").value;
    var work_phone=document.getElementById("work_phone").value;
    //    var select=document.getElementById("select").value;
    var usr_note_1=document.getElementById("usr_note_1").value;
    var city=document.getElementById("city").value;


    if (first_name== '') msg += blame + " First Name\n";
    if (last_name== '') msg += blame + " Last Name\n";
    ///if (address== '') msg += blame + " Address\n";
    if (city== '') msg += blame + " City\n";
    //if (state== '') msg += emOption + " State\n";
    //if (zip== '') msg += blame + " Zip Code\n";
    if (email== '') msg += blame + " E-Mail Address\n";
    //if (day_phone=="" && work_phone=="") msg+=blame+" Day Phone or Work Phone\n";
    if (work_phone=="") msg+=blame+" Work Phone\n";

    if (msg != ''){
    	msg="Client information:\n"+msg;
	}

    var B_name=document.getElementById("B_name").value;
    var B_license=document.getElementById("B_license").value;
    var B_company=document.getElementById("B_company").value;
    var B_principal=document.getElementById("B_principal").value;
    var B_state=document.getElementById("B_state").value;
    var B_zip=document.getElementById("B_zip").value;
    var B_address=document.getElementById("B_address").value;
    var B_email=document.getElementById("B_email").value;
    var B_phone=document.getElementById("B_phone").value;
    //var work_phone=document.getElementById("work_phone").value;
    //    var select=document.getElementById("select").value;
    var B_city=document.getElementById("B_city").value;


    if (B_name== '') B_msg += blame + " Broker/Agent Name\n";
    if (B_license== '') B_msg += blame + " Broker License\n";
    if (B_company== '') B_msg += blame + " Brokerage Company\n";
    if (B_principal== '') B_msg += blame + " Pincipal Broker\n";
    if (B_address== '') B_msg += blame + " Mailing Address\n";
    if (B_city== '') B_msg += blame + " City\n";
    if (B_state== '') B_msg += emOption + " State\n";
    if (B_zip== '') B_msg += blame + " Zip Code\n";
    if (B_phone=="" ) B_msg+=blame+" Office Phone\n";
    if (B_email== '') B_msg += blame + " E-Mail Address\n";

    if (msg != ''){
    	B_msg="\n"+"Broker information:\n"+B_msg;
	}

    msg=msg+B_msg;

    if (msg != ''){
	alert(msg);
	return false;
    }else{
    	get_1(); //echo form_2
	//form.submit();
    }
}



function test_2(){

//if (window.document.order.elements['NewYork[]']) alert("1. letezik");
//if (window.document.order.elements['Brooklyn[]']) alert("2. letezik");

for (var i=0; i<window.document.order.elements['NewYork[]'].length; ++i){
 	if (window.document.order.elements['NewYork[]'][i].checked)  alert('Yup, cat ' + window.document.order.elements['NewYork[]'][i].value + ' is checked!');
//else alert('Nope, cat ' + f['cat[]'][i].value + ' is not checked.');
}

}



function get_2form(obj) {
    //alert('itt: '+document.getElementById("first_name").value);
    var ff=window.document.order;
    var arrstr="";
    for (var i=0; i<ff.elements['NewYork[]'].length; ++i){
 	if (ff.elements['NewYork[]'][i].checked) {
        arrstr=arrstr+"&NewYork[]="+escape(encodeURI(ff.elements['NewYork[]'][i].value));
        }
    }
    for (var i=0; i<ff.elements['Brooklyn[]'].length; ++i){
 	if (ff.elements['Brooklyn[]'][i].checked) {
   //     arrstr=arrstr+"&"+escape(encodeURI("Brooklyn[]="))+escape(encodeURI(ff.elements['Brooklyn[]'][i].value));
        }
    }

    var new_poststr="&num_beds=" + escape(encodeURI( document.getElementById("num_beds").value )) +
                    "&Bathrooms=" + escape(encodeURI( document.getElementById("Bathrooms").value ))+
	            "&pets=" + escape(encodeURI( document.getElementById("pets").value ))+
	            "&bldg_type_id=" + escape(encodeURI( document.getElementById("bldg_type_id").value ))+
                    "&min_price=" + escape(encodeURI( document.getElementById("min_price").value ))+
                    "&max_price=" + escape(encodeURI( document.getElementById("max_price").value ))+
                    "&mortgage_free_prequalification=" + escape(encodeURI( document.getElementById("mortgage_free_prequalification").value ))+
                    "&usr_note=" + escape(encodeURI( document.getElementById("usr_note").value ))+
	            "&NewYorkCity=" + escape(encodeURI( document.getElementById("NewYorkCity").value ))+
                    "&BrooklynCity=" + escape(encodeURI( document.getElementById("BrooklynCity").value ))+
		    "&filename_ref=" + escape(encodeURI( document.getElementById("filename_ref").value ));


      var poststr = "first_name=" + escape(encodeURI( document.getElementById("first_name").value )) +
                    "&last_name=" + escape(encodeURI( document.getElementById("last_name").value ))+
	            "&address=" + escape(encodeURI( document.getElementById("address").value ))+
	            "&state=" + escape(encodeURI( document.getElementById("state").value ))+
                    "&zip=" + escape(encodeURI( document.getElementById("zip").value ))+
                    "&email=" + escape(encodeURI( document.getElementById("email").value ))+
                    "&day_phone=" + escape(encodeURI( document.getElementById("day_phone").value ))+
                    "&work_phone=" + escape(encodeURI( document.getElementById("work_phone").value ))+
	            "&usr_note_1=" + escape(encodeURI( document.getElementById("usr_note_1").value ))+
                    "&city=" + escape(encodeURI( document.getElementById("city").value ))+
		    "&B_name="+escape(encodeURI(document.getElementById("B_name").value))+
    		    "&B_company="+escape(encodeURI(document.getElementById("B_company").value))+
    		    "&B_principal="+escape(encodeURI(document.getElementById("B_principal").value))+
    		    "&B_state="+escape(encodeURI(document.getElementById("B_state").value))+
    		    "&B_zip="+escape(encodeURI(document.getElementById("B_zip").value))+
    		    "&B_address="+escape(encodeURI(document.getElementById("B_address").value))+
    		    "&B_email="+escape(encodeURI(document.getElementById("B_email").value))+
    		    "&B_phone="+escape(encodeURI(document.getElementById("B_phone").value))+
		    "&B_city="+escape(encodeURI(document.getElementById("B_city").value))+
		    "&B_license="+escape(encodeURI(document.getElementById("B_license").value));

//alert(arrstr+new_poststr);
window.scroll(0,0);
document.getElementById('my_maincontent').innerHTML="<img style='margin-top:10px;margin-left:290px;width:32px;height:32px;margin-bottom:18px;' src='referral_anim.gif'/><br><div style='font-size:16px; font-family: arial, sans-serif; font-weight:bold;display:inline;margin-left:177px;margin-bottom:36px;'>  Buyer Referral Agreement Loading </div>";
//" <img style='margin-top:30px;margin-left:240px' src='referral_anim.gif'/>";
makePOSTRequest('referral_form_2_ajax', poststr+arrstr+new_poststr);
//makePOSTRequest('referral_thanks.html',"");
      //makePOSTRequest('referral_form_2_ajax', arrstr+new_poststr);
  //    makePOSTRequest_no('referral_pdfmailer_ajax',poststr);
//"&select=" + encodeURI( document.getElementById("select").value )+
}


function textCounter(field, countfield, maxlimit) {
 //var	countid=document.getElementById(countfield);
 if (field.value.length > maxlimit) {
  field.value = field.value.substring(0, maxlimit);}
// otherwise, update 'characters left' counter
 //else
  //countid.innerHTML = maxlimit - field.value.length;
}






