
var agent = navigator.userAgent.toLowerCase()
var is_ns  = ((agent.indexOf('mozilla') != -1) && (agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)
                && (agent.indexOf('opera') == -1) && (agent.indexOf('webtv') == -1) && (agent.indexOf('hotjava') == -1))
var is_ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1))
var is_opera = (agent.indexOf("opera") != -1);
var win;
//last used page url without querry parameters
var lastpage_cookieName 			= 'last_page';
// name of the last used tab
var lasttab_cookieName 				= 'last_tab';
// id of the object that is the function required parameter
var last_objId_cookieName 			= 'last_objId';
//page on which iframe is located
var last_iframe_page_cookieName 	= 'last_iframe_page';
//id of the div object that iframe is encapsulated
var last_iframe_DivId_cookieName 	= 'last_iframe_DivId';
//id of the iframe 
var last_iframeId_cookieName 		= 'last_iframeId';
//iframe source
var last_iframeSrc_cookieName 		= 'last_iframeSrc';
//last form id cookie name
var last_formID_cookieName 			= 'last_formID';
//last customer id cookie name
var last_CLID_cookieName 			= 'last_CLID';
//last object id cookie name
var last_OBID_cookieName 			= 'last_OBID';
//last object category id cookie name
var last_CBID_cookieName 			= 'last_CBID';

//name of the tab currently selected on the page
var selected_tab_ID = 'Tab1';


function add_png(name, path){
	if(path==null) path="";
	if(document.all && !window.opera) {
	    if (/MSIE (5\.5)|[6789]/.test(navigator.userAgent) && navigator.platform == "Win32") {
	        document.write("<style>."+name+" {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='imgs/"+path+name+".png', sizingMethod='crop');}</style>");
	    }
	} else if (document.getElementById) {
	    document.write("<style>."+name+" {background:url('imgs/"+path+name+".png') no-repeat;}</style>");
	}
}

function getCurrentPage(){
	return document.location.href.replace(/\?.*/g,'');
	
}

function ChangeLang(LID, LAbs){
	setCookie("LID", LID);
	setCookie("LAbs", LAbs);
	document.location.reload(true);
}

function optionAdd(_objSelect,_text,_value, _id) {
    if (is_ie) {
        var _objOption = document.createElement("OPTION");
        _objOption.text = _text;
        _objOption.value = _value;
        _objOption.id = _id;
        _objSelect.add(_objOption);
    }else if (is_ns || is_opera) {
        var _nextOption = _objSelect.options.length;
        _objOption = new Option(_text,_value,_id);
        _objSelect.options[_nextOption] = _objOption
    }
}

function optionColorAdd(_objSelect,_text,_col_value,_value) {
    if (is_ie) {
        var _objOption = document.createElement("OPTION");
        _objOption.text = _text;
        _objOption.value = _value;
        _objOption.style.color = _col_value;
        _objSelect.add(_objOption);
    }else if (is_ns || is_opera) {
        var _nextOption = _objSelect.options.length;
        _objOption = new Option(_text,_value);
        _objSelect.options[_nextOption] = _objOption
    }
}

function optionInsert(_objSelect,_text,_value,_position) {
    _itemsTextsBackup = new Array()
    _itemsValuesBackup = new Array()
    for (i=_position;i<_objSelect.length;i++) {
        _itemsTextsBackup.push(_objSelect[i].text)
        _itemsValuesBackup.push(_objSelect[i].value)
    }

    optionDelete(_objSelect,_position)
    optionAdd(_objSelect,_text,_value)

    for (i=0;i<_itemsValuesBackup.length;i++)
        optionAdd(_objSelect,_itemsTextsBackup[i],_itemsValuesBackup[i])
}

function optionDelete(_objSelect,_start,_length) {
    if (_start == null || _start < 0)
        _start = 0;
    if (_length == null || _length < 0 || _length == 0)
        _elements = objSelect.length;
    else
        _elements = (_length > (objSelect.length - _start))	?objSelect.length - _start	:_start + _length;
    for (i=_start;i<_elements;i++)
        objSelect.options[_start] = null;
}

