var curr = "pf";


/*
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
*/

	// CLASS ajax , constructor
	/*
	*
	*
	*	urlComponents 	: assoc array with the index beeing the name of the url variable
	*					and the value as the variables value
	*
	*/
	function _aim_Ajax(baseUrl,urlComponents,method_) {
		this.xmlHttp = null;			// the xmlhttp object
		this.url = baseUrl;			// url send by ajax
		this.method = method_;			// post or get
		this.components = urlComponents;	// array of name and value pair
		this.valueSTR = "";			// string url encoded for sending through ajax
		/*
			build url
		*/

		switch(this.method) {
			case "GET":					// get method
				var counter=0;
				for(i in this.components) {	// build componets string
					this.valueSTR += counter == 0 ?
										(i+"="+encodeURIComponent(urlComponents[i]))
												  :
										("&"+i+"="+encodeURIComponent(urlComponents[i]));
					counter++;
				}
				counter=null;
				this.url += "?"+this.valueSTR;	// build whole url that will be sent
			break;

			case "POST":				// post method
				var counter=0;
				for(i in this.components) {	// build components string
					this.valueSTR += counter == 0 ?
										(i+"="+encodeURIComponent(urlComponents[i]))
										:
										("&"+i+"="+encodeURIComponent(urlComponents[i]));
					counter++;
				}
				counter=null;
			break;
		}


		//	end build url
	}
	// create xml http req
	_aim_Ajax.prototype.createRequest = function () {
		try {
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				this.xmlHttp=null;
			}
		}
		if(!this.xmlHttp && typeof XMLHttpRequest != "undefined")
			this.xmlHttp = new XMLHttpRequest();

		if (this.xmlHttp)
			return this.xmlHttp;
	}

	_aim_Ajax.prototype.openConnection = function () {
		// just to be cautious , neved really used
			this.xmlHttp.onreadystatechange = function () {} // clear any previous set handlers
		this.xmlHttp.abort();					 // close any previous connections

		// open connection
		this.xmlHttp.open(this.method, this.url, true);

		// if post is beeing used to send data to php
		if(this.method == "POST") {
			this.xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			this.xmlHttp.setRequestHeader("Content-length",this.valueSTR.length);
			this.xmlHttp.setRequestHeader("Connection","close");
		}
	}


	_aim_Ajax.prototype.sendRequest = function () {

		if(this.method == "POST") { 	// if post
			this.xmlHttp.send(this.valueSTR); // send post data
		} else {						// if get , no data needed , its all in the url
			this.xmlHttp.send(null);
		}
			// force xml response , pass 4096 ff or netscape limit
			if (this.xmlHttp.overrideMimeType) { // ie does not have this
			this.xmlHttp.overrideMimeType('text/xml');
		}
	}

function getobj(name) {
  if(document.getElementById) {
  		
		if(typeof name=="string") {
			this.obj = document.getElementById(name);
			  return this.obj;
		} else {
			  this.obj = name.style;
			  return this.obj;
		}
	}
}

function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function isValidURL(url){
	var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	if(RegExp.test(url)){
		return true;
	}else{
		return false;
	}
}

function IsNumeric(strString)
//  check for valid numeric strings	
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
  {
  strChar = strString.charAt(i);
  if (strValidChars.indexOf(strChar) == -1)
	 {
	 blnResult = false;
	 }
  }
return blnResult;
}

function setfact(to) {
	getobj('tipfact').value = to;
	/*
		
	var ajax = new _aim_Ajax("php/setfact.php",
						{
							"fact"	:	to
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			
			delete ajax;
			}
		}
	}		

	*/
}


