function log(txt){
    $('#log').append($('<div></div>').html(txt));
}
//

function getLoc_hashtable_from_cookie(){
 
 var str=$.cookie("loc_hashtable");
// alert("begin1="+str);
 
 var newHashLoc=new Hashtable();
 var arrayLoc,oneLoc;
    if(null!=str){
        arrayLoc=str.split("#");
         for (var i=0; i < arrayLoc.length; i++) {
            if(""==arrayLoc[i]){ ;break;}
            oneLoc=arrayLoc[i].split("|");
           // alert(oneLoc[0]+":"+oneLoc[1]);
            newHashLoc.put(oneLoc[0],oneLoc[1]);
         }
      
    }
    return newHashLoc;
}
var loc_hashtable=getLoc_hashtable_from_cookie();
 
var const_int=-243;//using in location.jsp
/** @param
    idSel - індентифкатор тега, що містить значення для запиту
    idChildSel - індетифікатор тега, куди записується результат
    param - параметр, що вказує на команду
*/
function loadChoosenSelect(idParentSel,idSel,idChildSel,param){
               var v=$('#'+idSel).attr('value');
            
               jQ_childSel=$('#'+idChildSel);                
               remember(idParentSel,idSel);  //remember(idSel,idChildSel);        
               oldChildVal=jQ_childSel.attr('value');
                           
               jQ_childSel.empty();
               if(""==v)v=const_int;
           
               jQ_childSel.load("AjaxController.jsp", { LOC_LIST: v ,rand:Math.random() }, 
                    function () {
                       ch1(idSel,idChildSel); 
               });      
}
function ch1(idSel,idChildSel){
    jQ_childSel=$('#'+idChildSel);     
    var mm=memoryValue(idSel);
     // (устанавливаем значение атрибута selected равным "selected"):    
     $("#"+idChildSel+" > option[@value='"+mm+"']").attr("selected", "selected");
     //log("selected-------------"+idChildSel+"--"+jQ_childSel.attr("onchange"));
     jQ_childSel.change(); 
}

function memoryValue(idSel){
    var res=0;
    var v=$('#'+idSel).attr('value');    
    res=getRegionHash(idSel+v);
 
    if(""==res) res=0;
    if(0==res){
        setRegionHash(idSel+v,const_int);
        res=const_int;
    }
    return res;
}
function remember(idParentSel,idSel){
    var memX=idParentSel;    
    if(null!=document.getElementById(idParentSel)){
         memX=idParentSel+$('#'+idParentSel).attr('value');
    }    

    setRegionHash(memX,$('#'+idSel).attr('value'));
}

function setRegionHash(name,val){
//    alert(name+":"+val);
    loc_hashtable.put(name, val);
//    alert(loc_hashtable);
    if(name.substring(0,4)!="ZERO")  {
        $.cookie(name,null);
    }else{
     $.cookie(name,val, {expires: 30});
    }
    
    $.cookie("loc_hashtable", loc_hashtable.toString(), {expires: 100});
 
}
function getRegionHash(name){
   
    return loc_hashtable.get(name);
}
function readCookie(name){
 
    return $.cookie(name);
}

function writeCookie(name,val){
 
    $.cookie(name, val, {expires: 367});
}
/*
function writeToCookieOneRegion(ParentNameRegion,valRegion){      
    writeCookie(ParentNameRegion+$('#'+ParentNameRegion).attr('value'),$('#'+valRegion).attr('value'));
}*/
function point(pX,pY){
    this.x=pX;
    this.y=pY;
}
/*
function show_hide_tr (id)
{
	el = document.getElementById(id);
	if ((el) && el.style.display=="none") {
		el.style.display = "block";
		el.height="0px";
		//el.style.display = "table";
	}
	else if (el) { el.style.display = "none"; el.height="20px"}
}
function show_block(id)
{
	el = document.getElementById(id);
	if ((el) && el.style.visibility=="hidden")
	{
            el.style.visibility = "visible";
	}
}
function hidde_block(id)
{
	el = document.getElementById(id);
	if ((el) && el.style.visibility=="visible")
	{
		el.style.visibility = "hidden";
	}
}*/
/**11.07.2008*/
function getSelectElText(pName){
            var pId=$("#"+pName).attr("value");                
            var rTxt=$("#"+pName+" >option[@value='"+pId+"']").text();
   return rTxt;
}

/**11.07.2008**/
function changeId(fromId,toId){
       // alert("FROM: "+fromId+" TO:"+toId);
        var fromTag=$("#"+fromId);
        fromTag.attr("id",toId);
        fromTag.attr("name",toId);
}
/**05.08.2008*/
function trim(pString)
{
return pString.replace(/(^\s+)|(\s+$)/g, "");
}
function urlSpace(val)
{
    var newVal="";
        while(1==1){
            newVal=val.replace(" ","%20");
            if(newVal==val) break;
            val=newVal;
        }
        return val;
}
////////////////////////////   check data /////
function checkField(fName,fRule){
      if(trim($("#"+fName).attr("value")).search(fRule) != -1){
            return true;
      } else {
            return false;
      }
    }

////////////IMG
function OpenWin(url)
{
  www = window.open(url,'img','resizable=1,scrollbar=yes,'+
                              'toolbar=0,status=0,menubar=0,'+
                              'width=700,height=500');
  www.focus();
}
