///////////////////////////////////////////////////////////////////////////////////////////////////
// Guttsa site
// A.Kretschmer September 2009
////////////////////////////////////////////////////////////////////////////////////////////////////

var showForm = true;
var showTable = true;
var showDoc = true;
var showJob = true;
var formID;


function sendData() {

	objID="savemessage";
	theObject = document.getElementById(objID);
	theObject.style.visibility = "visible";
	theObject.style.height="auto";
	theObject.style.width="200";
	
	setQueryString();
	//alert(querystring);
	
	var url="../ajax/nlformaccept.php?id="+formID;
	httpRequest("POST",url,true,handleResponse,querystring);
}


function showHideForm(id,e) {

	objID="categoryform";
	formID=id;
	
	//alert("ID is" + formID);

	theObject = document.getElementById(objID);

	if (showForm == true) {
		//Show the category form
		theObject.style.visibility = "visible";
		theObject.style.height="450px";
		theObject.style.width="300px";

		var posx = 0;
		var posy = 0;

		posx=e.clientX + document.body.scrollLeft;
		posy=e.clientY + document.body.scrollTop;

		if (posy>200)
		{
			posy=posy-200;
		}

		posy=100;
		posx=300;

		theObject.style.left = posx + "px";
		theObject.style.top = posy + "px";
		
		var url = "../ajax/nlform.php?id=" + formID;
		httpRequest("GET",url,true,handleResponse);
		
		showForm = false;

	} else {

		//Hide theform

		theObject.innerHTML = "";
		theObject.style.visibility = "hidden";
		theObject.style.height="0px";
		theObject.style.width="0px";
		showForm = true;
	}//end if
}

function closeform() {
	theObject = document.getElementById("categoryform");
	theObject.style.visibility = "hidden";
	theObject.style.height="0px";
	theObject.style.width="0px";
	showForm = true;
}

function closesavemessage() {
	
	theObject = document.getElementById("savemessage");
	//theObject.innerHTML = "";
	theObject.style.visibility = "hidden";
	theObject.style.height="0px";
	theObject.style.width="0px";
	if (showForm == false)
	{
		closeform();
	}
}



///////////////////////////////////////////////////////////////////////////////////////
//Doc part
////////////////////////////////////////////////////////////////////////////////////////


function showHideDoc(id) {

	objID="docobj";
	theObject = document.getElementById(objID);
	//alert(id);
	if (showDoc== true) {
		//Show the category form
		theObject.style.visibility = "visible";
		theObject.style.height="600px";
		theObject.style.width="910px";

		var posx = 60;
		var posy = 5;

		theObject.style.left = posx + "px";
		theObject.style.top = posy + "px";

		var url = "../ajax/docs.php?id=" + id;
		httpRequest("GET",url,true,handleResponse);
		
		showDoc = false;

	} else {

		//Hide theform

		theObject.innerHTML = "";
		theObject.style.visibility = "hidden";
		theObject.style.height="0px";
		theObject.style.width="0px";
		showDoc = true;
	}//end if
}

function closedoc() {
	theObject = document.getElementById("docobj");
	theObject.style.visibility = "hidden";
	theObject.style.height="0px";
	theObject.style.width="0px";
	showDoc = true;
}


//////////////////////////////////////////////////////////////////////////////////////
//Calendar example
////////////////////////////////////////////////////////////////////////////////
//A variable used to disttinguish whether to open or close the calendar.
var showCalendar = true;
var showJobs = true;



function showHideCalendar() {

	objID="calendar";

	if (showCalendar == true) {
		//Show the calender
		document.getElementById("opencloseimg").src = "../images/minus.gif";
		httpRequest("GET", "../ajax/calendar.php", true, handleResponse);
		showCalendar= false;
	} else {
		//Hide the Calendar
		document.getElementById("opencloseimg").src = "../images/plus.gif";
		document.getElementById(objID).innerHTML = "";
		showCalendar= true;
	}//end if
}

function createform(e) {
	
	objID = "createtask";
	theObject = document.getElementById(objID);

	theObject.style.visibility = "visible";
	theObject.style.height="200px";
	theObject.style.width="200px";

	var posx = 0;
	var posy = 0;

	posx=e.clientX + document.body.scrollLeft;
	posy=e.clientY + document.body.scrollTop;

	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";

	//The location we are loading the page into.
	
	httpRequest("GET", "../ajax/theform.php", true, handleResponse);

}