function setprice(id,chageTo) {
	
	var ajax = new _aim_Ajax("php/setprice.php",
						{
							"price"		:	chageTo,
							"id"		: 	id
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				ALERT_TITLE = "done";
				if(ajax.xmlHttp.responseText == "ok") { alert("O mers... mare minune"); }
			/* end action */
			delete ajax;
			}
		}
	}	
}   
function setcmdstatus(id,chageTo) {
	
	var ajax = new _aim_Ajax("php/changestatus.php",
						{
							"status"	:	chageTo,
							"id"		: 	id
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				ALERT_TITLE = "done";
				if(ajax.xmlHttp.responseText == "ok") { alert("O mers... mare minune"); }
				
			/* end action */
			delete ajax;
			}
		}
	}	
}   
function deletecomanda(id,dir) {
	var ajax = new _aim_Ajax("php/stergecomanda.php",
						{
							"id"		: 	id,
							"dir"		:	dir
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				ALERT_TITLE = "done";
				if(ajax.xmlHttp.responseText == "ok") { alert("O mers... mare minune"); }
				alert(ajax.xmlHttp.responseText);
			/* end action */
			delete ajax;
			}
		}
	}	
}   

//

function changeCalendar(chageTo) {
	
	var ajax = new _aim_Ajax("php/getPage.php",
						{
							"pagina"	:	chageTo
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				//alert(ajax.xmlHttp.responseText);
				getobj('calendaretable').innerHTML = ajax.xmlHttp.responseText;
			/* end action */
			delete ajax;
			}
		}
	}	
	/*
	var gramaj = getobj('gramaj');
	var dimensiune = getobj('dimensiune');
	var policromie = getobj('policromie');
	var coliluna = getobj('coliluna');
	
	switch(chageTo) {
		
		case "buzunar":
			// dimensiune
			dimensiune.innerHTML = "<b>7 x 10 cm</b>";
			gramaj.innerHTML = '<select name="" id="" style="width:204px;border:1px solid #585858;"><option value="100">100 g/mp</option><option value="150">150 g/mp</option><option value="200">200 g/mp</option><option value="250">250 g/mp</option><option value="300">300 g/mp</option></select>';
		break;

		case "perete":
			var dim="";
			dim += '<select name="cmd_dimensiune" id="cmd_dimensiune" style="width:204px;border:1px solid #585858;"> ';
			dim += '<option value="16*47">16 x 47</option>';
			dim += '<option value="21*32">21 x 32</option>';
			dim += '<option value="30*47">30 x 47</option>';
			dim += '</select>';
			
			dimensiune.innerHTML = dim;
			gramaj.innerHTML = "perete";
			
			//   
			//<input type="text" onkeyup="if(this.value > 35) { alert('Lungimea nu poate fi mai mare de 35 cm.');this.value=''; }" id="cmd_dimLungime" value="<?php echo $_SESSION["cmd_dimLungime"]; ?>" name="cmd_dimLungime" class="reginput"/>
		break;

		case "birou":
			gramaj.innerHTML = "birou";
		break;
	
	}
	*/
}
function loadCalc() {
	
	var ajax = new _aim_Ajax("php/loadCalc.php",
						{
							"noop"	: "noop"
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				//alert(ajax.xmlHttp.responseText);
				getobj('calccontent').innerHTML = ajax.xmlHttp.responseText;
			/* end action */
			delete ajax;
			}
		}
	}	
	
}   
/**
*
*/
function onld() {
	
}

function switchfact(w) {
	var a = getobj('fpf');
	var b = getobj('ff');

	if(w=="fpf") {
	
	a.style.display = "block";
	a.style.visibility = "visible";
	
	b.style.display = "none";
	b.style.visibility = "hidden";
	
	} else {
	b.style.display = "block";
	b.style.visibility = "visible";

	a.style.display = "none";
	a.style.visibility = "hidden";
	}
}




// constants to define the title of the alert and button text.
var ALERT_TITLE = "Eroare";
var ALERT_BUTTON_TEXT = "Ok";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts


