function trim(str) {
     s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
     return s;
}

function nowadays() {
	dt = new Date();
	return dt.getDate()+"_"+dt.getMinutes()+"_"+dt.getSeconds()+"_"+dt.getMilliseconds();
}

function is_int(str) {
	var tmp=parseInt(str);
	if (tmp.toString()!=str)
	    return false;
	return true;
}

function child_el(obj,idx) {
	var i;
	var tmp=obj.childNodes;
	var cnt=0;
	for (i=0;i<tmp.length,cnt<=idx;i++)
	{
	    if (tmp[i].nodeType!=1)
	        continue;
	    if (cnt==idx)
	        return tmp[cnt];
	    cnt++;
	}
	return null;
}

function load_iframe(cUrl,targetId) {
	if(targetId!=1){
		ajax.Server(cUrl+'&embed='+targetId);
	}
	else{
		ajax.Server(cUrl);
	}
}

function open_div(name){
	if(document.getElementById("second_"+name).style.display=="none"){
		document.getElementById("second_"+name).style.display='';
	}
	else{
		document.getElementById("second_"+name).style.display="none";	
	}
}

function isset(elmnt){
	if(document.getElementById(elmnt)!=null && document.getElementById(elmnt)!=undefined){
		return true;
	}
	if(String(elmnt).indexOf('object') >= 0){
		return true;
	}
	return false;
}
// Returns a reference to the element specified
function $(id)
{
	return document.getElementById(id)
}
var styles = {
	Set: function(id, method, value) {
		return id.style[method] = value;
	},
	Get: function(id, method) {
		return id.style[method]
	}
}
document.getElementsByClassName = function(node, classname) {
    var elements = [];
    var re = new RegExp('(^| )' + classname + '( |$)');
    var children = node.getElementsByTagName("*");
    for (var i = 0, j = children.length; i < j; i++)
        if (re.test(children[i].className)) elements.push(children[i]);
    return elements;
}
// Adds an event handler to an element
function addEvent(element, type, handler)
{
	if (element.addEventListener) {
		element.addEventListener( type, handler, false );
	}
	else if (element.attachEvent) {
		element["e" + type + handler] = handler;
		element[type + handler] = function() { element["e" + type + handler](window.event); }
		element.attachEvent("on" + type, element[type + handler]);
	}
}
function nowadays(){
	dt = new Date();
	return dt.getDate()+"_"+dt.getMinutes()+"_"+dt.getSeconds()+"_"+dt.getMilliseconds();
}
var xmlSch=null;
var ajax = {
	Server:function(url,target,is_async,is_uncach,is_post,relocation){				
		if(!isset(is_uncach)){
			is_uncach=true;
		}
		var cUrl=url;//encodeURI
		var return_value=true;		
		if(target){
			cUrl+='&target='+target;
		}
		if(is_async==null || is_async==undefined){
			is_async=false;
		}
		if(!relocation){
			cUrl+='&at_ajax=1';		
			if(isset('ajax_debug_div')){
				if(!ajax.numLoading || ajax.numLoading<2){
					$('ajax_debug_div').innerHTML='';
				}
		   	$('ajax_debug_div').innerHTML+='<br/>'+cUrl+'&debug_mode=1';
		   }
		}
	   var loaded=new serverLoad();
	   var tosend;
	   var method="GET";
	   if(is_post){
	   	method="POST";
	   	var url_part=cUrl.split("?");
			var i;
			if(url_part.length>1){
				tosend=url_part[1];
			}
			for(i=2;i<url_part.length;i++){
				tosend+='?'+url_part[i];
			}
			cUrl=url_part[0];
			if(is_uncach){
				cUrl+='?v='+nowadays();
			}
		}else{
		   if(is_uncach){
				cUrl+='&v='+nowadays();
			}
		}
		if(relocation){
			document.location=cUrl;
			return false;
		}
		return loaded.sendAndLoad(cUrl,method,is_async,target,tosend);		
	},
	ServerPost:function(url,post_data,is_async,target){		
		is_uncach=true;
		var cUrl=url;
		var return_value=true;
		cUrl+='&v='+nowadays();		
		if(is_async==null || is_async==undefined){
			is_async=false;
		}
		if(target){
			cUrl+='&target='+target;
		}
		cUrl+='&at_ajax=1';	
		if(isset('ajax_debug_div')){
			if(!ajax.numLoading || ajax.numLoading<2){
				$('ajax_debug_div').innerHTML='';
			}
	   		$('ajax_debug_div').innerHTML+='<br/>'+cUrl+'&debug_mode=1';
	   	}
		var loaded=new serverLoad();
		return loaded.sendAndLoad(cUrl,'POST',is_async,target,post_data);		
	},
	//expect to receive the xmlhttprequest object
	XmlToHtml:function(rslt){
		var return_value=true;	
		if(xmlSch==null){
			if (window.ActiveXObject){				
				this.conname = new ActiveXObject('Microsoft.XMLHTTP');
				xmlSch = new ActiveXObject("Microsoft.XMLDOM");
				xmlSch.async = false;
			}else if(document.implementation && document.implementation.createDocument){				
				this.conname = new XMLHttpRequest();
				xmlSch = document.implementation.createDocument("", "doc", null);
				xmlSch.async = false;
			}
		}
		
		if (window.ActiveXObject){	
			xmlSch.load(rslt.responseXML);		
		}else{
			xmlSch=rslt.responseXML;
		}/*
		xmlSch.load(rslt.responseXML);		
	   var root=rslt.responseXML.documentElement;	  
		xmlSch.load(rslt.responseXML);		*/
	   var root=xmlSch.documentElement;	  
	   if(root==null || root==undefined){
	   	if(isset('ajax_debug_div')){
		   	//$('ajax_debug_div').innerHTML+="<br/>"+rslt.responseText;
		   	var win=window.open();		  
				win.document.writeln(rslt.responseText);
		   	//alert(cUrl + " can't found");
		   }
		   window.status='error at ajax praser';
	   	//document.body.innerHTML=cUrl + " can't found";
	   	return null;
	   }
	   //window.open(cUrl);
	   var current_child=root.firstChild;
	   var i,j,k,id,attr,method_length,elmnt,val,nodev,fcs;
	   while(current_child!=null && current_child!=undefined){	
	   	if(current_child.nodeName=='todo'){
		   	for(i=0;i<current_child.getElementsByTagName('elmnt_id').length;i++){
		   		//document.getElementById('test1').getAttribute('style');
		   		elmnt=document.getElementById(current_child.getElementsByTagName('elmnt_id')[i].firstChild.nodeValue);
		   		val=current_child.getElementsByTagName('elmnt_val')[0].firstChild.nodeValue;
		   		if(current_child.getElementsByTagName('method')!=null && current_child.getElementsByTagName('method')!=undefined){
		   			method_length=current_child.getElementsByTagName('method').length-1;
			   		for(j=0;j<current_child.getElementsByTagName('method').length && elmnt!=null && elmnt!=undefined;j++){
			   			attr=current_child.getElementsByTagName('method')[j].firstChild.nodeValue;
			   			if(j==method_length){
			   				if(attr=='innerHTML'){//in some browsers setAttribute not working well so we do it manually
								elmnt.innerHTML=val;
							}else if (attr=='selected'){
								elmnt.selected=val;
							}else{
			   					try{
			   						elmnt.setAttribute(attr,val);
			   					} catch (e) {alert(e);}
			   				}
			   			}else{
			   				elmnt=elmnt.getAttribute(attr);
			   			}
			   		}
		   		}
		   	}
	   	}else{
	   		if(current_child.nodeName=='js_data'){
	   			//document.location='http://helena4love.com/icms2/htdocs/archive/he/prog_search_search_simple_cmd_main';
	   			if (window.ActiveXObject){ 
				    eval(current_child.firstChild.nodeValue);
			    }else{
					fcs=current_child.firstChild;			    			    
				   while(trim(fcs.nodeValue)=='' && fcs!=null && fcs!=undefined){ 
    					fcs=fcs.nextSibling;
   					}
   					if(fcs!=null && fcs!=undefined){
   						eval(fcs.nodeValue);
   					}
   				}
	   		}else{
	   			if(current_child.nodeName=='return_value'){
	   				if (window.ActiveXObject){ 
				    	return_value =eval(current_child.firstChild.nodeValue);
				    }else{
					    fcs=current_child.firstChild;
					    while(trim(fcs.nodeValue)=='' && fcs!=null && fcs!=undefined){ 
					    	fcs=fcs.nextSibling;
					    }
					    if(fcs!=null && fcs!=undefined){
					    	return_value =eval(fcs.nodeValue);
					    }
				    }
	   			}
	   		}
	   	}
	   	current_child=current_child.nextSibling;
	   }
	   return return_value;
	},
	mozilaSetAttribute:function (elmnt,attr,val){
		if(attr=='innerHTML'){
			elmnt.innerHTML=val;
		}
	},
	Submit:function (id,action,target,relocation){
		if(typeof(id)!='object'){
			if(isset(id)){
				id=$(id);
			}else{
				alert("no such id");
				return false;
			}
		}
			
		var url="";
		var mytarget=false;
		if(action){
			url=action;
		}else{
			if(id.action!=null && id.action!=''){
				url=id.action;
			}else{
				alert('no action');return false;
			}
		}
		if(target!=null && target!=''){
			//url+='&target='+target;
			mytarget=target;
		}else{
			if(id.target!=null && id.target!=''){
				//url+='&target='+id.target;
				mytarget=id.target;
			}
		}
		var post_data=this.FormToGet(id);
		if(relocation){
			document.location=url+'&'+post_data;
			return false;
		}
		if(mytarget){
			this.ServerPost(url,post_data,false,mytarget)
		}else{
			this.ServerPost(url,post_data);
		}
		return false;
	},
	FormToGet:function(obj){
		var getstr = "";
	   var i;
	   for (i=0; i<obj.childNodes.length; i++) {   	
	   	//if(obj.childNodes[i].name=='undefined' || obj.childNodes[i].name=='' || obj.childNodes[i].name==null)continue;   	
	      if (obj.childNodes[i].tagName == "INPUT") {
	         if (obj.childNodes[i].type == "text" || obj.childNodes[i].type == "hidden" || obj.childNodes[i].type == "password") {
	            getstr += encodeURIComponent(obj.childNodes[i].name) + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
	         }
	         if (obj.childNodes[i].type == "checkbox") {
	            if (obj.childNodes[i].checked) {
	               getstr += encodeURIComponent(obj.childNodes[i].name) + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
	            } else {
	               // HTML not sending unchecked element!:
	               //getstr += obj.childNodes[i].name + "=&";
	            }
	         }
	         if (obj.childNodes[i].type == "radio") {
	            if (obj.childNodes[i].checked) {
	               getstr += encodeURIComponent(obj.childNodes[i].name) + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
	            }
	         }
	      }   else if (obj.childNodes[i].tagName == "SELECT") {
	      	var sel = obj.childNodes[i];
	      	//if(sel.type='select-one'){		
	      		if(sel && sel.name && sel.options && sel.selectedIndex>=0 && sel.options[sel.selectedIndex]){
		       getstr += encodeURIComponent(sel.name) + "=" + encodeURIComponent(sel.options[sel.selectedIndex].value) + "&";
	      		}else{
	      			if(sel && sel.name){
	      				getstr += encodeURIComponent(sel.name) + "=&";
	      			}
	      		}
	      	/*}else{
	      		for (var j=0; j<sel.options.length; j++) {
	            if (sel.options[j].selected) {
	            	getstr += encodeURIComponent(sel.name) + "=" + encodeURIComponent(sel.options[j].value) + "&";
	            }
	          }
	      	}*/
	      }  else if (obj.childNodes[i].tagName == "TEXTAREA") {
	         var sel = obj.childNodes[i];
	         getstr += encodeURIComponent(obj.childNodes[i].name) + "=" + encodeURIComponent(obj.childNodes[i].value) + "&";
	      } else {
	      	var elmnt=obj.childNodes[i];
	      	if(elmnt.childNodes.length && elmnt.childNodes.length>0 && elmnt.childNodes[0] && elmnt.childNodes[0].tagName!='undefined'){
	      			getstr +=this.FormToGet(elmnt);
	      	}
	      }
	      
	   }
	   
	   return getstr;
	}
}