function optionSelect(_objSelect,_index) {
    _index = (_index == null || _index >= _objSelect.length)?0:_index;
    if (is_ie || is_opera)
        _objSelect.selectedIndex = _index
    if (is_ns)
        _objSelect.options[_index].selected = true
}

function arrayFind(arr,val) {
    for (var i=0;i<arr.length;i++) {
        if (arr[i] == val)
            return i;
    }
    return -1;
}

function showit(wwidth, wheight, wname, surl) {
//    if (wwidth == 0 || wheight == 0)
//        showPlace = open("",wname);
//    else
        showPlace = open(surl,wname,"scrollbars=no, status=yes");
        
    return false;    
}

function email_is_valid(_email) {
    email_re1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/
    email_re2 = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
    if (_email == "" || email_re1.exec(_email) || !email_re2.exec(_email))
        return false
    return true
}

//trim - Strip whitespace from the beginning and end of a string
function trim(_inStr) {
    _outStr = _inStr.replace(/(^\s+)|(\s+$)/g,"");
    return _outStr;
}

//ltrim - Strip whitespace from the beginning of a string
function ltrim(_inStr) {
    _outStr = _inStr.replace(/^\s+/g,"");
    return _outStr;
}

//rtrim - Remove trailing whitespace
function rtrim(_inStr) {
    _outStr = _inStr.replace(/\s+$/g,"");
    return _outStr;
}

//function document.onmouseover(){
//if ((event.srcElement) && (event.srcElement.tagName == "IMG" || event.srcElement.tagName == "A" || event.srcElement.tagName == "B")) 
//	return window.status="";
//}

function SetPerms(){
	
	_fObj = document.getElementById("GrAv");
	_fObj_right = document.getElementById("GrAl");
	num_of_options = _fObj.length;
	if (num_of_options){// if options exists
		value = _fObj[_fObj.selectedIndex].value;
	    text = _fObj[_fObj.selectedIndex].text;
		id_right = "GrAl_"+value;
		id_left = "GrAv_"+value;
	    if(value != 0 && document.getElementById(id_left)){
		    optionAdd(_fObj_right, text, value, id_right);
		    index = document.getElementById(id_left).index; 
		    _fObj.remove(index);
	//    	objSelect = _fObj;
	//    	optionDelete(_fObj,1,1);
	    }
	}
}

function UnSetPerms(){
	
	_fObj = document.getElementById("GrAl");
	_fObj_left = document.getElementById("GrAv");
	num_of_options = _fObj.length;
	if (num_of_options){// if options exists	
		value = _fObj[_fObj.selectedIndex].value;
	    text = _fObj[_fObj.selectedIndex].text;
		id_left = "GrAv_"+value;
		id_right = "GrAl_"+value;
	    if(value != 0 && document.getElementById(id_right)){
		    optionAdd(_fObj_left, text, value, id_left);
		    index=document.getElementById(id_right).index; 
		    _fObj.remove(index);
	//    	objSelect = _fObj;
	//    	optionDelete(_fObj,1,1);
	    }
	}
}

function SelectAll(_fObj){
	Available = document.getElementById("GrAv");
	Allowed = document.getElementById("GrAl");
	
	Av_options_num = Available.length;
	Al_options_num = Allowed.length;
	
	Available.multiple = true;
	Allowed.multiple = true;
	
//	GrAv_arr = new Array();
//	Available.focus();
//	for(i=0;i<Av_options_num;i++)
//		GrAv_arr[i] = Available.options[i].value;
	
	GrAl_arr = new Array();
	Allowed.focus();
	for(i=0;i<Al_options_num;i++)
		GrAl_arr[i] = Allowed.options[i].value;

		
//	_fObj.GrAv_list.value = GrAv_arr.join(",");	
	_fObj.GrAl_list.value = GrAl_arr.join(",");	
		
	return true;



}


/*
 * Show window with the help for templates creating
*/

