// JavaScript Document
var mouse = {

	init: function(ev) {
		if(document.all){
			this.ev = ev;
			this.x = ev.clientX;
			this.y = document.body.scrollHeight;
			this.target = ev.target;
			
			
		} else {
			this.ev = window.event;
			this.x = ev.pageX;
			this.y = ev.pageY;
			this.target = ev.srcElement;
		}
	
		/*
		this.e = (e) ? e : window.event;
		this.x = (e.pageX) ? e.pageX : e.clientX;
		this.y = (e.pageY) ? e.pageY : e.clientY;	
		this.target = (e.target) ? e.target : e.srcElement;
		*/
	},

	getX: function(e){
		
		var e = new mouse.init(e);
		return e.x;
		
	},
	
	getY: function(e){
		
		var e = new mouse.init(e);
		return e.y;
		
	}
	
	// Use:
	/*
	var offX = mouse.getX(event);
	*/
};


var vAJAX = {
	
	init: function(){
		var XHRO = false;
		
		if(window.XMLHttpRequest) XHRO = new XMLHttpRequest();
		else if(window.ActiveXObject) XHRO = new ActiveXObject("Microsoft.XMLHTTP");
	
		return XHRO;
	},
	
	process: function(str){
		var obj = new Object();
		var temp = new Array();
		var arr = str.split("&");
		for(var i =0; i<arr.length; i++){
			temp = arr[i].split("=");
			//alert(temp[0]);
			//obj.temp[0] = temp[1];
		}
		return obj;
	}
	
};

var admin = {
	
	validateLogin: function(frm){
		
		var myResult = $('result');
		
		if(frm.admun.value != "" && frm.admps.value != ""){
			
			//alert("Logging In");
			var XHRO = vAJAX.init();
			var fields = frm.elements;
			var url = "login_check.php";
			var qs = "";
			
			for(var i = 0; i < fields.length; i ++){
				qs+=fields[i].name+"="+fields[i].value+"&";
			}	
			qs = qs.substring(0,qs.length-1);
			//alert(qs);

			if(XHRO){
				//alert("Here");
				XHRO.open("POST", url);
				XHRO.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				
				XHRO.onreadystatechange = function(){
					
					if(XHRO.readyState == 4 && XHRO.status == 200){
						var htmlText = XHRO.responseText;
						//alert(htmlText);
						
						if(parseInt(htmlText)) {
							myResult.className = "";
							//myResult.innerHTML ='<strong>Login Successful. Transfering to Admin Index Page.</strong>';
							myResult.innerHTML ='<strong>Loading....</strong>';
							var t = setInterval('window.location="page/content.php"',2000);
							//window.location.href="index.php";
						} else {
							myResult.className = "error";
							myResult.innerHTML ='<strong>'+htmlText+'</strong>';
							frm.admun.value = ""; frm.admps.value = "";
							frm.admun.focus();
						}
					}
					
				}
			
				XHRO.send(qs);		
			}
		} else {
			myResult.className = "error";
			myResult.innerHTML = "<strong>Username and password required</strong>";

		}
	}
	
};