//Simplified XMLHttpRequest Connector
//constructor
function serverLoad(){
        this.browser = (window.XMLHttpRequest) ? 'mozilla' : 'ie';
    this.conname = (this.browser == 'mozilla') ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
}
//methods

function connectionResult(connection,target){
    switch(connection.readyState){
            case 0://
            //uninitialized
            //window.status = "serverLoad Request Status- Unknown";
            break;
            case 1:
            if(target && isset(target)){
            	$(target).innerHTML='<div class="onload"><img src="onload.gif" /></div>'+$(target).innerHTML;
            }
            //loading 
            //window.status = "serverLoad Request Status- Loading";
            break;
            case 2:
            //loaded
            //window.status = "serverLoad Request Status- Loaded";
            break;
            case 3:
            //interactive state - whatever that means
            //window.status = "serverLoad Request Status- Interactive";
            break;
            case 4:
            	var ret=false;
            		ajax.numLoading--;
                 if (connection.status==200){ 
                         ret=ajax.XmlToHtml(connection);
                       if(ajax.numLoading==0){
                       	window.status='Done';
                       	         				
                       	
                       }else{
                       		window.status='Loading '+ajax.numLoading+' Object ...';
                       }
                 }else{
                         window.status='error on request';
                       if(ajax.numLoading==0){
                       		window.status='error on request';
                       }else{
                       		window.status='Loading '+ajax.numLoading+' Object, error on request' + (ajax.numLoading+1);
                       }
                 }
                 //clear connection
                 connection.abort();
                 this.conname=null;
                 return ret;
            break;
    }
};