function ShowTPLSHelp(obj,url){
	//If the checkbox isn't checked
	if (obj.checked){
		//Retriving user screen resolution
		user_window_width 	= window.screen.width;
		user_window_height 	= window.screen.height;

		//New window position
		window_left = user_window_width-300;
		window_top 	= user_window_height-800;
		
		win = window.open(url,'_blank',"height=400,width=300,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes,location=no,top="+window_top+",left="+window_left+"",true);
	}else{
		if (win!=null && win!=undefined)
			win.close();
	}
}

/*
 * Opens new window
*/

function OpenNewWindow(url, target){
	win = window.open(url,target);
	return win;
}


/**
 * @param object
 * @param string
 * @Desc Shows/hides the div tag using checkbox 
 */
function SHElem(ckBox,divID) {
	
	(!ckBox.checked)	?HideElem(divID) :ShowElem(divID);
	return ResizeUpperElem();
}

/**
 * @param object
 * @param string
 * @Desc Shows/hides the div tag using checkbox and loads the iframe content into itself 
 */
function SHLElem(ckBox, divID, iframeID, if_src) {
	if (document.getElementById(iframeID).src=='' || document.getElementById(iframeID).src==document.location.href)
		document.getElementById(iframeID).src = if_src;

	(!ckBox.checked)	?HideElem(divID) :ShowElem(divID);
	return ResizeUpperElem();
}

/**
 * @param object
 * @param string
 * @Desc Opens the div tag  and loads the iframe content into itself 
 */
function OLElem(_obj, an_f_ID, divID, iframeID, if_src, save_path) {
	if (save_path == null || save_path == undefined )
		save_path = false;
	document.getElementById(iframeID).src = if_src;
	
	//Unchecking the similar element
	try{
		document.getElementById(an_f_ID).text_checked = 'false';	
	}catch (e){}
	
	try{
		if (_obj.text_checked=='true'){
			HideElem(divID);
			_obj.text_checked = 'false';	
			
			if (save_path){
				//Saving this iframe load parameters
				setCookie(last_iframe_page_cookieName, '');	
				setCookie(last_objId_cookieName, '');	
				setCookie(last_iframe_DivId_cookieName, '');	
				setCookie(last_iframeId_cookieName, '');	
				setCookie(last_iframeSrc_cookieName, '');	
			}
		}else{
		 	ShowElem(divID);
			_obj.text_checked = 'true';	
			
			if (save_path){
				//Saving this iframe load parameters
				setCookie(last_iframe_page_cookieName, getCurrentPage());	
				setCookie(last_objId_cookieName, _obj.id);	
				setCookie(last_iframe_DivId_cookieName, divID);	
				setCookie(last_iframeId_cookieName, iframeID);	
				setCookie(last_iframeSrc_cookieName, if_src);	
			}
		}	
	}catch(e){}
	return ResizeUpperElem();
}

/**
 * @Desc Resizes upper element 
 */
function ResizeUpperElem() {
	try{
		//Iframe this document opened in
		upper_iframe 	= window.parent.document.getElementById(window.name+'_id');

		//Div element that upper_iframe is incapsulated in
		upper_iframe_div 	= upper_iframe.parentNode;

		//Updating upper div height (parent iframe parent tag)
		upper_iframe_div.style.height 	= document.body.scrollHeight;
	
		return true;
	}catch(e){
		return false;
	}
}
/**
 * @param object
 * @param string
 * @Desc Shows/hides the div tag using checkbox and resizes inner iframe to its body height 
 */
 
function  SHElemRes(ckBox,divID,iframeName) {
	(!ckBox.checked)	?HideElem(divID) :ShowElem(divID);
	document.getElementById(divID).style.height = (h = document.frames[iframeName].document.body.scrollHeight)>300	?300	:h;
	return ResizeUpperElem();
}

/**
 * @Desc Hides element 
 */
function HideElem(id){
	try{
		document.getElementById(id).style.display	= "none";
		return true;
	}catch(e){}
}

/**
 * @Desc Displays element 
 */
