/** Displays confirmation dialog ("Are you sure?").
 * @return bool true - user have clicked 'yes', false - 'no'
 */
function sure(sMsg){
	var sMess;
	sMess = (sMsg && sMsg.length > 10) ? sMsg : msg[0];
	var aMessages = aMessages;
    if (aMessages && aMessages['js.sure'])
    	 sMess = aMessages['js.sure'];
    return confirm(sMess);
}

function formatMes(sKey)
{
    var i = 1;
    var sMsg = aMessages[sKey];
    var sMsg2 = aMessages[sKey];
    while (sMsg.length)
    {
        if (sMsg.search(/%s/) != -1)
        {
            sMsg = sMsg.replace(/.*?(%s.*|\n)/ , '$1');
            sMsg = sMsg.replace(/^%s/, '');
            sMsg2 = sMsg2.replace(/%s/, arguments[i]);
            i++;
        }
        else
        {
            sMsg = sMsg.replace(/.*/, '');
        }
    }
    return sMsg2;
}
/** Open popup window.
 * @param string url       url to open in popup window
 * @param string name      window name
 * @param int    width     window width
 * @param int    height    window height
 * @param bool   bIsResize true - window resizable, false - fixed size
 */
function openWin(url, name, width, height, bIsResize){
  if (!width)
        width = screen.width*0.9;
  if (!height)
        height = screen.height*0.75;
  IsReize = bIsResize?1:0;
  var newWindow = window.open(url, name, 'left=' + Math.ceil((screen.width - width)/2) + ',top=' + Math.ceil((0+screen.height - height)/2) + ',width=' + width + ',height=' + height + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable='+bIsResize+',channelmode=0,fullscreen=0');
  return false;
}


/** Selects/deselects all checkbox with given name
 * @param string name checbox name
 * @param bool   val  true - select, false - deselect boxes
 */
function selectAll(name,val){
        a = document.getElementsByName(name);
        for(i=0;i<a.length;++i) {
                a[i].checked =   val;
        }
}
//check checkbox with id 
function checkId(iId)
{
	if(document.getElementById(iId).checked)
		document.getElementById(iId).checked=false;
	else
		document.getElementById(iId).checked=true;
}

/** Returns value of selected item (singel or first for multiselect)
 *  in SELECT element.
 * @param string sName select element name
 * @return mixed selected element value or 0 - if none selected
 */
function getSelectedId( sName ){
        var a = document.getElementsByName(sName);
        for(i=0;i<a.length;++i)
                if(a[i].checked) return a[i].value;
        return 0;
}

function getSelectedAllId( sName ){
	var a = document.getElementsByName(sName);
	var aIds=new Array();
	bChecked=false;
	for(i=0;i<a.length;++i)
		if(a[i].checked) aIds[i]=a[i].value;
	return aIds;
}
/** Clear form elements. Used insted reset() when neede clear elements
 *  but not reset to start values.
 * @param string formName name form to clear
 */
function clearForm(formName)
{
        formName.reset();
        var a = formName.elements;
        for(i=0;i<a.length;++i)
        {
                type_input = a[i].type.toLowerCase();
                if(type_input == 'select-one') {
                        a[i].selectedIndex = 0;
                } else if (type_input == 'checkbox') {
                        a[i].checked = false;
                } else if (type_input == 'file') {
                } else if (type_input == 'submit') {
                } else if (type_input == 'button') {
                } else {
                        a[i].value = '';
                }
        }
}



/** Inserts text in textarea (replaces selection or insert to the end of text)
 * @param object oTextArea textarea object
 * @param string sText     text to insert
 * @return bool true
 */
function insertToTextArea(oTextArea, sText) {
    //for IE
    if (document.selection) {
            oTextArea.focus();
            oSel = document.selection.createRange();
            oSel.text = sText;
    }
    //for MOZILLA/NETSCAPE
    else if (oTextArea.selectionStart || oTextArea.selectionStart == "0") {
            var startPos  = oTextArea.selectionStart;
            var endPos    = oTextArea.selectionEnd;
            var str       = oTextArea.value;

            oTextArea.value = str.substring(0, startPos) + sText + str.substring(endPos, str.length);
    } else {
            oTextArea.value += sText;
    }

    return true;
}

/** Show Date Time
 *
 */

/**
 * Validate date
 */
function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/]\d{1,2}[\/]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate( ) == day) {
            if (testDate.getMonth( ) + 1 == mo) {
                if (testDate.getFullYear( ) == yr) {
                    return true;
                } else {
                    alert(msg[1]);
                }
            } else {
                alert(msg[2]);
            }
        } else {
            alert(msg[3]);
        }
    } else {
        alert(msg[4]);
    }
    return false;
}

/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
	document.write('<style type="text/css">\n')
	document.write('.submenu{display: none;}\n')
	document.write('</style>\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}

function get_cookie(Name) { 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function onloadfunction(){
	if (persistmenu=="yes"){
		var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
		var cookievalue=get_cookie(cookiename)
		if (cookievalue!="")
			document.getElementById(cookievalue).style.display="block"
	}
}

function savemenustate(){
	var inc=1, blockid=""
	while (document.getElementById("sub"+inc)){
		if (document.getElementById("sub"+inc).style.display=="block"){
			blockid="sub"+inc
			break
		}
		inc++
	}
	var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
	var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
	document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
	window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
	window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
	window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
	window.onunload=savemenustate

/*
* Return javascript error
*/
window.onerror=function(m,u,l)
{
	window.status = "Java Script Error: "+m+" - Line:"+l;
	return true;
}
