// JavaScript Document AJAX

function getHTTPObject()
{
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else 
		{
			alert("Your browser does not support AJAX.");
			return null;
		}
}

function setOutput(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('list').innerHTML = httpObject.responseText;
	}
}

function showMatches(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('matchList').style.display = 'block';
		document.getElementById('matchList').style.zIndex = '10';
		document.getElementById('matchList').innerHTML = httpObject.responseText;
	}
}
function showAddress(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('similarAddress').style.display = 'block';
		document.getElementById('similarAddress').style.zIndex = '10';
		document.getElementById('similarAddress').innerHTML = httpObject.responseText;
	}
}

function exportIdList(idList)
{

	var mypostrequest=new ajaxRequest()
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
	   document.getElementById("result").innerHTML=mypostrequest.responseText
	  }
	  else{
	   alert("An error has occured making the request")
	  }
	 }
	}
	var parameters="idList="+idList;
	mypostrequest.open("POST", "getCsv.php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}

function checkAddress()
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		var address = document.getElementById('address').value;
		httpObject.open("GET", "verifyAddress.php?address="+address, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showAddress;
	}
}

function showList()
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		var pageName = document.getElementById('page_name').value;
		
			httpObject.open("GET", "getListOfNames.php?page_name="+pageName+"&inputText="+document.getElementById('inputText').value, true);
		httpObject.send(null);
		document.getElementById('list').innerHTML = '<div class="success">Working! Please wait</div>';
		httpObject.onreadystatechange = setOutput;

	}

}
function showResults(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('allMiles').innerHTML = httpObject.responseText;
	}
}

function showSuccess(id){
		alert('Updated');
		document.getElementById('result_'+id).innerHTML =  httpObject.responseText;
}

function similarDate()
{
	var date = document.getElementById('datepicker').value;
	var mdy = date.split("/");
	if (mdy[2] == 2009)
	{
		if (mdy[0]  <= 3)
		{
				document.getElementById('car_id').value = 1;
		}
		else
		{
			document.getElementById('car_id').value = 2;
		}
	}
	else
	{
			document.getElementById('car_id').value = 2;
	}
	
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "getSimilar.php?date="+date, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}
function lastMile()
{
	var destination = document.getElementById('destination').value;
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "showLastMile.php?destination="+destination, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showLastMile;
	}
}
function addAnotherFtp(customer_id)
{
	var newHtml = 'Username:<input type="text" id="username"> Password<input type="text" id="password">';
	document.getElementById('addFtp').innerHTML = newHtml;
	document.getElementById('addFtpLink').innerHTML = '<button onMouseUp="addNewFtp('+customer_id+');">Submit</button>';
}

function addNewFtp(customer_id)
{
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "addNewFtp.php?customer_id="+customer_id+"&username="+username+"&password="+password, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showFtpList;

	}
}

function removeFtp(ftp_account_id, customer_id)
{
	document.getElementById('ftp_'+ftp_account_id).innerHTML = 'Are you sure? <a href="javascript: void(0);" onClick="deleteFtp('+ftp_account_id+', '+customer_id+');">Yes</a>  || <a href="customer_history.php?customer_id='+customer_id+'"> No</a>  ';
}

function deleteFtp(ftp_account_id, customer_id)
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "deleteFtp.php?customer_id="+customer_id+"&ftp_account_id="+ftp_account_id, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showFtpList;

	}
}

function addAnotherMysql(customer_id)
{
	var newHtml = 'Mysql Username:<input type="text" id="mysql_username" size="10"><br> Mysql Password<input type="text" id="mysql_password" size="10"><br> Mysql Privileges<input type="text" id="mysql_privileges" size="10">';
	document.getElementById('addMysql').innerHTML = newHtml;
	document.getElementById('addMysqlLink').innerHTML = '<button onMouseUp="addNewMysql('+customer_id+');">Submit</button>';
}