//send and load
//param 1: url 
//param 2: "POST" "GET" 
//param 4: async
//param 5: to send - (xml or url encoded string)
//note: callback function should expect to receive the xmlhttprequest object
serverLoad.prototype.sendAndLoad = function(url, method, async,target,tosend){
        var connection = this.conname;
        if(!ajax.numLoading){
        		ajax.numLoading=0;
        }
        ajax.numLoading++;
        window.status='Loading '+ajax.numLoading+' Object ...';
        if(async){
	        this.conname.onreadystatechange = function(){
	        		connectionResult(connection,target);                
	        }
        }
        this.conname.open(method,url,async);
        if(tosend == undefined || tosend == null){
                var vars = null;        
        }else{
                var vars = tosend;
                this.conname.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        }
        this.conname.send(vars);
        if(!async){
        		return connectionResult(connection,target);
        }

};
//get object reference
serverLoad.prototype.getRef = function(){
   return this.conname;
};
//shortcut to responsetext
serverLoad.prototype.getResponseText = function(){
   return this.conname.responseText;
};
//shortcut to responseXML
serverLoad.prototype.getResponseXML = function(){
   return this.conname.responseXML;
};
//abort request
serverLoad.prototype.abortRequest = function(){
   this.conname.abort();
};

function includeJS(jsPath){
	var js = document.createElement("script");
	/*js.setAttribute("type", "text/javascript");
	js.setAttribute("src", jsPath);*/	
	js.type="text/javascript";
	js.src=jsPath;
	js.defer = true;
	document.getElementsByTagName("head")[0].appendChild(js);
}
function includeCSS(cssPath,media){
	var css = document.createElement("link");
	css.rel="Stylesheet";
	css.href=cssPath;
	css.type="text/css";
	if(!media)media="all";
	css.media=media;
	document.getElementsByTagName("head")[0].appendChild(css);
}


function rcss(){
	var i,a,s;a=document.getElementsByTagName('link');
	for(i=0;i<a.length;i++){s=a[i];
	if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {var h=s.href.replace(/(&|%5C?)forceReload=d+/,'');
	s.href=h+(h.indexOf('?')>=0?'&':'?')+'forceReload='+(new Date().valueOf()
	)
	}
	}
}

function load_new_iframe(cUrl,targetId) {
	if(targetId!=1){
		document.getElementById('newifrm').src =cUrl+'&embed='+targetId+'&v='+nowadays();
	}
	else{
		document.getElementById('newifrm').src =cUrl+'&v='+nowadays();
	}
}