// JavaScript Document


/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

//-------------------------------------------------------------
//---------------------------------------------------------

function getprojectdata() //function reads the data in for projects and passes it load_projects function to show on screen
{
	http = createRequestObject();
	http.open('get','processrequests.php?action=getProjects');
	http.onreadystatechange=load_projects;
	http.send(null);
}

function load_projects() //this handles loading the data into the allprojects div
{
	if(http.readyState==4){
		document.getElementById('allprojects').innerHTML = http.responseText;
		hideEditForm();	
	}
	
}

function addProject()
{
	http = createRequestObject();
	//get variables from the form
	form = document.getElementById('addprojectform');
	name = form.projectname.value;
	biddate = form.biddate.value;
	estimatorname = form.estimatorname.value;
	estimatorphone = form.estimatorphone.value;
	estimatoremail = form.estimatoremail.value;
	projectnote = form.projectnote.value;
	summary = form.txtsummary.value;
	form.reset();
	//run the http request
	http.open('get','processrequests.php?action=addProject&projectname='+name+'&biddate='+biddate+'&estimatorname='+estimatorname+'&estimatorphone='+estimatorphone+'&estimatoremail='+estimatoremail+'&projectnote='+projectnote+'&txtsummary='+summary);
	http.onreadystatechange=getprojectdata;
	http.send(null);
}

function deleteproject(bidid)
{
	
	var answer = confirm("Delete this project?");
	if (answer){
		http = createRequestObject();
		http.open('get','processrequests.php?action=delproject&bidid='+bidid);
		http.onreadystatechange=getprojectdata;
		http.send(null);
	}else{
		alert("Cancelled");
	}

}

function editproject(bidid)
{
	http = createRequestObject();
	http.open('get','processrequests.php?action=editproject&bidid='+bidid);
	http.onreadystatechange=showeditform;
	http.send(null);	
}

function showeditform()
{
	if(http.readyState == 4)
	{
		mydiv = document.getElementById('editdiv');
		mycell = document.getElementById('editcell');
		mydiv.innerHTML = http.responseText;
		mycell.style.backgroundColor = "#DADADA";
	}
}

function hideEditForm()
{
		mydiv = document.getElementById('editdiv');
		mycell = document.getElementById('editcell');
		mycell.style.backgroundColor = "white";
		mydiv.innerHTML = '';
}

function deletefile(fileid, bidid)
{
	http = createRequestObject();
	http.open('get','processrequests.php?action=deletefile&bidid='+bidid+'&fileid='+fileid);
	http.onreadystatechange=function(){editProject(bidid);};
	http.send(null);	
}

function getAssociatedFiles()
{
	if(http.readyState == 4)
	{
		mydiv = document.getElementById('currentfiles');
		mydiv.innerHTML = http.responseText;
		
	}
}

function getSummary(bidid)
{
	http = createRequestObject();
	http.open('get','processrequests.php?action=getSummary&bidid='+bidid);
	http.onreadystatechange=function(){showSummary(bidid);};
	http.send(null);	
}

function showSummary(divid)
{
	if(http.readyState == 4)
	{
		mydiv = document.getElementById(divid);
		mydiv.innerHTML = http.responseText;
		document.getElementById('hidelink_'+divid).style.visibility = 'visible';
	}
}

function getRF(bidid)
{
	http = createRequestObject();
	http.open('get','processrequests.php?action=getRF&bidid='+bidid);
	http.onreadystatechange=function(){showRF(bidid);};
	http.send(null);
}

function showRF(bidid)
{
	if(http.readyState == 4)
	{
		document.getElementById(bidid+'_response').innerHTML = http.responseText;
		//alert(http.responseText);
	}
}

function changeRE()
{
	form = document.getElementById('changemail');
	newemail = form.newemail.value;
	http = createRequestObject();
	http.open('get','processrequests.php?action=changeRE&newemail='+newemail);
	http.onreadystatechange=function(){renewForm('1');};
	http.send(null);
}

function changePK()
{
	form = document.getElementById('changePassKey');
	newpasskey = form.newpasskey.value;
	http = createRequestObject();
	http.open('get','processrequests.php?action=changePK&newpasskey='+newpasskey);
	http.onreadystatechange=function(){renewForm('2');};
	http.send(null);

}

function renewForm(num)
{
	mydiv = document.getElementById('changeFieldDiv'+num);
	buttondiv = document.getElementById('changeButtonDiv'+num);
	
	mydiv.innerHTML = http.responseText;
	buttondiv.innerHTML = '<input type=\"button\" name=\"Submit2\" value=\"Change\" style=\"font-size:9px;\" onclick=\'loadChangeForm('+num+');\'>';
}