function getXMLHTTPRequest() {
    var request = false;

    try { 
	    request = new XMLHttpRequest(); /* e.g. Firefox */ 
	} catch(e) { 
	    try { 
		    request = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */ 
		    } catch (e) { 
			    try { 
				    request = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ 
				    } catch (e) { 
					    request = false; 
				    } 
		    } 
	}
    return request;
}
function delItem(controller, pid) {
    var answer = confirm("Сигурни ли сте, че искате да изтриете избраният запис?")
	if (answer) {
        window.location = "index.php?controller=" + controller + "&action=delete&id=" + pid;
		return false;
	}
}
function delExt(controller, pid, group_id, item_id) {
    var answer = confirm("Сигурни ли сте, че искате да изтриете избраният запис?")
	if (answer) {
        window.location = "index.php?controller=" + controller + "&action=delete&id=" + pid + "&group_id=" + parseInt(group_id) + "&item_id=" + parseInt(item_id);
		return false;
	}
}
function ValidateContact(cName, cEmail, cInvalidEmail, cSecurityCode, cErrMsg){
	var FieldList = "";
	var validRegExp;
    var strEmail;
    if (document.getElementById("c_name").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;"+cName+"\n";
    }
    if (document.getElementById("c_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+cEmail+"\n";
    }
    validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i;
	strEmail = document.getElementById("c_email").value;
	if (document.getElementById("c_email").value != "")
	{
		if (strEmail.search(validRegExp) == -1) {
			FieldList = FieldList + "<br />&nbsp;&bull;&nbsp;"+cInvalidEmail+"\n";
		}
	}
    if (document.getElementById("validator").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+cSecurityCode+"\n";
    }	
	if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = cErrMsg+"\n\n" + FieldList;
        return( false );
    }
}
function renewCode(){
	var myRandom = parseInt(Math.random()*99999999);
	document.getElementById('security_code').src = 'random.php?what=contact&renew=true&rand=' + myRandom;
}
function bigimage(image,title,wid,hgt){
sw=(screen.width-wid)/2;
sh=(screen.height-hgt)/2;
newwin=window.open('','newwin','width='+wid+',height='+hgt+',scrollbars=1,resizable=1,menubar=0,toolbar=0,location=0,directories=0,status=0,top='+sh+',left='+sw+'');
newwin.document.open();
newwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n');
newwin.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
newwin.document.write('\n<html>\n<head>\n');
newwin.document.write('<title>'+title+'</title>\n');
newwin.document.write('</head>');
//newwin.document.write('<body style="margin:0;padding:0">\n<img src="'+image+'" width="'+wid+'" height="'+hgt+'" />');
newwin.document.write('<body style="margin:0;padding:0">\n<img src="'+image+'" alt="" />');
newwin.document.write('\n</body></html>');
newwin.document.close();
newwin.focus();
}
function ValidateReservation(rName, rEmail, rInvalidEmail, rAgree, rErrMsg)
{
	var FieldList = "";
	var validRegExp;
    var strEmail;
    if (document.getElementById("r_name").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;"+rName+"\n";
    }
    if (document.getElementById("r_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+rEmail+"\n";
    }
    validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i;
	strEmail = document.getElementById("r_email").value;
	if (document.getElementById("r_email").value != "")
	{
		if (strEmail.search(validRegExp) == -1) {
			FieldList = FieldList + "<br />&nbsp;&bull;&nbsp;"+rInvalidEmail+"\n";
		}
	}
	if (document.getElementById("r_agree").checked == false) {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+rAgree+"\n";
    }	
	if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = rErrMsg+"\n\n" + FieldList;
        return( false );
    }
}
function ValidateReserv(rRoom,rDFrom,rDTo,rErrMsg)
{
	var FieldList = "";

    if (document.getElementById("room_id").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;"+rRoom+"\n";
    }
    if (document.getElementById("date_from").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+rDFrom+"\n";
    }
	if (document.getElementById("date_to").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;"+rDTo+"\n";
    }	
	if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = rErrMsg+"\n\n" + FieldList;
        return( false );
    }	
}
function getRooms(e,rootPath) {
	httpX = getXMLHTTPRequest();
    var url = String(rootPath) + "cms/get_rooms.php";
    var object_id = e.options[e.selectedIndex].value;
	var myRandom = parseInt(Math.random()*99999999);
	httpX.open("GET", url + "?object_id=" + parseInt(object_id) + "&rand=" + myRandom, true); 
	httpX.onreadystatechange = function() {requestRooms(rootPath);}; 
	httpX.send(null);
}
function requestRooms(rootPath) { 
	if (httpX.readyState == 4) { 
        if (httpX.status == 200) {
			document.getElementById("roomBox").innerHTML = httpX.responseText;
		}
	} else {
			document.getElementById("roomBox").innerHTML = "<img src=\"" + String(rootPath) + "img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function getAvailability(boxID,rootPath,cYear,e) {
	httpX = getXMLHTTPRequest();
	document.getElementById(boxID).style.display = 'block';
    var url = String(rootPath) + "cms/get_availability.php";
    var room_id = (typeof(e) == 'object') ? e.options[e.selectedIndex].value : e;
	if (room_id != "") 
	{	
		var myRandom = parseInt(Math.random()*99999999);
		httpX.open("GET", url + "?room_id=" + parseInt(room_id) + "&year=" + parseInt(cYear) + "&rand=" + myRandom, true); 
	    httpX.onreadystatechange = function() {requestAvailability(boxID,rootPath);}; 
	    httpX.send(null);
	}
}
function requestAvailability(boxID,rootPath) { 
	if (httpX.readyState == 4) { 
        if (httpX.status == 200) {
			document.getElementById(boxID).innerHTML = httpX.responseText;
		}
	} else {
			document.getElementById(boxID).innerHTML = "<img src=\"" + String(rootPath) + "img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function clearBox(boxID)
{
	if (document.getElementById(boxID))
	{
	    document.getElementById(boxID).innerHTML = '';
    }
}
