// JavaScript Document
// HOSTING AJAX Handling
function setTDDetails(element, stil) {
//default:	padding-left:12px; 
//1:	font-size: 12px; border-bottom:#003366 1px dotted;
	element.style.borderBottom = "#003366 1px dotted";
	element.style.fontSize = "12px";
	switch(stil) {
		case 1: element.style.fontSize = "12px";
		break;
		default: element.style.paddingLeft = "12px";
		break;
	}
	return element;
}

//================================================================================

function GetPlan(id, lang) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{
		var getul = "id=" + id + "&lang=" + lang;
		xmlHttp.open("GET", URL_BASE + "AJAX/get_hplan.php?"+getul, true);
		xmlHttp.onreadystatechange = hSRPlan;
		xmlHttp.send(null);
		language = lang;
	} else
		setTimeout('GetPlan(' + id + ', ' + lang + ')', 1000);
}

function GetSupliments(lang) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{
		var getul = "lang=" + lang;
		xmlHttp.open("GET", URL_BASE + "AJAX/get_hsup.php?"+getul, true);
		language = lang;
		xmlHttp.onreadystatechange = hSRSupl;
		xmlHttp.send(null);
	} else
		setTimeout('GetSupliments(' + lang + ')', 1000);
}

//================================================================================

function hSRPlan() {
	if (xmlHttp.readyState == 4) {
		SetIdleState();
		if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			document.getElementById("divMessage").innerHTML = '<i>' + unescape(xmlResponse.getElementsByTagName("response")[0].childNodes[0].nodeValue) + '</i>';
			if_error = xmlResponse.getElementsByTagName("error");
			if(if_error[0].childNodes[0].nodeValue == "0") {
				divul = document.getElementById("ShowPlan");
				divul.innerHTML = "";
				nr_elm = xmlResponse.getElementsByTagName("haselements");
				plantitle = xmlResponse.getElementsByTagName("plantitle");
				if(eval(nr_elm[0].childNodes[0].nodeValue) > 0) {
					rows = xmlResponse.getElementsByTagName("row");
					if(rows.length > 0){
						var tabelu = document.createElement("table");
						tabelu.width = "259";
						tabelu.border = 0;
						tabelu.cellpadding = 5;
						tabelu.cellspacing = 0;
						var trul = null;
						var tdul = null;
						var x = 0;
						trul = tabelu.insertRow(x);
						//Tbl Header
						tdul = trul.insertCell(0);
						tdul.colSpan = "2";
						tdul.height = "15";
						tdul.style.borderBottom = "#003366 1px solid";
						tdul.style.background = "#a7cbf3";
						tdul.innerHTML = '<strong>' + plantitle[0].childNodes[0].nodeValue + '</strong>';
						x++;
						var stilu = 0;
						for(var i=0; i<rows.length; i++){
							bucata = rows[i];
							trul = tabelu.insertRow(x);
							//Nume
							tdul = trul.insertCell(0);
							tdul.width = "165";
							tdul.height = "15";
							tdul = setTDDetails(tdul, 0);							
							tdul.innerHTML = unescape(bucata.getElementsByTagName("nume")[0].childNodes[0].nodeValue);
							//Valoare
							tdul = trul.insertCell(1);
							tdul.width = "94";
							tdul.height = "15";
							tdul = setTDDetails(tdul, 1);							
							tdul.innerHTML = unescape(bucata.getElementsByTagName("valoare")[0].childNodes[0].nodeValue);
							x++;
						}
						divul.appendChild(tabelu);
					}
				}
			}
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	} else {
		if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4) {
			SetWaitState();	
		}	
	}
}

function hSRSupl() {
	if (xmlHttp.readyState == 4) {
		SetIdleState();
		if (xmlHttp.status == 200) {
			xmlResponse = xmlHttp.responseXML;
			document.getElementById("divMessage").innerHTML = '<i>' + unescape(xmlResponse.getElementsByTagName("response")[0].childNodes[0].nodeValue) + '</i>';
			if_error = xmlResponse.getElementsByTagName("error");
			if(if_error[0].childNodes[0].nodeValue == "0") {
				divul = document.getElementById("ShowPlan");
				divul.innerHTML = "";
				nr_elm = xmlResponse.getElementsByTagName("haselements");
				if(eval(nr_elm[0].childNodes[0].nodeValue) > 0) {
					rows = xmlResponse.getElementsByTagName("row");
					if(rows.length > 0){
						var tabelu = document.createElement("table");
						tabelu.width = "335";
						tabelu.border = 0;
						tabelu.cellpadding = 5;
						tabelu.cellspacing = 1;
						var trul = null;
						var tdul = null;
						var x = 0;
						var stilu = 0;
						var titlu = "&nbsp;";
						for(var i=0; i<rows.length; i++){
							bucata = rows[i];
							if(unescape(bucata.getElementsByTagName("nume")[0].childNodes[0].nodeValue) != titlu) {
								titlu = unescape(bucata.getElementsByTagName("nume")[0].childNodes[0].nodeValue);
								trul = tabelu.insertRow(x);
								//Tbl Header
								tdul = trul.insertCell(0);
								tdul.colSpan = "3";
								tdul.height = "15";
								tdul.style.borderBottom = "#003366 1px solid";
								tdul.style.background = "#a7cbf3";
								tdul.innerHTML = '<strong>Extra ' + titlu + '</strong>';
								x++;
							}
							trul = tabelu.insertRow(x);
							//Nume
							tdul = trul.insertCell(0);
							tdul.width = "140";
							tdul.height = "15";
							tdul = setTDDetails(tdul, 0);							
							tdul.innerHTML = unescape(bucata.getElementsByTagName("detaliu")[0].childNodes[0].nodeValue);
							//Valoare
							tdul = trul.insertCell(1);
							tdul.width = "119";
							tdul.height = "15";
							tdul = setTDDetails(tdul, 1);							
							tdul.innerHTML = bucata.getElementsByTagName("pret")[0].childNodes[0].nodeValue + '&nbsp;' + unescape(bucata.getElementsByTagName("valuta")[0].childNodes[0].nodeValue);
							//Ciclu
							tdul = trul.insertCell(2);
							tdul.width = "76";
							tdul.height = "15";
							tdul = setTDDetails(tdul, 1);							
							if(language == 1)
								tdul.innerHTML = bucata.getElementsByTagName("ciclu")[0].childNodes[0].nodeValue + '&nbsp;months';
							else
								tdul.innerHTML = bucata.getElementsByTagName("ciclu")[0].childNodes[0].nodeValue + '&nbsp;luni';
							x++;
						}
						divul.appendChild(tabelu);
					}
				}
			}
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	} else {
		if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4) {
			SetWaitState();	
		}	
	}
}
