function $(id){return document.getElementById(id);}
function createRow(tableId,cellId)
{
	if ($(cellId)) {
		return false;
	}
	else {
		var lastRow = $(tableId).rows.length;
		var row = $(tableId).insertRow(lastRow);
		var cell = row.insertCell(0);
		cell.setAttribute('colSpan', '2');
		cell.setAttribute('style', 'color:#FF0000');
		cell.setAttribute('id', cellId);
		return true;
	}
}
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 ValidateContact(vName, vEmail, vInvEmail, vMsg){
    var FieldList = "";
    var validRegExp;
    var strEmail;
    if ($("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 = $("c_email").value;

    if ($("c_email").value != "") {
        if (strEmail.search(validRegExp) == -1) {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if ($("c_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        $("error_msg").innerHTML = vMsg + ":\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 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) {
			$("articleBox").innerHTML = http4.responseText;
		}
	} else {
			$("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) {
			$("fileBox").innerHTML = http5.responseText;
		}
	} else {
			$("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 = $(price_id).value;
		var q = $(quantity_id);
		var quantity = q.options[q.selectedIndex].value;
		
		if (quantity == "")
		{
			quantity = 0;
		}
		
		total += parseFloat(price) * parseInt(quantity);
	}
	$('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($(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";		
	    $(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 = $("subscriber_email").value;

    if ($("subscriber_email").value != "") {
        if (strEmail.search(validRegExp) == -1) {
            FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vInvEmail + "\n";
        }
    }
    if ($("subscriber_email").value == "") {
        FieldList = FieldList + "<br />\n&nbsp;&bull;&nbsp;" + vEmail + "\n";
    }
    if (FieldList == ""){
        return ( true );
    } else {
        $("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 == ""){
		$("status_msg").innerHTML="Моля изчакайте до края на процеса...";
        return ( true );
    } else {
        $("error_msg").innerHTML = "Моля попълнете следните полета:\n\n" + FieldList;
        return( false );
	}
}
