function getHTTPObject() {
  var xmlhttp;
 
  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    
}
  return xmlhttp;

  
}
var http = getHTTPObject(); // We create the HTTP Object

/*
	Funtion Name=requestInfo 
	Param = url >> Url to call : id = Passing div id for multiple use ~ as a seprator for eg. div1~div2 :
	redirectPage >> if you like to redirect to other page once the event success then 
	the response text = 1 and the redirectPage not left empty
*/
var location_id
    function requestInfo(url,id,redirectPage) {      
		var temp=new Array();
			http.open("GET", url, true);
			http.onreadystatechange = function() {
				if (http.readyState == 4) {
				  if(http.status==200) {
			  		var results=http.responseText;
					if(redirectPage=="" || results!="1") {
						
						var temp=id.split("~"); // To display on multiple div 
						//alert(temp.length);
						var r=results.split("~"); // To display multiple data into the div 
						//alert(temp.length);
						if(temp.length>1) {
							for(i=0;i<temp.length;i++) {	
								//alert(temp[i]);
								document.getElementById(temp[i]).innerHTML=r[i];
							}
						} else {
							document.getElementById(id).innerHTML = results;
						}	
					} else {
						//alert(results);
						window.location.href=redirectPage;			
					}
				  } 
  				}
			};
			http.send(null);
       }

function IsNumeric(sText)
		{
   			var ValidChars = "0123456789.";
   			var IsNumber=true;
   			var Char;

 
   			for (i = 0; i < sText.length && IsNumber == true; i++) 
      		{ 
      			Char = sText.charAt(i); 
      			if (ValidChars.indexOf(Char) == -1) 
        		 {
        		 IsNumber = false;
        		 }
      		}
  			 return IsNumber;
   
  		 }

/*
	Function Name= emptyValidation
	Desc = This function is used to validation for the empty field 
	Param fieldList = This arguments set as a string varialble. you just need to supply the textbox name
	if the textbox is multiple then supply with ~ separator for eg. username~password
*/
function emptyValidation(fieldList) {
	
		
		var field=new Array();
		field=fieldList.split("~");
		var counter=0;
		for(i=0;i<field.length;i++) {
/*			
			if(field[i]=='Beds')
			{
			if(!IsNumeric(document.getElementById('Beds').value))
			{
			alert("please enter integar");
			document.getElementById('Beds').focus();	
			return false;
			}
			
			//alert(field[i]);
			}*/
	 if(document.getElementById(field[i]).value=="") {
				//alert("what is wrong");
				document.getElementById(field[i]).style.backgroundColor="#FF0000";
				counter++;
				//alert(counter);
			}

			else {
				document.getElementById(field[i]).style.backgroundColor="#FFFFFF";	
			}
		}
		if(counter>0) {
				alert("The Field mark as red could not left empty");
				return false;
				
		}  else {
			return true;
		}
		
}

