function RegisterNamespace(ns) {
	var nsParts = ns.split(".");
	var root = window;

	for(var i=0; i<nsParts.length; i++) {
		if(typeof root[nsParts[i]] == "undefined") {
		root[nsParts[i]] = new Object();
		root = root[nsParts[i]];
		}
	}
}

RegisterNamespace('Utility');

RegisterNamespace('Browser');


Browser.ClientWidth = function() {
	var w;
	if(document.innerWidth){ 
		w = document.innerWidth;
	} 
	else if(document.documentElement.clientWidth) { 
		w = document.documentElement.clientWidth;
	} else if(document.body) { 
		w = document.body.clientWidth; 
	}
	return w;
}

Browser.ClientHeight = function() {
	var h;
	if(document.innerHeight) { 
		h = document.innerHeight;
	} 
	else if(document.documentElement.clientHeight) { 
		h = document.documentElement.clientHeight;
	}
	else if(document.body) { 
		h = document.body.clientHeight; 
	}
	return h;
}


Utility.AnimateLoading = function(id) {
 	var	loading = $(id);
}

Utility.EmptyFunction = function() { }

Utility.Fade = function (id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        }
    }
}

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 


String.prototype.Empty = "";

Utility.FindPosition = function(elem) {
    var curleft = 0;
    var curtop = 0;

	if (elem.offsetParent) {
		while (elem.offsetParent) {
			curleft += elem.offsetLeft
			curtop += elem.offsetTop
			elem = elem.offsetParent;
		}
	}
	else if (elem.x) {
		curleft += elem.x;
		curtop += elem.x;
	}
    
    return { x : curleft , y : curtop }
}

Utility.IsFireFox = function() {    
	return window.navigator.userAgent.toLowerCase().indexOf("gecko") >= 0;
}

Utility.GetFullQueryString = function() {
	var url = window.location.href;
    var qsStart = url.indexOf('?')
	
    return qsStart >=0 ? url.substring(qsStart + 1) : "";
}

Utility.QueryString = new Object();


RegisterNamespace('Ajax');

Ajax.CreateRequestObject = function() {
   var req;
   if(window.XMLHttpRequest) {      
      req = new XMLHttpRequest();
   } 
   else if(window.ActiveXObject) {      
      req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else  {      
      alert('Problem creating the XMLHttpRequest object');
   }
   return req;
}


//Will replace The current Ajax Handler
Ajax.AjaxRequest = function() {
	var _xmlHttp = null;
	var _completeCallback = null;
	var _errorCallback = null;		
	var _ajaxRequestQueue = [];
	
	this.IsComplete = true;
	var _this = this;		
	
	this.CreateGet = function(url , data , onComplete, onError) {
		Create(url , 'GET' , data, onComplete , onError);
	}
	
	this.CreatePost = function(url , data , onComplete, onError) {
		Create(url , 'POST' , data, onComplete , onError);
	}		
		
	function Create(url , method , data , onComplete , onError) {        	    
        _completeCallback = onComplete;
                
        _errorCallback = onError;
        
        var dateTime = new Date();                                
        
        _xmlHttp = CreateRequestObject();
	    _xmlHttp.onreadystatechange = onReadyStateChange;
	    
	    if(method.toUpperCase() == 'GET') {	        
	        _xmlHttp.open(method , url +"?"+ data ,true);
	        _xmlHttp.send(null);
	        
	    }
	    else if(method.toUpperCase() == 'POST') {	        			
	        _xmlHttp.open(method , url , true);        
	        _xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	        _xmlHttp.setRequestHeader("Content-Length",data.length);
	        _xmlHttp.send(encodeURI(data));
	        
	    }
	    	    	
		this.IsComplete = false;
	}
	
	this.UploadFile = function(url , data , onComplete) {
	    _completeCallback = onComplete;
	    	    
	    _xmlHttp = CreateRequestObject();
	    _xmlHttp.onreadystatechange = OnReadyStateChange;
	    
	    _xmlHttp.open('POST' , url , true);        
        _xmlHttp.setRequestHeader("Content-Type","multipart/form-data");        
        _xmlHttp.send(encodeURI(data));
        
        this.IsComplete = false;
	}
	
	function CreateRequestObject() {
       var req;
       if(window.XMLHttpRequest) {      
          req = new XMLHttpRequest();
       } 
       else if(window.ActiveXObject) {      
          req = new ActiveXObject("Microsoft.XMLHTTP");
       }
       else {      
          alert('Problem creating the XMLHttpRequest object');
       }
       return req;
    }
	
	function onReadyStateChange() {
	    if(_xmlHttp.readyState == 4) {
	        _this.IsComplete = true;
	        if(_xmlHttp.status == 200) {

	            if(_completeCallback != null) {
	                _completeCallback({text : _xmlHttp.responseText});
	            }
	        }
	        else {
	            if(_errorCallback != null) {
	                _errorCallback({status : _xmlHttp.status , text : _xmlHttp.statusText });
	            }
	        }	        
	        
	        Cleanup();						
	    }
	}
	
	function Cleanup() {
	    _xmlHttp.onreadystatechange = {};
	    _xmlHttp = null;
	    _completeCallback = null;
	    _errorCallback = null;
		
	}	
}


Ajax.ScriptProxy = function() {
    var _url = "";
    
    this.SetUrl = function(url) {
        _url = url;               
    }                     
    
    
    this.RegisterAjaxMethod = function(phpFunctionName) {      
		if(typeof eval("AjaxMethod." +  phpFunctionName) != 'undefined') {
			alert('Method already defined');
			return;
		}
		
        var jsfunction = "AjaxMethod." +  phpFunctionName  + " = function()" +
        "{" +
			"Request = new Ajax.AjaxRequest();" +
            "var params = '';" +
	        "var onComplete = null; var onError = null;" +
	        "if(arguments.length > 1)" +
    	    "{" +
				"if(typeof(arguments[arguments.length - 1]) == 'function' && typeof(arguments[arguments.length - 2]) == 'function') { onError = arguments[arguments.length-1]; onComplete = arguments[arguments.length-2]; } else {" +
        	    "onComplete = arguments[arguments.length-1]; }" +
				
				"for(var i = 0; i < arguments.length - 1; ++i)" +
		        "{" +
		            "params += ',' + arguments[i] ;" + 
		        "}" +						
		        "params = params.substring(1);" +		
			"}else { onComplete = arguments[0]; }" +
            "/*ajaxReq*/Request.CreatePost('" + _url + "', '&functionName=" + phpFunctionName + "&params=' + params, onComplete, onError);" +
        "/*delete Request;*/}";
        
		eval(jsfunction);       
    }
	
	this.RegisterAjaxMethods = function(arrayOfMethods) {
		for(var i =0; i < arrayOfMethods.length; ++i) {
			this.RegisterAjaxMethod(arrayOfMethods[i]);
		}
	}
}

RegisterNamespace('AjaxMethod');

