var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch (e) {}
		}
	}
	if (!xmlHttp) {
		//alert("Error creating the XMLHttpRequest object.");
	} else {
		return xmlHttp;
	}
}

function process(vAutoPost, vPageName) {
	var sSuffix = /msie/.test( navigator.userAgent.toLowerCase() ) ? 'ie' : 'gecko' ;
	var vCarID = document.FrmOrderForm.var_car_id.value;
	var vFromDate = document.FrmOrderForm.date_from.value;
	var vToDate = document.FrmOrderForm.date_to.value;
	if (vFromDate == "" || vToDate == "") return false;
	if ( false ) {//((vAutoPost == true && xmlHttp))) { // (sSuffix == 'ie' && xmlHttp) || 
		var params = "";
		params += "carID=" + vCarID;
		params += "&date_from=" + vFromDate;
		params += "&date_to=" + vToDate;
		try {
			xmlHttp.open("GET", "http://www.rentexpress.ru/x_price.php?" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		} catch (e) {
			//alert("Can't connect to server:\n" + e.toString());
		}
	} else {
		document.location.href = vPageName + '?carID=' + vCarID + '&order=true&date_from=' + vFromDate + '&date_to=' + vToDate;
	}
}

function handleRequestStateChange() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			try {
				handleServerResponse();
			} catch(e) {
				//alert("Error reading the response: " + e.toString());
			}
		} else {
			//alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse() {
	var L_myDivElementDays = document.getElementById("myDivElementDays");
	var L_myDivElementOnOneDay = document.getElementById("myDivElementOnOneDay");
	var L_myDivElementPrice = document.getElementById("myDivElementPrice");

	var responseXml = xmlHttp.responseXML;
	var xmlDoc = responseXml.documentElement;

	var V_myDivElementDays = xmlDoc.getElementsByTagName("days")[0].firstChild.data + "";
	if (V_myDivElementDays < 1) {

		L_myDivElementDays.innerHTML = "0";
		L_myDivElementOnOneDay.innerHTML = "0";
		L_myDivElementPrice.innerHTML = "<p style=\"color:#990000;\">Введена некорректная дата</p>";
		FrmOrderForm.is_sendable.value = '';

	} else {
		var V_myDivElementOnOneDay = xmlDoc.getElementsByTagName("priceoneday")[0].firstChild.data + "";
		var V_myDivElementPrice = xmlDoc.getElementsByTagName("price")[0].firstChild.data + "";

		if (V_myDivElementDays >= 1) {
			L_myDivElementDays.innerHTML = "" + V_myDivElementDays + "";
			L_myDivElementOnOneDay.innerHTML = "" + V_myDivElementOnOneDay + " р.";
			L_myDivElementPrice.innerHTML = "<b>" + V_myDivElementPrice + "</b> р.";
			FrmOrderForm.is_sendable.value = 'true';
		}
	}

	/*
		var textResponse = xmlHttp.responseText;
		myDiv = document.getElementById("myDivElementPrice");
		myDiv.innerHTML = textResponse;
	*/
}

function GetDateByCalendar(vForm, vField, vSpan, vIsProcess) {
	WindowOpen("calendar-iframe.php?form=" + vForm + "&field=" + vField + "&span=" + vSpan + "&iprocess=" + vIsProcess + "&cash=" + GetRand(), 180, 210, 'no', 'no');
	/*
	result = showModalDialog("calendar-iframe.php?cash=" + GetRand(), "", "dialogWidth:180px; dialogHeight:200px; help:no; resizable:no; scroll:no; status:no;");
	//result = window.modal("calendar-iframe.php?cash=" + GetRand(), 'width=180,height=200,toolbars=0,resizable=0,scroll=0,status=0');
	if (result != null) {
		var oField = eval(vForm + '.' + vField);
		oField.value = result['dateStandart'];
		var oSpan = document.getElementById(vSpan);
		oSpan.innerHTML = result['dateRussian'];
		if (vIsProcess) process();
	}
	*/
}

String.prototype.isArgument = function() {
	return /^([a-zA-Z]){1,}=([0-9]){1,}$/.test(this);
}

/*
call this function just work like window.open(url,name,feature);
however, for IE5.0+, it will open a showModelessDialog window;
and For Gecko(Mozilla or Netscape), the child window will stay on top focus untill user close it.
programmed by hedger
hedger@yahoo-inc.com
*/


function dialog(url, name, feature, isModal) {
	if (url == null) {
		return false;
	}
	url = url;
	if (name == null) {
		name = "";
	}
	if (feature == null) {
		feature = "";
	}
	if (window.showModelessDialog) {
		var WindowFeature = new Object();
		WindowFeature["width"] = 400;
		WindowFeature["height"] =400;
		WindowFeature["left"] = "";
		WindowFeature["top"] = "";
		WindowFeature["resizable"] = "";

		if(feature !=null && feature!="") {
			feature = ( feature.toLowerCase()).split(",");
			for(var i=0;i< feature.length;i++) {
				if(feature[i].isArgument()) {
					var featureName = feature[i].split("=")[0];
					var featureValue = feature[i].split("=")[1];
					if(WindowFeature[featureName]!=null) {
						WindowFeature[featureName] = featureValue;
					}
				}
			}
		}

		if(WindowFeature["resizable"]==1 || WindowFeature["resizable"]=="1" || WindowFeature["resizable"].toString().toLowerCase()=="yes"){WindowFeature["resizable"] = "resizable:1;minimize:1;maximize:1;"}
		if(WindowFeature["left"]!=""){WindowFeature["left"] ="dialogLeft:" +  WindowFeature["left"] +"px;";}
		if(WindowFeature["top"]!=""){WindowFeature["top"] ="dialogTop:" +  WindowFeature["Top"] +"px;"; }
		if(window.ModelessDialog ==null){window.ModelessDialog = new Object() ; };
		if(name!="") {
			if(window.ModelessDialog[name]!=null && !window.ModelessDialog[name].closed ) {
				window.ModelessDialog[name].focus();
				return window.ModelessDialog[name];
			}
		}
		var F = WindowFeature["left"] +WindowFeature["top"] +  "dialogWidth:"+WindowFeature["width"] +" px;dialogHeight:"+WindowFeature["height"]+"px;center:1;help:0;" + WindowFeature["resizable"] +"status:0;unadorned:0;edge: raised; ;border:thick;"
		if(isModal) {
			window.showModalDialog(url,self,F);
			return false;
		} else {
			window.ModelessDialog[name] = window.showModelessDialog(url,self,F);
			return window.ModelessDialog[name];
		}	
	} else {
		if(document.getBoxObjectFor) {
			if(isModal) {
				var Modal = window.open(url, name, "modal=1," + feature);
				var ModalFocus = function() {
					if(!Modal.closed) {
						Modal.focus();
					} else {
						Modal = null;
						window.removeEventListener(ModalFocus, "focus");
						ModalFocus = null;
					}
				}
				window.addEventListener("focus", ModalFocus, false); 
				return false;
			} else {
				return window.open(url, name, "modal=1," + feature);
			}
		} else { 
			return window.open(url, name, feature);
		}
	}
	return null;
}

function modal(url, feature) {
	dialog(url, "", feature, true);
	return false;
}