function init_table() {

		requestInfo('showTable.php?mode=list','showTable','');
		 document.getElementById('showTable').style.display="block";
		 document.getElementById('showList').style.display="none";
		  //document.getElementById('add_prop_block').style.display="none";
		   //document.getElementById('join_purdue').style.display="none";
		  
	}

	

	
	
	function save_data() {
			
			var Name = document.getElementById('Name').selectedIndex;
        	Name = document.getElementById('Name').options[Name].value;
			Location2=location_id;
		
  			var housingtype = document.getElementById('housingtype').selectedIndex;
        	housingtype = document.getElementById('housingtype').options[housingtype].value;

			
			var Beds=document.getElementById("Beds").value;
			var Baths=document.getElementById("Baths").value;
			var Sq_feet=document.getElementById("Sq_feet").value;
			var RentPerUnit=document.getElementById("RentPerUnit").value;
			var strclass=document.getElementById("strclass").value;
	        if(document.getElementById("SponserdListing").checked==true)
			var SponserdListing=document.getElementById("SponserdListing").value;
			else
			 var SponserdListing=0;
			//alert(SponserdListing);
			var AmountPaid=document.getElementById("AmountPaid").value;
		
		
			//var AmountDeducted=document.getElementById("AmountDeducted").value;
			var CurrentDeducution=document.getElementById("CurrentDeducution").value;
			//var Active=document.getElementById("Active").value;
			//var prev_id=document.getElementById("prev_id").value;

			var checkValidation=emptyValidation('Name~Location2~Beds~Baths~Sq_feet~RentPerUnit~strclass~SponserdListing~CurrentDeducution');
	
		if(checkValidation==true) {
			requestInfo('showTable.php?mode=save_new&Name='+Name+'&Location2='+Location2+'&Beds='+Beds+'&Baths='+Baths+'&Sq_feet='+Sq_feet+'&RentPerUnit='+RentPerUnit+'&strclass='+strclass+'&SponserdListing='+SponserdListing+'&AmountPaid='+AmountPaid+'&CurrentDeducution='+CurrentDeducution+'&housingtype='+housingtype,'showTable','');
		} 
	}
	
	function update_data() {
	
			var prev_id=document.getElementById("prev_id").value;
			var id=document.getElementById("pk_Listing_ID").value;
			Location2=location_id;
		
  			var housingtype = document.getElementById('housingtype').selectedIndex;
        	housingtype = document.getElementById('housingtype').options[housingtype].value;
		   
		   
		   var Beds=document.getElementById("Beds").value;
			var Beds=document.getElementById("Beds").value;
			
			var Baths=document.getElementById("Baths").value;

			var Sq_feet=document.getElementById("Sq_feet").value;
			var RentPerUnit=document.getElementById("RentPerUnit").value;
			var strclass=document.getElementById("strclass").value;

			if(document.getElementById("SponserdListing").checked==true)
			var SponserdListing=document.getElementById("SponserdListing").value;
			else
			 var SponserdListing=0;
			
			//alert(SponserdListing);
			var AmountPaid=document.getElementById("AmountPaid").value;
			//var AmountDeducted=document.getElementById("AmountDeducted").value;
			var CurrentDeducution=document.getElementById("CurrentDeducution").value;
			//var Active=document.getElementById("Active").value;
			
			var checkValidation=emptyValidation('Location2~Beds~Baths~Sq_feet~RentPerUnit~strclass~SponserdListing~CurrentDeducution');

		if(checkValidation==true) {
			requestInfo('showTable.php?mode=update_data&id='+id+'&Location2='+Location2+'&Beds='+Beds+'&Baths='+Baths+'&Sq_feet='+Sq_feet+'&RentPerUnit='+RentPerUnit+'&strclass='+strclass+'&SponserdListing='+SponserdListing+'&AmountPaid='+AmountPaid+'&CurrentDeducution='+CurrentDeducution+'&prev_id='+prev_id+'&housingtype='+housingtype,'showTable','');
		} 
		//alert("hii");
	}
	
	
function confirmLink(theLink)
{
   
    var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
    if (is_confirmed) {
        theLink.href += '';
    }

    return is_confirmed;
}

function getLocationValue()
{

var Name=document.getElementById("Name").value;

var urlpass = "templates/changeloctest.php?&Name="+Name;

// Set te random number to add to URL request

// Pass the login variables like URL variable
http.open('get', urlpass);
http.onreadystatechange = getinsertdata;
http.send(null);
return false;
	
}


function getinsertdata() 
{
	if(http.readyState == 4)
	{
		var getlocation = http.responseText;
		
		var arrr_loc=getlocation.split('~');
		
	   if(getlocation!='')
		{
		   
		   document.getElementById('Location2').value = arrr_loc[1];
		  location_id =arrr_loc[0];
		   
		   
		}
		else {
		
		  
		   document.getElementById('Location2').value='';
		   document.getElementById('Location2').title ='';
		
		}
		// else if login is ok show a message: "Site added+ site URL".
		//document.getElementById('insert_response').innerHTML =response ;
		
	}
}