function ShowElem(id){
	try{
		document.getElementById(id).style.display	= "";
		return true;
	}catch(e){}
}

/**
 * Confirming the rename
 *
 */
function RenameConfirm(caption, error_msg, url, title, NewTitleSet, aID){
	var NewTitle;
	NewTitle = prompt(caption, (NewTitleSet==""?title:NewTitleSet));

	if (NewTitle!="" && NewTitle!=null && NewTitle!=title){
		incorrect = /[\?&@#\$%\^\*:\/\{\}\[\]~`]/;
		if (incorrect.exec(NewTitle)){
			alert(error_msg);
			return RenameConfirm(caption, error_msg, url, title, NewTitleSet, aID);
		}
		document.getElementById(aID).href += "&upd_val="+NewTitle;
		return true; 
	}else return false;
	
}

/**
 * Changes the background color
 *
 */
function ChBgColor(_fObj, MainColor, Color1){

//	MainColor	= MainColor.toUpperCase();
//	tabColor 	= '' + _fObj.style.backgroundColor.toUpperCase();

	_fObj.style.backgroundColor =_fObj.id == selected_tab_ID	?MainColor	:Color1; //( tabColor == MainColor )	?MainColor	:Color1;
	
	return true;	
}

/**
 * Changes the background color
 *
 */
function ChBgColorBack(_fObj, MainColor , Color1, Color2){

//	MainColor 	= MainColor.toUpperCase();
//	Color1 		= Color1.toUpperCase();
//	Color2 		= Color2.toUpperCase();
//	tabColor 	= '' + _fObj.style.backgroundColor.toUpperCase();

//	alert(_fObj.id +' '+selected_tab_ID);
	_fObj.style.backgroundColor = _fObj.id != selected_tab_ID ?Color2	:MainColor//( tabColor == Color1 )	?Color2	:MainColor;
	return true;	
}


/**
 *  @param TR object
 *  @param string (color)
 *  @param string (color)
 *	@desc Changes the background color (switches them)
 */
function ChangeBGColor(coll, TBgColor1, TBgColor2){
    TBgColor1 = TBgColor1.toUpperCase();
    TBgColor2 = TBgColor2.toUpperCase();
    if (coll!=null){
        for (i=0; i<coll.length; i++){
          coll[i].bgColor = (coll[i].bgColor.toUpperCase() == TBgColor1)	?TBgColor2	:TBgColor1;
        }
    }
}

//////NEW VERSION _ VORKING IN MOZILLA
function getIDsOneLevel(_fObj,tagName) {
    var res = -1;
    var tmp_arr = new Array();
    nodes = _fObj.parentNode.getElementsByTagName(tagName);
    for (j=0; j<nodes.length; j++) {
        if(nodes[j].parentNode == _fObj.parentNode) {
            tmp_arr.push("" + nodes[j].id);
        }
    }
    return tmp_arr;
}

function MoveUp(idPref,CID,TBgColor1,TBgColor2) {
	ShowDataSaveResultMsg('');
	try{
	    var CID 	= "" +idPref+ CID;
	    	_fObj 	= document.getElementById(CID);
	
	    ArrayID = getIDsOneLevel(_fObj,"TR");
	    count 	= arrayFind(ArrayID,CID);
	    if(count > 1){
	        _fObj_parent	= _fObj.parentNode;
	        _fObj_prev 		= document.getElementById(ArrayID[count - 1]);
	        _fObj 			= _fObj_parent.removeChild(_fObj);
	        _fObj_parent.insertBefore(_fObj, _fObj_prev);
	        ChangeBGColor(_fObj.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	        ChangeBGColor(_fObj_prev.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	    }
	}catch (e){
	}    
	return false;
}

function MoveDown(idPref,CID,TBgColor1,TBgColor2) {
	ShowDataSaveResultMsg('');
	try{
	    var CID = "" +idPref+ CID;
	    _fObj = document.getElementById(CID);
	
	    ArrayID = getIDsOneLevel(_fObj,"TR");
	    count = arrayFind(ArrayID,CID);
	    if(count < (ArrayID.length - 1)) {
	        _fObj_parent = _fObj.parentNode;
	        _fObj_next = document.getElementById(ArrayID[count + 1]);
	        _fObj_next = _fObj_parent.removeChild(_fObj_next);
	        _fObj_parent.insertBefore(_fObj_next, _fObj);
	        ChangeBGColor(_fObj.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	        ChangeBGColor(_fObj_next.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	    }
	}catch (e){
	}
    return false;
}

function MoveUpDupl(idPref,CID,CID_dupl,TBgColor1,TBgColor2) {
	ShowDataSaveResultMsg('');
	try{
	    var CID 		= "" +idPref+ CID;
	    var CID_dupl 	= "" +idPref+ CID_dupl;
	    _fObj 			= document.getElementById(CID);
	    _fObj_dupl 		= document.getElementById(CID_dupl);
	
	    ArrayID = getIDsOneLevel(_fObj,"TR");
	    
	    count 		= arrayFind(ArrayID,CID);
	    count_dupl 	= arrayFind(ArrayID,CID_dupl);
	    if(count > 2){
	        _fObj_parent	= _fObj.parentNode;
	        
	        _fObj_prev 		= document.getElementById(ArrayID[count - 2]);
	        _fObj_prev_dupl = document.getElementById(ArrayID[count_dupl - 2]);
	        
	        _fObj 			= _fObj_parent.removeChild(_fObj);
	        _fObj_dupl 		= _fObj_parent.removeChild(_fObj_dupl);
	        
	        _fObj_parent.insertBefore(_fObj_dupl, _fObj_prev);

	        _fObj_parent.insertBefore(_fObj, _fObj_dupl);
	        ChangeBGColor(_fObj.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	        ChangeBGColor(_fObj_prev.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	    }
	}catch (e){
	}    
	return false;
}

function MoveDownDupl(idPref,CID,CID_dupl,TBgColor1,TBgColor2) {
	ShowDataSaveResultMsg('');
	try{
	    var CID 		= "" +idPref+ CID;
	    var CID_dupl 	= "" +idPref+ CID_dupl;
	    _fObj 			= document.getElementById(CID);
	    _fObj_dupl 		= document.getElementById(CID_dupl);
	
	    ArrayID = getIDsOneLevel(_fObj,"TR");
	    
	    count 		= arrayFind(ArrayID,CID);
	    count_dupl 	= arrayFind(ArrayID,CID_dupl);
	    if(count_dupl < (ArrayID.length - 2)) {
	        _fObj_parent 	= _fObj.parentNode;
	        
	        _fObj_next 		= document.getElementById(ArrayID[count + 2]);
	        _fObj_next_dupl = document.getElementById(ArrayID[count_dupl + 2]);
	        
	        _fObj_next 		= _fObj_parent.removeChild(_fObj_next);
	        _fObj_next_dupl = _fObj_parent.removeChild(_fObj_next_dupl);
	        
	        _fObj_parent.insertBefore(_fObj_next_dupl, _fObj);
	        
	        _fObj_parent.insertBefore(_fObj_next, _fObj_next_dupl);
	        ChangeBGColor(_fObj.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	        ChangeBGColor(_fObj_next.getElementsByTagName("TD"),TBgColor1,TBgColor2);
	    }
	}catch (e){
	}
    return false;
}

function CreateListOrderID(inpID,tableID, string_count) {
    var coll = document.getElementById(tableID).getElementsByTagName("TBODY")[0].getElementsByTagName("TR");
    CIDs = new Array();
    if (coll != null) {
        for (i=0; i<coll.length; i++) {
            if(coll[i].id != "")
                CIDs.push((coll[i].id.substr(string_count)));
        }
    }
    //Input field to store IDs order
    document.getElementsByName(inpID).item(0).value = CIDs.join(",");
}



function setCookie(cookieName, cookieValue){
//	today = new Date();
//	cookieExpire = new Date();
//    cookieExpire.setTime(today.getTime()+1000000);
//    var today = new Date();
//    var expires = new Date();
//    expires.setTime(today.getTime() + 1000*60*60*24*365);
    document.cookie = cookieName + "=" + escape(cookieValue);// + "; expires=" + expires.toGMTString();
	
}

function getCookie(cookieName) {
    var search = cookieName + "=";
    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;
            return unescape(document.cookie.substring(offset, end));
        }
    }
    return false;
}



/**
 * @return boolean
 * @param srting $login
 * @desc Checks given login for CMS corresponding. Returns <b>true</b> if login is corrrect and <b>false</b> in case some errors found 
*/
function IsCorrectLogin(login, login_min_length, login_max_length) {
    var re = new RegExp("^[a-zA-Z0-9_\\-\\.]{"+login_min_length+","+login_max_length+"}$");
    return re.test(login);
}

/**
 * @Desc Changes the listing type 
 */
function ChangeListRecords(url,obj,title, last_val){
	
	RT = 0;
	
	if (document.getElementById('RecordsTotal')!=undefined && document.getElementById('RecordsTotal')!=null)
		//Total number of records available
		RT = Math.round(document.getElementById('RecordsTotal').innerText);
	
	val = obj.value;
    NCRefresh = /(&|\?)?(NCRefresh)=[^&]*/g

	last_val = last_val==''	?'0,'	:last_val;
	vals_arr = last_val.split(',');
	
	first_val = val>RT	?'0'	:vals_arr[0];
	
	val = ''+first_val+','+val;
	
	if (url.search(title+"="+last_val)!=-1){
		eval("re = /"+title+"="+last_val+"/g;");
		url = url.replace(re,title+"="+val);
	}else{
		url+='&'+title+"="+val;
	}

	url = url.replace(NCRefresh,'');

	
	url = 'http://'+location.host+location.pathname+'?'+url;
	document.location.href = url;

}

function fixDate(_year,_month,_date) {
    date_year 	= _year[_year.selectedIndex].value
    date_month 	= _month[_month.selectedIndex].value -1;
    date_date 	= _date[_date.selectedIndex].value

    objSelect = _date;

    optionDelete(objSelect,1)

    defaultIndex = 0
    for (i=1;i<=31;i++) {
        test_date = new Date(date_year,date_month,i)

        if (i == test_date.getDate())
            optionAdd(objSelect,i,i)
        if (i == date_date)
            defaultIndex = i
    }
    optionSelect(objSelect,defaultIndex)
} 

function InNumber(e){
	
//	try{
		var keyChar = window.event ? event.keyCode : e.which;
//			alert(keyChar);
		if (keyChar == 46 || keyChar == 9 || keyChar == 8 || keyChar == 37 || keyChar == 39 || keyChar == 13 || (keyChar > 47 && keyChar < 58) 
//		|| (keyChar > 97 && keyChar < 106) 
){
//			alert('ok');
		        return true;
		}
		return false;
//	}catch(e){
//	
//	}	
}

function PureNumber(e){
	
	var keyChar = window.event ? event.keyCode : e.which;
	if (keyChar > 47 && keyChar < 58 ){
	        return true;
	}
	return false;
}

function IsHour(_inp){
	var keyChar = window.event ? event.keyCode : e.which;
	value 	= _inp.value;
	v_l		= value.length;
	if (v_l==1){
	
	}		
}

function CheckNumber(_inp){
	numb = /[^0-9]/g;
	try{
		_inp.value = _inp.value.replace(numb, '');
	}catch(e){
		alert(e.message);
	}
}

function CheckFloatNumber(_inp){
	numb = /[^0-9\.]/g;
	try{
		_inp.value = _inp.value.replace(numb, '');
	}catch(e){
		alert(e.message);
	}
}

/**
 * @Desc Compares two date times and returns 0 if the are equal, 1 if first one is greater, and -1 if second one is greater;
**/

function CmpDateTime(s_year, s_month, s_day, s_hour, s_minute, e_year, e_month, e_day, e_hour, e_minute){
	res = 	s_year == e_year
			?(	s_month == e_month
				?(	s_day == e_day
					?(	s_hour == e_hour
						?(	s_minute == e_minute
							?0
							:(s_minute > e_minute	?1	:-1)
						 )
						:(s_hour > e_hour	?1	:-1)
					 )
					:(s_day > e_day	?1	:-1)
				 )
				:(s_month > e_month	?1	:-1)
			 )
			:(s_year > e_year	?1	:-1);

	return res;					
				
}



/**
 * Changes the background color
 *
 */
function SetTabActive(_fObj, ShElemid, hideIdsArr, Color, Color2, TabIdStart, save_last_tab){
	Tabs_num = hideIdsArr.length;

	save_last_tab = save_last_tab==undefined	?false	:save_last_tab;
	try{
		//Changing all tabs color to the default one
		for(i=0;i<=Tabs_num;i++){
			curId = TabIdStart+(i+1);
	
			if (curId== _fObj.id)	continue;
	
			document.getElementById(curId).style.backgroundColor 			= Color2;
			document.getElementById(curId+'_bottom').style.backgroundColor 	= Color2;
			document.getElementById(curId+'_caption').style.fontWeight  	= 'normal';
		}
		
		//Changing curent object color
		_fObj.style.backgroundColor = Color;
		document.getElementById(_fObj.id+'_bottom').style.backgroundColor 	= Color;
		document.getElementById(_fObj.id+'_caption').style.fontWeight  		= 'bold';
		
		
	
		//Hiding all tab forms
		for(i=0;i<Tabs_num;i++){
			HideElem(hideIdsArr[i]);
		}
		
		//Displaying current selected tab
		ShowElem(ShElemid);

		if (save_last_tab){
			//Saving tab sa last visited tab into the cookie
			setCookie(lasttab_cookieName, ShElemid);
			
			//Setting this page last params (like: OBFormID, OBID, CLID etc.)
			SetSwitchCookieParams();
			
			//saving selected tab ID
			selected_tab_ID = _fObj.id;
		}
	}catch(e){}
	
}


/**
 * Sets the last time active tab (after page refresh only)
 *
 */
function SwitchToLastTab(allIdsArr, Color, Color2, TabIdStart){
	try{
		url_re = /\?.*/g;
		
		//Retriving the last visited page
		last_page = getCookie(lastpage_cookieName);
		if (last_page)
			last_page = last_page.replace(url_re,'');
		
		//Retriving the current page
		cur_page = getCurrentPage();
		
		//If the page is the same (has been refreshed)
		if (cur_page!=last_page){
			setCookie(lastpage_cookieName, cur_page);
			return false;
		}else{	
		
			//retriving ther last used tab
			last_tab = getCookie(lasttab_cookieName);
			
			//If no tab set
			if (!last_tab)
				return false;
				
			//Retriving all paramaters for the form distinguishing
			url = document.location.search;
			//Retriving CBID
			CBID = ReturURLParam(url, 'CBID');

			if (CBID!=null){
				//retriving the last object id
				last_CBID = getCookie(last_CBID_cookieName);
				//retriving the last category id
				if (last_CBID!=CBID)
					return false;
			}
			
			//Number of all tabs on the page
			Tabs_num = allIdsArr.length;
	
			hideIdsArr  = new Array();
			active_tabId = '';
			j= 0;
			//Ganging all tabs color to the default one
			for(i=0;i<Tabs_num;i++){
				//If the tab matches the last visited one
				if (last_tab==allIdsArr[i]){
					active_tabId = TabIdStart+(i+1);
					continue;
				}
				hideIdsArr[j] = allIdsArr[i];
				j++;
				
			}
			
			//Switching to the last used tab
			SetTabActive(document.getElementById(active_tabId), last_tab, hideIdsArr, Color, Color2, TabIdStart);
			last_iframe_page = getCookie(last_iframe_page_cookieName);
			if (last_iframe_page){
				//checking whether the last page of the opened div was this one page
				if (cur_page==last_iframe_page){
					try{
						_obj 	= document.getElementById( getCookie( last_objId_cookieName ) );
						divID 	= getCookie( last_iframe_DivId_cookieName);
						iframeID= getCookie( last_iframeId_cookieName);
						if_src	= getCookie( last_iframeSrc_cookieName);
						
						OLElem(_obj, '', divID, iframeID, if_src);
					}catch(e){}
				}
			}
		}
	}catch(e){}
}

/**
 * @Desc Displays popup thumbnail of the image 
 */
function ShowThumb(url,file_name,size){
//  alert(document.body.scrollTop);
    
    mouse_x = event.x;
    mouse_y = event.y+document.body.scrollTop;
//window.innerWidth - for Netscape  
    
    //Popup image coordunates
    x = mouse_x+20;
    y = mouse_y;
    
    window_height   = document.body.clientHeight;
    win_scroll      = window_height-y;
    
//  x = (window_width>(mouse_x+size))   ?mouse_x    :mouse_x-size;
//  y = (window_height>(mouse_y+size))  ?mouse_y    :mouse_y-size+10;
    
    img = document.getElementById("THUMB");
    
    img.src = url+"&img="+file_name+"&size="+size;
    img.style.top   = y;
    img.style.left  = x;
    img.style.display   = "";

    return true;
}

/**
 * @Desc Displays popup thumbnail of the image 
 */
function HideThumb(){
    img = document.getElementById("THUMB");
    img.src = "";
    img.style.display   = "none";
    return true;
}


/*
@desc Changes the message textr displaying right next to the language tabs
*/
function ShowDataSaveResultMsg(msg){
	try{
		document.getElementById('msg_txt').innerHTML = msg + "<br>";
	}catch (e){
	}	
}


function ResizeUpperFrames(){
	//if upper frame and DIV were resized then resizing thier upper frame and div
	if (ResizeUpperElem()){
		try{
				
			//Iframe parent iframe this document opened in
			upper_iframe2 	= window.parent.parent.document.getElementById(window.parent.name+'_id');
	
			//Div element that upper_iframe is incapsulated in
			upper_iframe_div2 	= upper_iframe2.parentNode;
	
			//Updating upper div height (parent iframe parent tag)
			upper_iframe_div2.style.height 	= window.parent.document.body.scrollHeight;

		}catch(e){}
	}

}


function RemURLParams(url, params){
	re = new  RegExp("("+params.join('|')+")=.*?(\&|$)");
	url = url.replace(re,"");
	return url.replace(/&+/g,'&');
}

function ReturURLParam(url, param){
	try{
		re 	= new RegExp(param+"=(.*?)(\&|$)");
		r 	= url.match(re);
		return r[1];
	}catch(e){
		return null;
	}
}

function MakeListingUrl(ref){
	return ref+'&CID='+ (document.getElementById('CBID').value?document.getElementById('CBID').value:'d92036b1ca0ff7eaf2fb6309425ce11c')+'&CBID='+document.getElementById('CBID').value;
}

function SetSwitchCookieParams(){
	url = document.location.search;
	//Retriving CBID
	CBID = ReturURLParam(url, 'CBID');
	//Saving object editing form path	
	if (CBID!=null ){
		setCookie(last_CBID_cookieName, CBID);
	}
}


function ActivateElement(_obj, obj_ID){
	try{
		if (_obj.selected != undefined || _obj.checked != undefined){
			target_obj = document.getElementById(obj_ID);
			
			if (target_obj.checked !=undefined)
				target_obj.checked = false;
				
			if (target_obj.selected !=undefined)
				target_obj.selected = (_obj.selected || _obj.checked)==false	?true	:target_obj.selected;
			target_obj.disabled = !(_obj.selected || _obj.checked);
		}
	}catch(e){}
}



function replace(str1, str2, text) { 
    var r, re, rez;
    r = /(?=[\{\}\[\]\(\)\^\$\.\|\*\+\?\\])/g;
    str1 = str1.replace(r, "\\");
    re = new RegExp(str1);
    rez = text.replace(re, str2);
    return rez;
}
    
    