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 ValidateBooking(vName, vAddress, vPhone, vEmail, vAdults, vChildren, vInvEmail, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;
    if (document.getElementById("c_name").value == ""){
  	    FieldList = "<br />\n&nbsp;&bull;&nbsp;" + vName + "\n";
    }
	if (document.getElementById("c_address").value == ""){
  	    FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vAddress + "\n";
    }
	if (document.getElementById("c_phone").value == ""){
  	    FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vPhone + "\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 (document.getElementById("c_adults").value == ""){
  	    FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vAdults + "\n";
    }
	if (document.getElementById("c_children").value == ""){
  	    FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vChildren + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = vMsg + ":\n\n" + FieldList;
        return( false );
    }
}
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 ValidateProduct(){
    var FieldList = "";
    if (document.getElementById("title_bg").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Име на продукт (BG)\n";
    }
    if (document.getElementById("title_en").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на продукт (EN)\n";
    }
	if (document.getElementById("title_ru").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на продукт (RU)\n";
    }
    if (document.getElementById("category_id").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Категория\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}
function ValidateCountry(){
    var FieldList = "";
    if (document.getElementById("title_bg").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Име на държава (BG)\n";
    }
    if (document.getElementById("title_en").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на държава (EN)\n";
    }
	if (document.getElementById("title_ru").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на държава (RU)\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}
function ValidateLocation(){
    var FieldList = "";
    /*if (document.getElementById("title_bg").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Заглавие (BG)\n";
    }
    if (document.getElementById("title_en").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Заглавие (EN)\n";
    }
    if (document.getElementById("category_id").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Категория\n";
    }*/
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}
function ValidateCategory(){
    var FieldList = "";
    if (document.getElementById("category_bg").value == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Име на категория (BG)\n";
    }
    if (document.getElementById("category_en").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на категория (EN)\n";
    }
	if (document.getElementById("category_ru").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;Име на категория (RU)\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Следните полета са задължителни:\n\n" + FieldList;
        return( false );
    }
}
function ValidateOption(){
	var FieldList = "";
}
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 (answer) {
        window.location = "index.php?controller=" + controller + "&action=" + dAction + "&id=" + pid + dParentId;
		return false;
	}
}

function delPicture(product_id, picture_id, cat) {
	http3 = getXMLHTTPRequest();
    var url = "del_picture.php";
	var myRandom = parseInt(Math.random()*99999999);
	http3.open("GET", url + "?product_id=" + parseInt(product_id) + "&cat="+cat+"&picture_id=" + parseInt(picture_id) + "&rand=" + myRandom, true); 
	http3.onreadystatechange = function(){
		if (http3.readyState == 4) { 
			document.getElementById("pictureBox"+cat).innerHTML = http3.responseText;
		} else {
				document.getElementById("pictureBox"+cat).innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
		}		
	}
	http3.send(null);
}

function delPdf(product_id, lang) {
	http3 = getXMLHTTPRequest();
    var url = "del_pdf.php";
	var myRandom = parseInt(Math.random()*99999999);
	http3.open("GET", url + "?product_id=" + parseInt(product_id) + "&lang="+lang+"&rand=" + myRandom, true); 
	http3.onreadystatechange = function(){
		if (http3.readyState == 4) { 
			document.getElementById("pdfBox").innerHTML = http3.responseText;
		} else {
				document.getElementById("pdfBox").innerHTML = "<img src=\"../img/ajax_24.gif\" style=\"border:none;width:24px;height:24px;\" alt=\"\" />\n";
		}		
	}
	http3.send(null);
}


function delArticle(product_id, article_id) {
	http4 = getXMLHTTPRequest();
    var url = "del_article.php";
	var myRandom = parseInt(Math.random()*99999999);
	http4.open("GET", url + "?product_id=" + parseInt(product_id) + "&article_id=" + parseInt(article_id) + "&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 delFile(file_id, newsletter_id) {
	http5 = getXMLHTTPRequest();
    var url = "del_file.php";
	var myRandom = parseInt(Math.random()*99999999);
	http5.open("GET", url + "?file_id=" + parseInt(file_id) + "&newsletter_id=" + parseInt(newsletter_id) + "&rand=" + myRandom, true); 
	http5.onreadystatechange = handleDelFile;
	http5.send(null);	
}
function handleDelFile() {
	if (http5.readyState == 4) { 
        if (http5.status == 200) {
			document.getElementById("fileBox").innerHTML = http5.responseText;
		}
	} else {
			document.getElementById("fileBox").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 bigpic(file,wid,hgt){
	sw=(screen.width-wid)/2;
    sh=(screen.height-hgt)/2;
	window.open(file,'mywindow','width='+wid+',height='+hgt+',scrollbars=1,resizable=1,menubar=0,toolbar=0,location=0,directories=0,status=0,top='+sh+',left='+sw+'')
}
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;
}

var min=10;
var max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('div');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('div');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

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 calcPrice2(e,rownum) {
	var dPrice = parseFloat(document.getElementById(e).value);
	for (var i = 0; i < theArray.length; i++){
		var id = "price2_" + 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 getRadioVal(radioObj)
{
    var value = "";

    if (!radioObj.length)
    {
        if (radioObj.checked) value = radioObj.value;
    } else {
        for (var i = 0; i < radioObj.length; i++)
        {
            if (radioObj[i].checked)
            {
                value = radioObj[i].value;
                break;
            }
        }
    }
    return value;
}
function ValidateSubscription(vEmail, vInvEmail, vOpt, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;

    if (getRadioVal(document.subscription_form.action) == "") {
        FieldList = "<br />\n&nbsp;&bull;&nbsp;" + vOpt + "\n";
    }
    validRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/i;
    strEmail = document.getElementById("subscriber_email").value;

    if (document.getElementById("subscriber_email").value != "") {
        if (strEmail.search(validRegExp) == -1) {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if (document.getElementById("subscriber_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        document.getElementById("news_err_msg").innerHTML = vMsg + ":\n\n" + FieldList;
        return( false );
    }
}
function check(method,fieldname)
{
    var checkboxes = document.getElementsByName(fieldname);
    for(i=0; i < checkboxes.length; i++)
    {
        checkboxes[i].checked = method;
    }
}
function ValidateNewsletterSend(fieldname) {
    var FieldList = "";
	var ch = 0;
    
    var checkboxes = document.getElementsByName(fieldname);
    for(i=0; i < checkboxes.length; i++)
    {
		if (checkboxes[i].checked == 1)
		{
            ch = 1;
		}
    }
	if (ch == 0)
	{
        FieldList = "<br />\n&nbsp;&bull;&nbsp;Изпрати на";
	}
    if (FieldList == ""){
		document.getElementById("status_msg").innerHTML="Моля изчакайте до края на процеса...";
        return ( true );
    } else {
        document.getElementById("error_msg").innerHTML = "Моля попълнете следните полета:\n\n" + FieldList;
        return( false );
	}
}

function openPopup(url, width, height, scrollbars, resizeable){	
    window.open(url,'popup','width='+width+', height='+height+', scrollbars='+scrollbars+', resizeable='+resizeable+'')
}

function showhide(id){
	if(id.value=='T'){
		document.getElementById('top_desc_bg').style.display = 'block';
		document.getElementById('top_desc_en').style.display = 'block';
		document.getElementById('top_desc_ru').style.display = 'block';
	} else {
		document.getElementById('top_desc_bg').style.display = 'none';
		document.getElementById('top_desc_en').style.display = 'none';
		document.getElementById('top_desc_ru').style.display = 'none';
	}
}
