// AJAX TECHNOLOGY 2.0
var xmlhttp;
var loadstatus = [];
var justload = [];
var just_image=[];
var att='';
var imp=0;
var AjaxCore = {
	method: "GET",
	DisplayContent: function(XModel,XBack,VCall){
		if (xmlhttp.readyState==4)
  			{// 4 = "loaded"
  				if (xmlhttp.status==200)
    				{// 200 = "OK"	
    	              						  		  		  											    
		    			for (i=0;i<XModel.length;i++){
						   XMLTagObj = xmlhttp.responseXML.getElementsByTagName(XModel[i])[0]; 	
						   
						   //if (!XMLTagObj) alert('You are not logined or this feature is disabled');
						   //else {
						    if (!document.getElementById(XBack[i]+'_'+VCall[i])){
						   		parentDiv = document.getElementById(XBack[i]);						   
						   		newdiv = document.createElement('div');
						   		newdiv.setAttribute('id',XBack[i]+'_'+VCall[i]);
						   		parentDiv.appendChild(newdiv);
							}
						    Core2.action.load(XBack[i]+'_'+VCall[i],Core2.action.eval(XMLTagObj.childNodes.item(0).data,XBack[i]+'_'+VCall[i]));											
						    Core2.action.display(XBack[i]);
						    area=XBack[i]+'_'+VCall[i]; // 
						    loadstatus[area]=1; // ghi chu the da duoc load
							
						   //}
					    }			
 
             		}
      		    else
            		{ // load fail
            			alert("Error 101:" + xmlhttp.statusText);
    		        }
  		}	
	},
	Request: function(){
		this.XML = true;
		this.action = {
			display: function(id){
				if (document.getElementById(id))
				document.getElementById(id).style.display = '';
			},
			hide: function(id){
			    if (document.getElementById(id))
				document.getElementById(id).style.display = 'none';
			},						
			change: function(id){
				if (document.getElementById(id)){
					if (document.getElementById(id).style.display == 'none') this.display(id);
					else this.hide(id);
				}
			},
			check_email: function(email){
				Core2.loadXMLDoc(root+'register/checkemail/'+email+'/none/xml',['load_check'],['check'],['available'],"reLoad","hide_all");
			},
			check_user: function(user){
				Core2.loadXMLDoc(root+'register/checkuser/'+user+'/none/xml',['load_check'],['checkuser'],['available'],"reLoad","hide_all");
			},
			load_crop: function(id,x1,y1,width,height){
				Core2.loadXMLDoc(root+'photo/cropimg/img_'+id+'/x1_'+x1+'/y1_'+y1+'/w_'+width+'/h_'+height+'/none/xml/'+Math.random(),['cropped_image'],['current_image'],['content'],'reLoad','hide_all');
			},
			save_crop: function(id,x1,y1,width,height){
				Core2.loadXMLDoc(root+'photo/savecropimg/img_'+id+'/none/xml/'+Math.random(),['cropped_image'],['current_image'],['content'],'reLoad','hide_all');
			},
			load_blur: function(id,x1,y1,x2,y2){
				Core2.loadXMLDoc(root+'photo/blurimg/img_'+id+'/x1_'+x1+'/y1_'+y1+'/x2_'+x2+'/y2_'+y2+'/none/xml/'+Math.random(),['blur_image'],['current_image'],['content'],'reLoad','hide_all');
			},
			save_blur: function(id,x1,y1,width,height){
				Core2.loadXMLDoc(root+'photo/saveblurimg/img_'+id+'/none/xml/'+Math.random(),['blur_image'],['current_image'],['content'],'reLoad','hide_all');
			},
			change_tabs: function(root,activetab){
				list_tab=document.getElementById(root).getAttribute('tab');
				exit=0;
				active_tab=document.getElementById(root).getAttribute('activetab');
				if (activetab!=active_tab) { 
					background=document.getElementById(activetab).getAttribute('background');				
					document.getElementById(root).setAttribute('activetab',activetab);
					if (list_tab){					
						fulllist=list_tab.split(';');
						for (j=0;j<fulllist.length;j++){
							if (fulllist[j]==activetab) exit=1;
							if (fulllist[j]!=activetab) {
								document.getElementById(fulllist[j]).className='main_tab_inactive';
								inactive=document.getElementById(fulllist[j]).getAttribute('background');
								document.getElementById(fulllist[j]).setAttribute('background',bg_inactive);
							} else {
								document.getElementById(fulllist[j]).className='main_tab_active';
								document.getElementById(fulllist[j]).setAttribute('background',bg_active);
							}
						}
						if (exit==0) document.getElementById(root).setAttribute('tab',list_tab+';'+activetab);
					}
				} 
				document.getElementById(activetab).className='main_tab_active';
				document.getElementById(activetab).setAttribute('background',bg_active);
				Core2.action.display(activetab);
			},
			new_images: function(){
				Core2.action.change_tabs('tab_image_01','tab_image_01');
				Core2.loadXMLDoc('ajax.php?HAT=xml&ajax=new_pictures&page=1',['load_images'],['latest_images'],['new_images_page_1'],"reLoad","hide_all");
			},			
			eval: function(respone,parentNode){				  
				var content = new Array();	var xml = respone; var tmp = respone; var src=-1;
				while(respone.indexOf("<script") > -1 || respone.indexOf("</script") > -1) {
					var start = respone.indexOf("<script");					
					var start_end = respone.indexOf(">", start);
					var end = respone.indexOf("</script", start);
					var end_end = respone.indexOf(">", end);						
					content.push(respone.substring(start_end+1, end));
					respone = respone.substring(0, start) + respone.substring(end_end+1);										
				}		
				
				for(var i=0; i<content.length; i++) {
					content[i] = Core2.action.replace(content[i],['<!--','//','-->','--'],['','','','']);									
					try {
						eval(content[i])
					} catch (e){ }
				}	
				return xml;				
			},
			require: function(JS,parentNode){
				var e = document.createElement("script");
   							e.src = JS;
   							e.type="text/javascript";
   							document.getElementById(parentNode).appendChild(e);
			},
			replace: function(content,resource,pattern){
				if (resource.length > 1){
					for (i=0;i<resource.length;i++) {						
						content = content.replace(resource[i],pattern[i]);
					}
				}
			  return content;
			},
			load: function(parentNode,Context){
			document.getElementById(parentNode).innerHTML = Context;
			},
			parseString: function(){
				var ob = document.getElementsByTagName("script");						
							for(var i=0; i<ob.length; i++){
								if (ob.item(i).text!=null) eval(ob.item(i).text);
							}							
			},			
			loadprofile: function(id){						
				if (id==0) Core2.loadXMLDoc(root+'profile/load_female/none/xml',['load_form'],['profile_ajax'],['content'],"reLoad","hide_all");	
				else Core2.loadXMLDoc(root+'profile/load_male/none/xml',['load_form'],['profile_ajax'],['content'],"reLoad","hide_all");	
			},
			logout: function(){
				document.getElementById('password').value='';
				Core2.action.display('not_login');
				Core2.action.hide('logined');
			}			
		}
	}
}
AjaxCore.Request.prototype.loadXMLDoc = function(XAdd,XModel,XBack,VCall,reLoad,Xmethod){
xmlhttp=null;
forcereload=1;
for (i=0;i<XModel.length;i++){ //kiem tra load chua
  area=XBack[i]+'_'+VCall[i];
// disalble all element which is child of this parent 
  att=document.getElementById(XBack[i]).getAttribute('loaded');
	tmp=0;						   
	if (att){						   
		list=att.split(';');
		for (j=0;j<list.length;j++){
			loadstatus[XBack[i]+'_'+list[j]]=1;
			if (list[j]==VCall[i]) {
				Core2.action.display(XBack[i]+'_'+list[j]);
				tmp=1;	
			}
			else {
				if (Xmethod=='hide_all') Core2.action.hide(XBack[i]+'_'+list[j]);					
			}
		}
	}
	if (loadstatus[area]!=1) 	forcereload=0;  
    if (tmp==0) { 
	   		if (att) att=att+';'+VCall[i];
			else att=VCall[i];
			document.getElementById(XBack[i]).setAttribute('loaded',att);
	} 
// end disable
 } 
 if (reLoad=="reLoad"||forcereload==0){ // bat buoc load
 	justload['XAdd']=XAdd; justload['XModel']=XModel; justload['XBack']=XBack; justload['VCall']=VCall; justload['reLoad']=reLoad; justload['Xmethod']=Xmethod; 
	if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
 		}
	else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}		
	xmlhttp.onreadystatechange=function(){ AjaxCore.DisplayContent(XModel,XBack,VCall); }
	xmlhttp.open(AjaxCore.method,XAdd,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xmlhttp.setRequestHeader("Accept-Charset","UTF-8");
	xmlhttp.send(null);
 } else { // hien thi nhung cai da load
 	for (i=0;i<XModel.length;i++){ 		
		Core2.action.display(area);
	}	
 }
}
Core2 = new AjaxCore.Request();
Core3 = Core2;