function addNewMysql(customer_id)
{
	var mysql_username = document.getElementById('mysql_username').value;
	var mysql_password = document.getElementById('mysql_password').value;
	var mysql_privileges = document.getElementById('mysql_privileges').value;
	if( (mysql_username != '') && (mysql_password != '') && (mysql_privileges != ''))
	{
		httpObject = getHTTPObject();
		if (httpObject != null) 
		{
			httpObject.open("GET", "addNewMysql.php?customer_id="+customer_id+"&mysql_username="+mysql_username+"&mysql_password="+mysql_password+"&mysql_privileges="+mysql_privileges, true);
			httpObject.send(null);
			httpObject.onreadystatechange = showMysqlList;

		}
	}
	else
	{
		alert('You forgot something');
	}
}

function removeMysql(mysql_account_id, customer_id)
{
	document.getElementById('mysql_'+mysql_account_id).innerHTML = 'Are you sure? <a href="javascript: void(0);" onClick="deleteMysql('+mysql_account_id+', '+customer_id+');">Yes</a>  || <a href="customer_history.php?customer_id='+customer_id+'"> No</a>  ';
}

function deleteMysql(mysql_account_id, customer_id)
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "deleteMysql.php?customer_id="+customer_id+"&mysql_account_id="+mysql_account_id, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showMysqlList;

	}
}


function showMysqlList(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('mysqlList').innerHTML = httpObject.responseText;
	}
}

function autoSuggest()
{
	var destination = document.getElementById('destination').value;
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "autoSuggest.php?destination="+destination, true);
		httpObject.send(null);
		httpObject.onreadystatechange = autoSuggestList;
	}
}

function autoSuggestList(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('auto_suggest_div').innerHTML = httpObject.responseText;
	}
}
function showLastMile(){

	if(httpObject.readyState == 4)
	{
		document.getElementById('showLastMile').value = httpObject.responseText;
	}
}

function showAllMiles()
{
	var startYear = document.getElementById('startYear').value;
	var endYear = document.getElementById('endYear').value;
	var business = document.getElementById('business').checked;
	
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "showAllMiles.php?startYear="+startYear+"&endYear="+endYear+"&business="+business, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showResults;
	}
}

function editPayment(id)
{
	var amount = document.getElementById('amount_'+id).value;
	var date_paid = document.getElementById('date_paid_'+id).value;
	var type = document.getElementById('type_'+id).value;
	var check_number = document.getElementById('check_number_'+id).value;
	var accounting_id = document.getElementById('accounting_id_'+id).value;
	
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "updatePayment.php?accounting_id="+accounting_id+"&amount="+amount+"&date_paid="+date_paid+"&type="+type+"&check_number="+check_number, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showSuccess(id);
	}
}
function deleteSelectedCustomer(customer_id)
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "deleteCustomer.php?customer_id="+customer_id, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}
function checkName()
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		var last_name = document.getElementById('last_name').value;
		httpObject.open("GET", "checkLastName.php?last_name="+last_name, true);
		httpObject.send(null);
		httpObject.onreadystatechange = showMatches;
	}
}

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}

function loadSearchOptions()
{
	var mypostrequest=new ajaxRequest()
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
	   document.getElementById("result").innerHTML=mypostrequest.responseText
	  }
	  else{
	   alert("An error has occured making the request")
	  }
	 }
	}
	//var subject=encodeURIComponent(document.getElementById("subject").value)
	//var message=encodeURIComponent(document.getElementById("message").value)
	//var parameters="customer_id="+customer_id+"&subject="+subject+"&message="+message
	var parameters="";
	mypostrequest.open("POST", "load_search_options.php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}

function searchQuery()
{
	var mypostrequest=new ajaxRequest()
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
	   document.getElementById("result").innerHTML=mypostrequest.responseText
	  }
	  else{
	   alert("An error has occured making the request")
	  }
	 }
	}
	var ending_date=encodeURIComponent(document.getElementById("ending_date").value)
	var starting_date=encodeURIComponent(document.getElementById("starting_date").value)
	var order_by_asc=encodeURIComponent(document.getElementById("order_by_asc").value)
	var order_by_desc=encodeURIComponent(document.getElementById("order_by_desc").value)
	var event_id=encodeURIComponent(document.getElementById("event_id").value)
	var parameters="ending_date="+ending_date+"starting_date="+starting_date+"order_by_asc="+order_by_asc+"order_by_desc="+order_by_desc
	mypostrequest.open("POST", "searchQuery.php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}




// END invoices/proposals

var httpObject = null;
