var xmlHttp;

function ValidateContact(vName, vEmail, vInvEmail, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;
    if (document.getElementById("c_name").value == ""){
  	    FieldList = "<br />\n&nbsp;&bull;&nbsp;" + vName + "\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 />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if (document.getElementById("c_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = vMsg + ":\n\n" + FieldList;
        return( false );
    }
}

function ValidateBooking(vName, vEmail, vWish, vPhone, vInvEmail, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;
    if (document.getElementById("b_name").value == ""){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vName + "\n";
    }
	if (document.getElementById("b_phone").value == ""){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vPhone + "\n";
    }
	if (document.getElementById("b_wish").value == "0"){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vWish + "\n";
    }
    validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i;
    strEmail = document.getElementById("b_email").value;

    if (document.getElementById("b_email").value != "") {
        if (strEmail.search(validRegExp) == -1) {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if (document.getElementById("b_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = vMsg + ":\n\n" + FieldList;
        return( false );
    }
}


function ValidatePresent(vName, vEmail, vPresent_to, vPhone, vInvEmail, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;
    if (document.getElementById("b_name").value == ""){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vName + "\n";
    }
	if (document.getElementById("b_phone").value == ""){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vPhone + "\n";
    }
	if (document.getElementById("present_name").value == "0"){
  	    FieldList = FieldList+ "<br />\n&nbsp;&bull;&nbsp;" + vPresent_to + "\n";
    }
    validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i;
    strEmail = document.getElementById("b_email").value;

    if (document.getElementById("b_email").value != "") {
        if (strEmail.search(validRegExp) == -1) {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if (document.getElementById("b_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = vMsg + ":\n\n" + FieldList;
        return( false );
    }
}

function ValidateProduct(idArr){
    var FieldList = "";
    if (document.getElementById("category_hash").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Град\n";
    }
    if (document.getElementById("cat_hash").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Категория\n";
    }
	for (var i = 0; i < idArr.length; i++)
	{
	    if (document.getElementById(idArr[i]).value == "") {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + idArr[i] + "\n";
        }	
	}
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}


function ValidateOffer(idArr){
    var FieldList = "";
    if (document.getElementById("category_hash").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Категория\n";
    }
    if (document.getElementById("product_hash").value == "") {
        FieldList = FieldList+"<br />\n&nbsp;&bull;&nbsp;Хотел\n";
    }
	for (var i = 0; i < idArr.length; i++)
	{
	    if (document.getElementById(idArr[i]).value == "") {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + idArr[i] + "\n";
        }	
	}
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}


function delItem(controller, pid, myAction, parent_id) {
    var answer = confirm("Сигурни ли сте, че искате да изтриете избраният запис?");
	var dAction = (typeof myAction == "undefined") ? "delete" : myAction;
	var dParentId = (typeof parent_id == "undefined") ? "" : "&parent_id=" + parent_id;
	
	if(controller=="languages" && answer) 
		answer = confirm("Ако изтриете език всички страници, продукти и категории\n на съответния език ще бъдат изтрити!\n Сигурни ли сте че искате да продължите?");
	if (answer) {
        window.location = "index.php?controller=" + controller + "&action=" + dAction + "&id=" + pid + dParentId;
		return false;
	}
}
function delProduct(controller, pid, myAction, parent_id) {
    var answer = confirm("Сигурни ли сте, че искате да изтриете избраният запис?");
	var dAction = (typeof myAction == "undefined") ? "delete" : myAction;
	var dParentId = (typeof parent_id == "undefined") ? "" : "&parent_id=" + parent_id;
	
	if(controller=="languages" && answer) 
		answer = confirm("Ако изтриете език всички страници, продукти и категории\n на съответния език ще бъдат изтрити!\n Сигурни ли сте че искате да продължите?");
	if (answer) {
        window.location = "index.php?controller=" + controller + "&action=" + dAction + "&hash=" + pid + dParentId;
		return false;
	}
}
function delOption(controller, pid, myAction, parent_id) {
    var answer = confirm("Сигурни ли сте, че искате да изтриете избраният запис?");
	var dAction = (typeof myAction == "undefined") ? "delete" : myAction;
	var dParentId = (typeof parent_id == "undefined") ? "" : "&parent_id=" + parent_id;
	
	if(controller=="languages" && answer) 
		answer = confirm("Ако изтриете език всички страници, продукти и категории\n на съответния език ще бъдат изтрити!\n Сигурни ли сте че искате да продължите?");
	if (answer) {
        window.location = "index.php?controller=" + controller + "&action=" + dAction + "&hash=" + pid + dParentId;
		return false;
	}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function changeParents(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return false;
  } 
var url="changeParents.php";
lang_id= str.options[str.selectedIndex].value;
url=url+"?lang_id="+lang_id;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
	//alert(xmlHttp.responseText);
	document.getElementById("par_id").innerHTML=xmlHttp.responseText;
}
}

function delNewsPic(state,new_id,pic)
{

	if(state==1) document.getElementById('del'+pic).style.visibility="visible";
	if(state==0) document.getElementById('del'+pic).style.visibility="hidden";
	if(state==2)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return false;
		} 
		var url="delNewsPic.php";
		url=url+"?pic="+pic;
		url=url+"&new="+new_id;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged1;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);

	}
}
function stateChanged1() 
{ 
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
	document.getElementById("pic_holder"+xmlHttp.responseText).innerHTML='<img src="addpic.jpg" class="cms_news_pics"/><br/><br/><input type="file" size="10" name="pic'+xmlHttp.responseText+'" id="pic'+xmlHttp.responseText+'">';
}
}
function moveMe(direction,table,containerID,theHash,subHash)
{
	http = GetXmlHttpObject();
    var url = "move_me.php";
	var myRandom = parseInt(Math.random()*99999999);
	var mySubHash = (typeof subHash == 'undefined') ? '' : "&sub_hash=" + subHash;
	http.open("GET", url + "?direction=" + direction + "&table=" + table + "&hash=" + theHash + mySubHash + "&rand=" + myRandom, true); 
	http.onreadystatechange = function () {handleMoveMe(containerID)};
	http.send(null);
}
function handleMoveMe (containerID) {
	if (http.readyState == 4) { 
        if (http.status == 200) {
			document.getElementById(containerID).innerHTML = http.responseText;
		}
	} else {
			document.getElementById(containerID).innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function moveMe2(direction,table,containerID,theHash,productHash)
{
	http = GetXmlHttpObject();
    var url = "move_me.php";
	var myRandom = parseInt(Math.random()*99999999);
	http.open("GET", url + "?direction=" + direction + "&table=" + table + "&hash=" + theHash + "&product_hash=" + productHash + "&rand=" + myRandom, true); 
	http.onreadystatechange = function () {handleMoveMe(containerID)};
	http.send(null);
}

function getRooms(e,rootPath) {
	httpX = GetXmlHttpObject();
    var url = String(rootPath) + "cms/get_rooms.php";
    var category_hash = e.options[e.selectedIndex].value;
	var myRandom = parseInt(Math.random()*99999999);
	httpX.open("GET", url + "?category_hash=" + category_hash + "&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,sLang) {
	httpX = GetXmlHttpObject();
	document.getElementById(boxID).style.display = 'block';
    var url = String(rootPath) + "cms/get_availability.php";
    var product_hash = (typeof(e) == 'object') ? e.options[e.selectedIndex].value : e;
	if (product_hash != "") 
	{	
		var myRandom = parseInt(Math.random()*99999999);
		httpX.open("GET", url + "?product_hash=" + product_hash + "&year=" + parseInt(cYear) + "&root_path=" + escape(rootPath) + "&lang=" + String(sLang) + "&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 = '';
    }
}
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
function isFloatKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode != 46 && (charCode < 48 || charCode > 57)) {
		return false;
	}
    return true;
}
function calcPrice(e,rownum) {
	var dPrice = parseFloat(document.getElementById(e).value);
	for (var i = 0; i < theArray.length; i++){
		var id = "price_" + theArray[i][4] + "_" + parseInt(rownum);
		var val = theArray[i][3] * dPrice;
		if (val == "Nan") val = "0.00";		
	    document.getElementById(id).value = val.toFixed(2);  
	}
}
function delArticle(product_hash, article_hash) {
	http4 = GetXmlHttpObject();
    var url = "del_article.php";
	var myRandom = parseInt(Math.random()*99999999);
	http4.open("GET", url + "?product_hash=" + product_hash + "&article_hash=" + article_hash + "&rand=" + myRandom, true); 
	http4.onreadystatechange = handleDelArticle;
	http4.send(null);
}
function handleDelArticle () {
	if (http4.readyState == 4) { 
        if (http4.status == 200) {
			document.getElementById("articleBox").innerHTML = http4.responseText;
		}
	} else {
			document.getElementById("articleBox").innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function delPicture(product_hash, picture_id) {
	http3 = GetXmlHttpObject();
    var url = "del_picture.php";
	var myRandom = parseInt(Math.random()*99999999);
	http3.open("GET", url + "?product_hash=" + product_hash + "&picture_id=" + parseInt(picture_id) + "&rand=" + myRandom, true); 
	http3.onreadystatechange = handleDelPicture;
	http3.send(null);
}
function delBanner(picture_id) {
	http3 = GetXmlHttpObject();
    var url = "del_banner.php";
	var myRandom = parseInt(Math.random()*99999999);
	http3.open("GET", url + "?picture_id=" + parseInt(picture_id) + "&rand=" + myRandom, true); 
	http3.onreadystatechange = handleDelBanner;
	http3.send(null);
}
function handleDelPicture () {
	if (http3.readyState == 4) { 
        if (http3.status == 200) {
			document.getElementById("pictureBox").innerHTML = http3.responseText;
		}
	} else {
			document.getElementById("pictureBox").innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function handleDelBanner () {
	if (http3.readyState == 4) { 
        if (http3.status == 200) {
			document.getElementById("pictureBox2").innerHTML = http3.responseText;
		}
	} else {
			document.getElementById("pictureBox2").innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
	}
}
function getTotal(e,price) {
	var total = 0;
	for (var i = 0; i < document.article_form.elements.length/2; i++)
	{
	    var price_id = 'pr_' + i;
		var quantity_id = "num_" + i;
		var price = document.getElementById(price_id).value;
		var q = document.getElementById(quantity_id);
		var quantity = q.options[q.selectedIndex].value;
		
		if (quantity == "")
		{
			quantity = 0;
		}
		
		total += parseFloat(price) * parseInt(quantity);
	}
	document.getElementById('total').innerHTML = total.toFixed(2);
}
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 ClearForm(){
  document.getElementById("b_name").value = "";
  document.getElementById("b_wish").value = "0";
  document.getElementById("b_email").value = "";
  document.getElementById("b_phone").value = "";
  document.getElementById("b_country").value = "Please choose";
  document.getElementById("b_hotel").value = "Please choose";
  document.getElementById("date_from").value = "";
  document.getElementById("date_to").value = "";
  document.getElementById("b_transfer2").checked = true;
  document.getElementById("b_type1").checked = false;
  document.getElementById("b_type2").checked = false;
  document.getElementById("b_type3").checked = false;
  document.getElementById("b_type4").checked = false;
  document.getElementById("b_type5").checked = false;
  document.getElementById("b_type6").checked = false;
  document.getElementById("b_adults").value = "";
  document.getElementById("b_children").value = "";
  document.getElementById("b_numrooms").value = "1";
  document.getElementById("b_method").checked = true;
  document.getElementById("b_enquiry").value = "";
 }


function getDocheight(){
na4 = document.getElementById("content").offsetTop;
krai = document.getElementById("razt").offsetTop;
h = (krai-na4);
var b = Math.ceil(h/72);
if(b<9){b=9;}
var new_h = b*72+16;
document.getElementById('content').style.height=new_h+'px';
}

function changepic(selectid, imgsrc){
		var edNum = document.getElementById(selectid).selectedIndex;
 		var pic = document.getElementById(selectid).options[edNum].value; 		
		document.fruits.src=imgsrc+pic;		
}
