var xmlSch=null;
var ajax = {	
	Server:function(url,target,is_async,is_uncach){		
		if(!isset(is_uncach)){
			is_uncach=true;
		}
		var cUrl=url;
		var return_value=true;
		if(is_uncach){
			cUrl+='&v='+nowadays();
		}
		if(target){
			cUrl+='&target='+target;
		}
		if(is_async==null || is_async==undefined){
			is_async=false;
		}
		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,'GET',is_async,target);		
	},
	//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 (window.ActiveXObject){
			   				elmnt.setAttribute(attr,val);
			   				}else{
			   					this.mozilaSetAttribute(elmnt,attr,val)
			   				}
			   			}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(mytrim(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(mytrim(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){		
		if(isset($(id))){id=$(id)};
		if(!isset(id)){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;
			}
		}
		url+='&'+this.FormToGet(id);
		if(mytarget){
			this.Server(url,mytarget);
		}else{
			this.Server(url);
		}
		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'){		         
		         getstr += encodeURIComponent(sel.name) + "=" + encodeURIComponent(sel.options[sel.selectedIndex].value) + "&";
	      	/*}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;
	}
}

function dbg(obj) {
	ret = '[';
	for(var i in obj) {
		ret = ret +"\n["+i+"]"+"=>"+"'"+ obj[i]+"'";
	}
	ret = ret + ']';
	vr = window.open();
	vr.document.write(ret);
}

function nowadays() {
	dt = new Date();
	return dt.getDate()+"_"+dt.getMinutes()+"_"+dt.getSeconds()+"_"+dt.getMilliseconds();
}

function is_int(str) {
    rgxp=/^(0|([1-9][0-9]*))$/;
	if (str.match(rgxp))
		return true;
	return false;
}

/*function is_phone(str) {
    rgxp=/^(0|([1-9][0-9]*))(\-0|([1-9][0-9]*))?$/;
	if (str.match(rgxp))
		return true;
	return false;
}*/

function validate_float(float_num) {
	var rgxp=/^(0|([1-9][0-9]*))(\.([0-9]{1,2}))?$/;
	if(float_num.match(rgxp)==null)
		return false;
	return true;
}

var search_history = new Array();
var search_arr = new Array();
function combo_jump_on_search(obj) {
	if(obj.id==undefined) {
		alert("can't use the function without object id");
		return;
	}
	if(event.keyCode==13)
		return;
	var len = obj.options.length;
	if(search_arr[obj.id]==undefined) {
		search_arr[obj.id] = new Array();
		for(a=1;a<len;a++) {
			search_arr[obj.id][a-1] = obj.options[a];
		}
	}
	if(search_history[obj.id]==undefined) {
		search_history[obj.id]='';
	}
	var ch = String.fromCharCode(event.keyCode);
	if(event.keyCode==47) {
		// use '/' for prev
		fill_prev_options_search(obj);
		return;
	}
	if(event.keyCode==45) {
		// use '-' for reset
		fill_all_options_search(obj);
		return;
	}
	search_history[obj.id] += ch;
	window.status = search_history[obj.id];
	var text = "";
	var i;
	regex = eval('/'+search_history[obj.id]+'/');
	for(i=0;i<len;i++) {
		if(obj.options[i] && obj.options[i].value!=-1) {
			text = obj.options[i].text;
			if(!text.match(regex)) {
				obj.options[i].removeNode(true);
				i--;
			}
		}				
	}
	return false;	
}

function combo_clear_search(obj) {
	if(obj.id==undefined) {
		alert("can't use the function without object id");
		return;
	}
	window.status = search_history[obj.id] = '';
	search_arr[obj.id] = new Array();
}

function fill_prev_options_search(obj) {
	var last_srch = eval('/'+search_history[obj.id]+'/');
	search_history[obj.id] = search_history[obj.id].substring(0,search_history[obj.id].length-1);
	if(!search_arr[obj.id])
		return;
	var len = search_arr[obj.id].length;
	regex = eval('/'+search_history[obj.id]+'/');
	var txt = '';
	for(i=0;i<len;i++) {
		txt = search_arr[obj.id][i].text;
		if(txt.match(last_srch)) {
			continue;
		}
		if(txt.match(regex)) {
			var opt = new Option();
			obj.options[obj.options.length] = opt;
			obj.selectedIndex = obj.options.length-1;
			opt.innerText = txt;
			opt.value = search_arr[obj.id][i].value;
		}
	}
}

function fill_all_options_search(obj) {
	var regex = eval('/'+search_history[obj.id]+'/');
	var txt = '';
	var len = search_arr[obj.id].length;
	for(i=0;i<len;i++) {
		txt = search_arr[obj.id][i].text;
		if(txt.match(regex)) {
			continue;
		}
		var opt = new Option();
		obj.options[obj.options.length] = opt;
		obj.selectedIndex = obj.options.length-1;
		opt.innerText = txt;
		opt.value = search_arr[obj.id][i].value;
	}
	search_history[obj.id] = '';
}

function isset(elmnt) {
	if(document.getElementById(elmnt)!=null && document.getElementById(elmnt)!=undefined){
		return true;
	}
	if(String(elmnt)=='[object]'){
		return true;
	}
	return false;
}

function $(id) {
	return document.getElementById(id)
}

function serverLoad() {
    this.browser = (window.XMLHttpRequest) ? 'mozilla' : 'ie';
    this.conname = (this.browser == 'mozilla') ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
}

function connectionResult(connection,target, s_time){
    switch(connection.readyState){
            case 0://
            //uninitialized
            //window.status = "serverLoad Request Status- Unknown";
            break;
            case 1:
            if(target && isset(target)){
            	var str=$(target).innerHTML;
					if(str.indexOf("onload.gif")<0){
            		$(target).innerHTML='<div class="onload"><img src="onload.gif" /></div>'+str;
					}
            }
            //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(isset('ajax_debug_div')) {
            			$('ajax_debug_div').innerHTML += ' - ' + (new Date().getTime()- s_time)+ ' ms';
            		}
                 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 ...';
        s_time = new Date().getTime();
        if(async){
	        this.conname.onreadystatechange = function(){
	        		connectionResult(connection,target, s_time);
	        }
        }
        this.conname.open(method,url,async);
        if(tosend == undefined || tosend == null){
                var vars = null;        
        }else{
                var vars = tosend;
        }
        this.conname.send(vars);
        if(!async){
        		return connectionResult(connection, target, s_time);
        }

};
//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 do_next() {
	sel=document.getElementById('sel_results');
	var i=sel.selectedIndex;
	if (i<sel.options.length-1)
	{
		document.getElementById('btn_prev').disabled=false;
		i++;
		sel.selectedIndex=i;
		sel.onchange();
	}
	if (i==sel.options.length-1)
		document.getElementById('btn_next').disabled=true;
	else
		document.getElementById('btn_next').disabled=false;
}

function do_prev() {
	sel=document.getElementById('sel_results');
	var i=sel.selectedIndex;
	if (i>0)
	{
		document.getElementById('btn_next').disabled=false;
		i--;
		sel.selectedIndex=i;
		sel.onchange();
	}
	if (i==0)
		document.getElementById('btn_prev').disabled=true;
	else
		document.getElementById('btn_prev').disabled=false;
}

function results_change(obj) {
	var index=obj.selectedIndex;
	document.getElementById('btn_prev').disabled=false;
	document.getElementById('btn_next').disabled=false;
	if (index==0)
		document.getElementById('btn_prev').disabled=true;
	if (index==obj.options.length-1)
		document.getElementById('btn_next').disabled=true;
	do_load(obj.value);
}

function retype_password(obj) {
	$('PASSWORD_2').value='';
	$('PASSWORD_2').focus();
}

function compare_password(pwd2) {
	var pwd1 = $('PASSWORD');
	if(pwd2.value!=pwd1.value) {
	    alert('Password is not same');
	    pwd2.value='';
	    pwd2.focus();
	}
}

function validate_email_address(elmnt) {
	return true;
	var mail_val=$(elmnt).value;
	var mail_regexp=/[\w]?[\w\._\-]+\.[\w]+/;
	if(mail_val!="" && mail_val.search(mail_regexp)==-1) {
	    alert('Illegal email address!');
	    $(elmnt).focus();
		$(elmnt).value="";
	    return false;
	}
	return true;
}
