BROWSER = new Object();
BROWSER.isOpera = navigator.userAgent.indexOf("Opera") > -1;
BROWSER.isIE    = navigator.userAgent.indexOf("MSIE") > 1 && !BROWSER.isOpera; 
BROWSER.isMoz   = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !BROWSER.isOpera;

if (typeof($) != "function") $ = function(ID) { return document.getElementById(ID); }

function openHTMLEditor(page, type) {
    window.open('htmlpagesedit.php?ownerid=' + page + '&typeid=' + type, '_new', 'resizable=yes, scrollbars=yes');
}

function openPopUp(page) {
    window.open(page, '_new', 'resizable=yes, scrollbars=yes');
}


function openPopUpSize(page) {
    window.open(page, '_new', 'resizable=yes, scrollbars=yes,width=650,height=700');
}

function openHTMLEditorNew(contentID, previewID, editText) {
   var loc = 'htmledit.php?field=' + contentID;
   var previewObj = document.getElementById(previewID);
   if (previewObj) loc += "&preview=" + previewID;
   if (editText) loc += "&edittext=1";
   editorWin = window.open(loc, '_new', 'width=770, height=500, scrollbars=no, resizable=yes');
   editorWin.focus();
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function SetPort(portcontrol, portchooser){
	control_hidden = MM_findObj(portcontrol);
	control_visible = MM_findObj(portcontrol+"_view");
	control_chooser = MM_findObj(portchooser);
	if(control_hidden && control_visible && control_chooser){
		control_hidden.value = control_chooser.value;
		control_visible.value = control_chooser.options[control_chooser.selectedIndex].text;
	}
}

function checkAll(field, all) {
    var theform=document.forms['form1'];
    var checka=theform[field];

    if (theform[all].checked) {
            if(checka.length){
                for (i = 0; i < checka.length; i++) {
                checka[i].checked = true;}
            }else{
            checka.checked = true;
            }
    }else{
    if(checka.length){
                for (i = 0; i < checka.length; i++) {
                checka[i].checked = false;}
            }else{
            checka.checked = false;
            }
    }

}

function checkAllForm(field, all, formName) {
    var theform=document.forms[formName];
    var checka=theform[field];

    if (theform[all].checked) {
            if(checka.length){
                for (i = 0; i < checka.length; i++) {
                checka[i].checked = true;}
            }else{
            checka.checked = true;
            }
    }else{
    if(checka.length){
                for (i = 0; i < checka.length; i++) {
                checka[i].checked = false;}
            }else{
            checka.checked = false;
            }
    }

}

function clearValue(textfieldId, imgID) {
	var item = document.getElementById(textfieldId);
    if (imgID) {
		img = $(imgID);
		if (img.tagName == "IMG") {
			img.height = 0;
		}
		if (img.tagName == "DIV") {
		    img.innerHTML = "";
		}
	}
	item.value = "";
}

editorWin = null;
function openHTMLEditor_new(contentID, previewID, editText) {
   var loc = 'htmledit.php?field=' + contentID;
   var previewObj = document.getElementById(previewID);
   if (previewObj) loc += "&preview=" + previewID;
   if (editText) loc += "&edittext=1";
   editorWin = window.open(loc, '_new', 'width=770, height=500, scrollbars=no, resizable=yes');
   editorWin.focus();
}

function redirect(id) {
   alert("This link works only in front-end site!")
}

function popupimg(img) {
	var win = window.open("/enlarge.php?src="+img, "Popup","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=200,height=200");
	win.focus();
}

// Display image/flash when input field source was changed
function setImage(imgInput, img) {
	var inputObj = document.getElementById(imgInput);
	if (!imgInput) return false;
	var imgObj = document.getElementById(img);
	if (!imgObj) return false;
	inputObj.imgObj = imgObj; 
	
	inputObj.onchange = inputObj.onblur = function() {
    	
    	if (this.value == this.currentValue) return;
    	
    	var isIMG = this.value.match(/\.(jpg|gif|png|bmp)$/g), val;
    	
        if (this.value.match(/.(swf|flv)$/)) {
		    var divNode = document.createElement('DIV');
		    divNode.id = this.imgObj.id;
		    val = this.value.match(/.swf$/) ? this.value : "/flash/vplayer.swf?vid=" + this.value + "&vidTitle=" + this.value;
		    var so = new SWFObject(val, "mymovie", "220", "230", "8", "#336699");
		    this.imgObj.parentNode.replaceChild(divNode, this.imgObj);
		    this.imgObj = document.getElementById(divNode.id);
		    so.write(divNode.id);
		} else if (this.imgObj.tagName == "DIV" && isIMG){
		    var imgNode = document.createElement('IMG');
		    imgNode.id = this.imgObj.id;
		    imgNode.border = "0";
			this.imgObj.parentNode.replaceChild(imgNode, this.imgObj);
			this.imgObj = document.getElementById(imgNode.id);
			this.imgObj.src = this.value;
		} else if (this.imgObj.tagName != "DIV" && isIMG) {
			var Img = new Image();
			Img.src = this.value;
			this.imgObj.height = 0;
			this.imgObj.src = this.value;
			Img.virtualImg = this.imgObj;
			Img.onload = function(){ this.virtualImg.height = this.height; }
			this.imgObj.onerror = function() { this.height = 0; }
			this.imgObj.virtualImg = Img;
			var res = executeSlow("setImageHeight('"+ this.imgObj.id +"')", 2);
			if (BROWSER.isOpera && !res) this.imgObj.height = 0;
		} else if (this.imgObj.tagName == "DIV" && !isIMG) {
			this.imgObj.innerHTML = this.value.match(/^\s*$/) ? '' : '<div style="cursor:pointer" onclick="VALIDATE.redirect(\'/download.php?file='+ this.value +'\', true)">Download</div>';
		} else {
			//this.imgObj.height = 0;
		    var divNode = document.createElement('DIV');
		    divNode.id = this.imgObj.id;
		    this.imgObj.parentNode.replaceChild(divNode, this.imgObj);
		    this.imgObj = document.getElementById(divNode.id);
			this.imgObj.innerHTML = this.value.match(/^\s*$/) ? '' : '<div style="cursor:pointer" onclick="VALIDATE.redirect(\'/download.php?file='+ this.value +'\', true)">Download</div>';
		}
		this.currentValue = this.value;
	}
}

function setImageHeight(imgID) {
   var Img = document.getElementById(imgID);
   if (!Img) return false;
   if (Img.virtualImg.fileSize>0 || (BROWSER.isOpera && Img.virtualImg.height>0)) {
      Img.height = Img.virtualImg.height;
      return true;
   } else {
      return false;
   } 
} 

function executeSlow(func, seconds, counter) {
   if (!counter) counter = 0;
   var res = eval(func);
   if (res) return true;
   else if (counter>=seconds*1000) return false;
   else setTimeout("executeSlow(\""+func+"\","+seconds+","+(counter+100)+")", 100);
}

MANAGER = new Object();

// open file manager window
MANAGER.openFileManager = function(fieldID) {
   var loc = "../backoffice/ajaxfilemanager/ajaxfilemanager.php";
   openWin(loc, 'fileManager', 800, 420, "");
   MANAGER.field = document.getElementById(fieldID);
}

// handle file manager returned url
MANAGER.handleURL = function(url) {
	if (!MANAGER.field) return;
	MANAGER.field.value = url;
	if (typeof(MANAGER.field.onchange) == "function") MANAGER.field.onchange();
} 

// open classic popup window
var win_array = [];
function openWin(loc, winName, winWidth, winHeight, winParams){
   if (!win_array[winName]) win_array[winName] = null;
   if (!win_array[winName] || (win_array[winName] && win_array[winName].closed)){
      if (winParams=='full'){
         winParams = 'resizable=yes,menubar=yes,status=yes,toolbar=yes,titlebar=yes,location=yes,directories=yes,scrollbars=yes';
      }
      win_array[winName] = window.open(loc, winName, "width="+winWidth+",height="+winHeight+","+winParams);
   }

   win_array[winName].location = loc;
   win_array[winName].focus();
   return win_array[winName];
}


// open modal dialog window
function openModal(loc, param, dWidth, dHeight, winParams){
   if (winParams=='full'){
      winParams = 'resizable:yes,status:yes;scroll:yes;help:yes;edge:raised;unadorned:yes';
   }
   if (typeof(winParams)=='undefined'){
      winParams = 'resizable:yes,status:no;scroll:yes;help:yes;edge:raised;unadorned:yes';
   }
      
   if (window.showModalDialog){
       window.showModalDialog(loc, param, "dialogWidth:"+dWidth+"px;dialogHeight:"+dHeight+"px;"+winParams);
   }
   else {
       var win = window.open(loc, 'editWin','height='+dHeight+',width='+dWidth+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
	   win.location = loc;
       win.focus();
   }
}

function in_array(el, arr) {
	for(var i=0; i<arr.length; i++) if (arr[i] == el) return true;
	for(var i in arr) if (arr[i] == el) return true;
	return false;
}

// Change color of elements with lightTagName,
// which are children of elemID. It works on mouse over/out event.
function hoverLight(elemID, lightTagName, ignoreTags) {
	var elem = $(elemID);
	if (!elem) return;
	var elems = elem.getElementsByTagName(lightTagName);
	for(var i=0; i<elems.length; i++) {
		elems[i].oldColor = elems[i].style.backgroundColor;
		elems[i].onmouseover = function(){
			var children = this.getElementsByTagName('*');
			for(var i=0; i<children.length; i++) {
			    if (this.ignoreTags && this.in_array(children[i].tagName, this.ignoreTags)) continue;
				children[i].style.backgroundColor = "#DCE4F5";
		    }
		}
		elems[i].onmouseout = function() {
			var children = this.getElementsByTagName('*');
			for(var i=0; i<children.length; i++) {
				if (this.ignoreTags && this.in_array(children[i].tagName, this.ignoreTags)) continue;
				children[i].style.backgroundColor = this.oldColor;
		    }
		}
		if (ignoreTags) elems[i].ignoreTags = ignoreTags.split(",");
		// hack for Mozilla
		elems[i].in_array = in_array;
	}
}

// Check All
function check(formName, fieldName, allName) {
    var theform = document.forms[formName];
    var checks = theform[fieldName];
    if (!checks.length){
        checks.checked = theform[allName].checked;
    }
    for (i = 0; i < checks.length; i++) {
         checks[i].checked = theform[allName].checked;
    }
}

// Check one
function validateChecks(formName, fieldName, allName){
    var theform = document.forms[formName];
    var checks = theform[fieldName];
    var fullchecked = true;
    if (!checks.length){
        fullchecked = checks.checked ? true : false
    }
    for (i = 0; i < checks.length; i++) {
         if (!checks[i].checked){ 
             fullchecked = false;
             break;
         }
    }
    if (theform[allName].checked && !fullchecked) theform[allName].checked = false;
    if (!theform[allName].checked && fullchecked) theform[allName].checked = true;
}

function initCheckboxHandler(formName, fieldName, allName) {
    var theform = document.forms[formName];
    var checks = theform[fieldName];
    if (!checks.length) {
    	checks.onclick = new Function("validateChecks('" + formName + "','" + fieldName + "','" + allName + "')");
    } 
    for (i = 0; i < checks.length; i++) {
    	checks[i].onclick = new Function("validateChecks('" + formName + "','" + fieldName + "','" + allName + "')");
    }
    theform[allName].onclick = new Function("check('" + formName + "','" + fieldName + "','" + allName + "')");
    validateChecks(formName, fieldName, allName);
}

function syncPreview(srcID, targetID) {
   var src = document.getElementById(srcID);
   var target = document.getElementById(targetID);
   if (!src || !target) return false;
   src.target = target;
   src.onkeyup = function(){ this.target.innerHTML = this.value; }
}

function setBannerSizeInfo(selectID, infoID) {
	var sel = document.getElementById(selectID);
	var info = document.getElementById(infoID);
	if (!sel || !info) return;
	if (!sel.infoBlock){
	    sel.infoBlock = {"sel":sel, "info":info};
	    sel.onchange = sel.onkeyup = function(){ 
	    	var val = this.options[this.selectedIndex].getAttribute("info");
	    	this.infoBlock.info.innerHTML = val ? val : ""; 
	    }
	    sel.onchange();
	}
}

 function imgpopup(theURL) {
        window.open("/showbigimage.php?src="+theURL, "Image","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=200,height=200");
    }
function hilite (obj,status) {
	if (status){
		obj.style.backgroundImage='url(/images/ship_bg_on.gif)';
		obj.style.cursor = 'pointer';
	}
	else{
		obj.style.backgroundImage='url(/images/ship_bg.gif)';
	}
}
function changesubimg( obj,status ){
	if (status){
		document.getElementById(obj).src = '/images/ship_bul.gif';
	}
	else{
		document.getElementById(obj).src = '/images/spacer.gif';
	}
}
function changesubimg1( obj,status ){
	if (status){
		document.getElementById(obj).src = '/images/ship_bul_sub.gif';
	}
	else{
		document.getElementById(obj).src = '/images/spacer.gif';
	}
}

function changeimg( obj, img ){
	document.getElementById(obj).src = img;
}

// faq function
function setContextOpen(clickArr, showArr, changeArr) {
	var i, j, k, m;
	for (j=0;j<clickArr.length;j++) {
		for(i=1; i<200; i++) {
		    var Obj = document.getElementById(clickArr[j] + i);
		    if (!Obj) break;
		    for(k = 0; k < showArr.length; k++) {
			    var sObj = document.getElementById(showArr[k] + i);
			    if (!sObj) break;
			    Obj.showObj = sObj;
			    Obj.openView = false;
			    for(m in changeArr) {
			        var cElemFir = document.getElementById(changeArr[m][0]+i);
			        var cElemSec = document.getElementById(changeArr[m][1]+i);
				    if (cElemFir && cElemSec) {
				        Obj.cElemFir = cElemFir;
				        Obj.cElemSec = cElemSec;
				    }
				}
				
			    Obj.onclick = function() {
			        if (this.openView) {
			           this.showObj.style.display = "none";
			           this.openView = false;
			           if (this.cElemFir) {
			           	   this.cElemFir.style.display = "block";
			           	   this.cElemSec.style.display = "none";
			           }
			        } else {
			           this.showObj.style.display = "block";
			           this.openView = true;
			           if (this.cElemFir) {
			           	   this.cElemFir.style.display = "none";
			           	   this.cElemSec.style.display = "block";
			           }
			        }
			    }
			}
		}
    }
}


function fancyPopup(l, w, h){
    var sep = l.href.match(/\?/) ? '&' : '?';
    if (!w) w = 600;
    if (!h){
        var h90 = $(window).height();
        h90 = Math.round(h90 * .8);
        h = h90; 
    }

    $.fancybox({
		'width'		    : w,
		'height'		: h,
		'href'			: l.href + sep + 'inlinePopup=1',
		'type'			: 'iframe'
    });
    return false;
}