function createCustomAlert(txt) {
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

/*
	calculator preturi
*/
function getGenerateCalcFormData(generateFor) {
	if(generateFor=="") return;
	getobj('calculator').innerHTML = "<img src='img/loading.gif' alt='' >";
	
	var ajax = new _aim_Ajax("php/calculator/generateCalcForm.php",
						{
							"tipprodus"		: 	generateFor
						}
						,"GET");
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				getobj('calculator').innerHTML = ajax.xmlHttp.responseText;
				//alert(ajax.xmlHttp.responseText);
			/* end action */
			delete ajax;
			}
		}
	}	
}

function calculeazaPret() {
	
	var tipprodus = "";
	var dimensiune = "";
	var policromie = "";
	var indoituri = "";
	var cantitate = "";
	var grafcom = "";
	var nrcoli = "";
	
	if(tipprodus = getobj('produs')) {
		tipprodus = tipprodus.value;
	}
	if(dimensiune = getobj('dimensiune')) {
		dimensiune = dimensiune.value;
	}
	if(policromie = getobj('policromie')) {
		policromie = getobj('policromie').value;
	}
	if(indoituri = getobj('indoituri')) {
		indoituri = getobj('indoituri').value;
	}	
	if(nrcoli = getobj('nrcoli')) {
		nrcoli = getobj('nrcoli').value;
	}		

	if(cantitate = getobj('cantitate')) {
		cantitate = getobj('cantitate').value;
	}
	grafcom = getobj('grafcom').checked;
	
	if(cantitate=="") {
		alert("Alegeti cantitatea dorita.");
		return;
	}
	
	var ajax = new _aim_Ajax("php/calculator/showCalcResults.php",
						{
							"tipprodus"		: 	tipprodus,
							"dimensiune"	: 	dimensiune,
							"policromie"	: 	policromie,
							"cantitate"		:	cantitate,
							"grafcom"		:	grafcom,
							"indoituri"		:	indoituri,
							"nrcoli"		:	nrcoli
						}
						,"GET");
	
	getobj('calculator').innerHTML = "<img src='img/loading.gif' alt='' >";
							
	ajax.createRequest();
	ajax.openConnection();
	ajax.sendRequest();
	ajax.xmlHttp.onreadystatechange = function () {
		if(ajax.xmlHttp.readyState == 4) {
			if(ajax.xmlHttp.status == 200) {
			/* action */
				getobj('calculator').innerHTML = ajax.xmlHttp.responseText;
			/* end action */
			delete ajax;
			}
		}
	}
	
}

	
	function flipTemplatePage(offset,howmany,cat) {
		
		getobj('showtemplates').innerHTML = "<br/><br/><img src='img/loading.gif' alt='' style='vertical-align:middle' > se incarca ..<br/><br/>";
		
		var ajax = new _aim_Ajax("php/template.php",
							{
								"offset"		:	offset,
								"howmany"		:	howmany,
								"cat"			:	cat
							}
							,"GET");
		ajax.createRequest();
		ajax.openConnection();
		ajax.sendRequest();
			ajax.xmlHttp.onreadystatechange = function () {
			if(ajax.xmlHttp.readyState == 4) {
				if(ajax.xmlHttp.status == 200) {
					/* action */
					
					//alert(ajax.xmlHttp.responseText);
					getobj("showtemplates").innerHTML = ajax.xmlHttp.responseText;
					
					/* end action */
					delete ajax;

				}
			}
		}
	}

	
		function setTemplate(towhat) {
		
		var ajax = new _aim_Ajax("php/settemplate.php",
							{
								"towhat"		:	towhat
							}
							,"GET");
		ajax.createRequest();
		ajax.openConnection();
		ajax.sendRequest();
			ajax.xmlHttp.onreadystatechange = function () {
			if(ajax.xmlHttp.readyState == 4) {
				if(ajax.xmlHttp.status == 200) {
					/* action */
					
					//alert(ajax.xmlHttp.responseText);
					
					/* end action */
					delete ajax;

				}
			}
		}
	}