function checkfortasks(thedate,e) {
	
	theObject = document.getElementById("taskbox");

	theObject.style.visibility = "visible";

	var posx = 0;
	var posy = 0;

	posx=e.clientX + document.body.scrollLeft;
	posy=e.clientY + document.body.scrollTop;

	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";

	//The location we are loading the page into.
	objID = "taskbox";
	var url = "../ajax/taskchecker.php?thedate=" + thedate;
	httpRequest("GET", url, true, handleResponse);

}



function closetask() {

	theObject = document.getElementById("createtask");

	theObject.style.visibility = "hidden";
	theObject.style.height="0px";
	theObject.style.width="0px";


	acObject = document.getElementById("autocompletediv");

	acObject.style.visibility = "hidden";
	acObject.style.height="0px";
	acObject.style.width="0px";

}

function hidetask() {

	theObject = document.getElementById("taskbox");

	theObject.style.visibility = "hidden";
	theObject.style.height="0px";
	theObject.style.width="0px";

}

function findPosX(obj) {

	var curleft = 0;
	
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {

	var curtop = 0;
	
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function autocomplete (thevalue, e) {
	
	
	objID="autocompletediv";
	theObject = document.getElementById(objID);



	theObject.style.visibility = "visible";
	theObject.style.width="152px";

	var posx = 0;
	var posy = 0;

	posx = (findPosX (document.getElementById("yourname")) + 1);
	posy = (findPosX (document.getElementById("yourname")) + 23);

	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";



	var theextrachar = e.which;
	var url = "";

	if (theextrachar == undefined){
		theextrachar=e.keyCode;
	}

	if (theextrachar == 8){
		if(thevalue.length == 1) {
		 url = "../ajax/autocomp.php";
		} else {
		 url = "../ajax/autocomp.php?sstring=" + thevalue.substr (0, (thevalue.length -1));
		}
	} else {
		url = "../ajax/autocomp.php?sstring=" + thevalue + String.fromCharCode(theextrachar);
	}

	httpRequest("GET", url, true, handleResponse);
}


function setvalue(thevalue) {

	acObject = document.getElementById("autocompletediv");

	acObject.style.visibility = "hidden";
	acObject.style.height="0px";
	acObject.style.width="0px";

	document.getElementById("yourname").value = thevalue;

}

function validateform (thevalue) {
	
	
	objID="messagebox";
	theObject = document.getElementById(objID);

	var url = "../ajax/validator.php?sstring=" + thevalue;

	httpRequest("GET", url, true, handleResponse);
}



function showHideJobs(type,name) {

	
	objID="docobj";
	theObject = document.getElementById(objID);

	if (showJobs == true) {
		//Show the calender
		var url = "../ajax/objdisplay.php?name=" + name + "&type=" + type;
		//showJobs= false;
		httpRequest("GET", url, true, handleResponse);
		
	} else {
		//Hide the Calendar
		document.getElementById(objID).innerHTML = "";
		showJobs= true;
	}//end if


}

//////////////////////////////////////////////////////////////////////////////////////
//Google Maps
////////////////////////////////////////////////////////////////////////////////
//A variable used to disttinguish whether to open or close the calendar.
var map = null;
var geocoder = null;

window.onload=function(){
	if (GBrowserIsCompatible()) {
	createMap(138.55,-34.9, 10);
	} else alert("You suck");
}

function createMap(lat,lng,zoomlevel){
        geocoder = new GClientGeocoder();	
		map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lng,lat), zoomlevel);
		map.setUIToDefault();
		map.setMapType(G_ROAD_MAP);

}

function addToMap(response){

		place = response.Placemark[0]; 
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(point, 17);
		map.setMapType(G_HYBRID_MAP);
		marker = new GMarker(point);
		map.addOverlay(marker);
		//marker.openInfoWindowHtml(place.address);
}



///////////////////////////////////////////////////////////////////////////////////////
//Job summary part
////////////////////////////////////////////////////////////////////////////////////////
function showtest() {
	objID="jobsummary";
	theObject = document.getElementById(objID);
    details=theObject.innerHTML;
	var a = new Array();
	a=details.split('.');
	address=a[0];
	geocoder.getLocations(address, addToMap);	
}

function showJobSummary(id) {
	
	objID="jobsummary";
	theObject = document.getElementById(objID);
	
	if (showJob == true) {
		theObject.style.visibility = "visible";
		theObject.style.height="100%";
		theObject.style.width="600px";

		var url = "../ajax/jobsummary.php?jn=" + id;
		httpRequest("GET",url,true,handleResponse);
		showJob = false;

	} else {
		theObject.innerHTML = "";
		theObject.style.visibility = "hidden";
		theObject.style.height="0px";
		theObject.style.width="0px";
		showJob = true;
	}//end if
}

//end class