var page = {
	
	pagename  : ['page_property','page_meta','page_type','page_content'],
	pagetitle : 'page_title',

	View: function(name){	
		
		$(this.pagetitle).innerHTML = this.pagename[name].replace(/\_/," ",this.pagename[name]).toUpperCase();
		
		for(var i=0; i<this.pagename.length; i++){
			if(i == parseInt(name)) {
				$(this.pagename[i]).style.display='block';	
			} else {
				$(this.pagename[i]).style.display='none';
			}
		}
		
		$(this.pagename[name]).style.display='';
	},
	
	Check: function(){
		
		
		
	},
	
	ShowDetail: function(id){
		
		var XHRO = vAJAX.init();
		var url = "editor/editor_ajax.php";
		var qs = "id="+id;
		
		
		if(XHRO){
			//alert("Here");
			XHRO.open("POST", url);
			XHRO.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			XHRO.onreadystatechange = function(){
				
				if(XHRO.readyState == 4 && XHRO.status == 200){
					var htmlText = XHRO.responseText;
					if(htmlText != "error") 
						page.Display(htmlText);
				}
				
			}
		
			XHRO.send(qs);		
		} 
		
		
	},
	
	ShowContent: function(id){
		
		var XHRO = vAJAX.init();
		var url = "editor/editor_ajax.php";
		var qs = "id="+id+"&view=content";
		
		
		if(XHRO){
			//alert(qs);
			XHRO.open("POST", url);
			XHRO.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			XHRO.onreadystatechange = function(){
				
				if(XHRO.readyState == 4 && XHRO.status == 200){
					var htmlText = XHRO.responseText;
					if(htmlText != "error") 
						page.DisplayContent(htmlText);
				}
				
			}
		
			XHRO.send(qs);		
		} 
		
		
	},

	
	Display: function(txt){
		
		var f = $('pageform');
		var fieldname = new Array();
		for(var i=0; i<f.elements.length;i++){
			fieldname[i] = f.elements[i].name;
		}
		fieldname.pop();
		//alert(txt);
		var array = txt.split("&");
		for(var i=0;i<array.length;i++){
			var temp = array[i].split("=");
			var index = temp[0];
			var value = temp[1];
			if(typeof(f.elements[index]) != "undefined"){
				var value1 = value.replace(/<amp>/,"&");
				var value2 = value1.replace(/<equal>/,"=");
				if(index == "page_parent"){
					if(value2 == 0) {
						$('addSub').disabled ='';
					} else {
						$('addSub').disabled="disabled";
					}
				} else if(index == "page_type"){
					f.elements[index][value2].checked = true;
				} else if(index == "page_sitemap"){
					f.elements[index].checked = value2 == 0 ? false : true;
				} else if(index == "page_title"){ 
					$(index).innerHTML = value2;
					f.elements[index].value = value2;
				} else
					f.elements[index].value = value2;
			} 
		}
	},
	
	DisplayContent: function(txt){
		
		//alert(txt);
		var temp = txt.split("=");
		var index = temp[0];
		var value = temp[1];
		var value1 = value.replace(/<amp>/,"&");
		var value2 = value1.replace(/<equal>/g,"=");
		
		/*alert(value2);*/
		/*
		$('content_load').innerHTML = 
		'<? '+
			'$sBasePath = "editor/fckeditor/";'+
			'$oFCKeditor = new FCKeditor("page_html") ;'+
			'$oFCKeditor->BasePath	= $sBasePath ;'+
			'$oFCKeditor->Value		= "'+value2+'";'+
			'$oFCKeditor->Height = "500";'+
			'$oFCKeditor->Create() ;'+
		' ?>';
		*/
		//$('content_load').innerHTML = '<textarea name="page_html" id="page_html" style="height:500px; width:555px" >'+value2+'</textarea>';
		//tinyMCE.content = value2;
		document.forms[0].page_html.value = value2;
	}

};


var Icon = {
	
	showText: function(div, text){
		$(div).innerHTML = text;
	}
	
	
	
	
};



function ShowBox(id){

	if($j(id).css("display") == "none"){
		$j(id).fadeIn("normal");
	} else {
		$j(id).fadeOut("fast");
	}

}

function SlideBox(id){

	if($j(id).css("display") == "none"){
		$j(id).slideDown("normal");
	} else {
		$j(id).slideUp("fast");
	}

}


//Show div only
function Show(id){
	if($j(id).css("display") == "none"){
		$j(id).fadeIn("normal");
	}
}

//Hide div only
function Hide(id){
	if($j(id).css("display") == "block"){
		$j(id).fadeOut("fast");

	}
}

var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

function OnEnter(e)
{
	var keycode = '';
	
	//for IE
	if(isInternetExplorer)	{
		keycode = e.keyCode;
	} else {
		if(e.which){
			keycode = e.which;	
		}
	}
	
	//on Enter
	if(keycode == 13){
		return true;		
	} else {
		return false;	
	}
}
