var rb_message_arr=new Array();
rb_message_arr['PROCESSING']='Proccessing....';
var last_location='';
var isProcessingURL=false;
var loadedobjects='';
var rootdomain="http://"+window.location.hostname+location.pathname;
var SITE_TITLE="~:: L2N SOFTWARE LIMITED ::~";

function $i(id){return document.getElementById(id);}
function $n(n){return document.getElementsByName(n);}
function $t(tn){return document.getElementsByTagName(tn);}
function $ptr(ref){return (ref.nodeName.toLowerCase()=='tr')?ref:(ref.parentNode)?$ptr(ref.parentNode):null;}
function $pT(ref,t,c){if(typeof t=='undefined'){return t.parentNode;}if(!ref.parentNode){return null;}if(typeof c=='undefined'){c=0;}if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase() && c==0){return ref.parentNode;}else if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase()){c--;return $pT(ref.parentNode,t.toLowerCase(),c);}else{return $pT(ref.parentNode,t.toLowerCase(),c);}}

function loadobjs(){
	if (!document.getElementById)
		return;
	for (i=0; i<arguments.length-1; i++){
		var file=arguments[i];
		var fileref="";
		if(loadedobjects.indexOf(file) !=-1){
//			alert("already loaded");
			if(arguments[arguments.length-1]!='')
				eval(arguments[arguments.length-1])();
		}
		else if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
			
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref);
			loadedobjects+=file+" "; //Remember this object as being already added to page
		}
	}
}


function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function show_status(msg){
	if(typeof msg=='undefined'){
		$('#status_msg_div').hide();
		return;
	}
	$('#status_msg_div').html(msg);
	$('#status_msg_div').show();	
	var status_div=document.getElementById('status_msg_div');
	//status_div.style.top=iecompattest().scrollTop+'px';	
	//status_div.style.left = '0px';
}

$.ajaxSetup({
  cache	: false
});

function GetUP(name,url)		//Ger Url Parameter
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var u=url||window.location.href;
  var results = regex.exec(u);
  if( results == null )
    return "";
  else
    return results[1];
}

function convertUrl(ur){
	if(typeof ur !='undefined'){
		var h=ur;
		
		h=h.replace(rootdomain,"");
		
		var file_name=GetUP('r',h);
		var function_name=GetUP('f',h);
		var left_url="";
		
		var u="";
		if(file_name!=""){
			u=rootdomain+"#"+file_name;
			h=h.replace("r="+file_name,"")
		}
		if(function_name!=""){
			u=u+"#"+function_name;
			h=h.replace("f="+function_name,"")
		}
			h=h.replace("?","");
			h=h.replace("&&","&");
			h=h.replace("&","/");
			h=h.replace(new RegExp("[/]+$", "g"), "");
		return (u+h);	
	}
	else{
		return "";
	}
							  
}

function initAjaxLink(){
	$(".ajaxlink").unbind('click', parseUrl);
	$('.ajaxlink').click(parseUrl);	
	initInpageLink();
}

function initInpageLink(){
$('.inpageLink').click(function() {
          if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&& location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target|| $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
              var targetOffset = $target.offset().top-15;
              $('html,body').animate({scrollTop: targetOffset}, 3000,'easeOutBounce');
             return false;
         	}
  		} 
	});  	
}

function parseUrl(){
	if(isProcessingURL){		//If previously processing any url
		return false;
	}
	
	var el=$(this);
	var link_url=el.attr('href');	
	
	document.title=SITE_TITLE;		

	if(location.hash=='' && link_url==null){
		return false;
	}
	
	var arr=location.hash.split('#');
	
	if(arr.length<1){
		return false;
	}
	var hashurl=arr[1];
	if(hashurl==''){
		return false;	
	}
	//alert(hashurl)	;
	
	if(link_url==null){
		//return false;
	}else
	{
		var h=convertUrl(link_url);
		window.location=h;
		if(location.hash==''){
			return false;
		}
		
		var arr=location.hash.split('#');
		
		if(arr.length<1){
			return false;
		}
		var hashurl=arr[1];
		if(hashurl==''){
			return false;	
		}
	}
	
	
	isProcessingURL=true;
	var par_arr=hashurl.split('/');
	
	window.location=location.hash.replace('.php','');
	var ex_p=(par_arr.length>1)?("&"+par_arr[1]):'';
	var requesturl='parseurl.php?p='+par_arr[0]+ex_p;
	show_status(rb_message_arr['PROCESSING']);
	$.ajax({
		   type		: "GET",
		   url		: requesturl,
		   success	: function(x){
			  document.getElementById('main_body_content').innerHTML=x;
			  if(arr.length>2)
			  	if(typeof arr[2]=='function')
			  		arr[2]();
				else
					eval(arr[2]+'()');
			
			
			last_location=location;	
			//Re initialize the link for lodaded page
			  initAjaxLink();
			  //Fix Png
			  correctPNG();
			  show_status();
			  
			 //Position the page
			 $target = $('[name=topanchor]');
			 if ($target.length) {
				  var targetOffset = $target.offset().top-10;
				  $('html,body').animate({scrollTop: targetOffset}, 1000);
			 }
			 //position the add
			 isProcessingURL=false;
		   },
		   	error:function(xmlHttp, textStatus, errorThrown){
			   show_status();
			   alert('error in communication, error no = '+ xmlHttp.status);
			   isProcessingURL=false;
		   }
		 });		
	return false;
}

function Round(num,n){	
	if(isNaN(num))
		return '0.00';
	return Math.round(num * Math.pow(10,n))/Math.pow(10,n);
}

function watermark(ref,img){
	 if (ref.value == '') { ref.style.background = '#FFFFFF url('+img+') left no-repeat'; }
}