/******************************************************
********* Users JS SDK (Al.Hosn University)****************
********* Created By: Mohamed Farouk (11 June 2007) *
********* Last Modified: 23 11 June 2007 ***************
******************************************************/
/// Globals //////
function ret(iid)
{
	return document.getElementById(iid);
}
function nothing() { }

///////////////////////////// Start Browser Detection /////////////
var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

function IsIE7() {
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "7")
        return true;
} //IsIE7

function IsNewerThanIE6() {
    if (BrowserDetect.browser == "Explorer" && parseInt(BrowserDetect.version) >= 7)
        return true;
} //IsIE7

function IsNewerThanIE7() {
    if (BrowserDetect.browser == "Explorer" && parseInt(BrowserDetect.version) >= 8)
        return true;
} //IsIE7

function IsIE() {
    if (BrowserDetect.browser == "Explorer")
        return true;
} //IsIE7

function IsNetscapeFamily() {
    if (BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Firefox")
        return true;
} //IsIE7
///////////////////////////// End Browser Detection ///////////////

////////////////////////////// OBJECTS ////////////////////////////
function DateControl()
{
	this.id = "";
	this.day = 1;
	this.month = 1;
	this.year = 1900;
	this.minYear = 1900;
	var _date = new Date();
	this.maxYear = _date.getFullYear();
	this.render = render;
	
	function render()
	{
		var _html = "Day: <select class='tInput'>";
		for(i=1;i<31;i++)
			_html+= "<option value='"+i+"'>"+i+"</option>";
		_html+= "</select> &nbsp; Month: <select class='tInput'>";
		for(i=1;i<12;i++)
			_html+= "<option value='"+i+"'>"+i+"</option>";
		_html+= "</select> &nbsp; Year: <select class='tInput'>";
		for(i=this.minYear;i<this.maxYear;i++)
			_html+= "<option value='"+i+"'>"+i+"</option>";
		_html+= "</select>";
		return _html;
	}//render
}//DateControl

///Combos And ListBoxes Handling

//MoveItem
//To Move Up or down
function MoveItem(comboObj, isUp)
{
	if(comboObj.selectedIndex==-1)
		return;
	var _step = isUp?-1:1;
	if((comboObj.selectedIndex+_step)<0
		|| (comboObj.selectedIndex+_step)>(comboObj.options.length-1))
		return;
	var _itemSelected = comboObj.options[comboObj.selectedIndex];
	var _otherItem = comboObj.options[comboObj.selectedIndex+_step];
	var _itemSelectedText =  _itemSelected.text;
	var _itemSelectedValue =  _itemSelected.value;
	_itemSelected.text = _otherItem.text;
	_itemSelected.value = _otherItem.value;
	_otherItem.text = _itemSelectedText;
	_otherItem.value = _itemSelectedValue;
	
	comboObj.selectedIndex = comboObj.selectedIndex+_step;
}//MoveItem


function comboFilter(filterId,comboId,itemsName,writeHTML)
{
	this.id = filterId;
	this.combo = comboId;
	this.html = "";
	this.optionsCache = new Array();
	this.working = false;
	this.timeOut = null;
	
	//this.getHtml = getHtml;
	this.fillCache = fillCache;
	this.preformFilter = preformFilter;
	this.startFiltering = startFiltering;
	
	//function getHtml()
	//{
		_html = "Filter "+itemsName+": <input style='width:75' id='comboFilter_text_"+this.id+"' class='tInput' type='text' onKeyUp='eval(\""+this.id+".startFiltering()\")'>";
		//return _html;
	//}//getHtml
	
	function fillCache()
	{
		for(i=0;i<ret(this.combo).options.length;i++)
		{
			this.optionsCache[this.optionsCache.length]=new Option(ret(this.combo).options[i].text,ret(this.combo).options[i].value);
		}//for
	}//fillCache
	if(writeHTML)
	{
		document.write(_html);
		this.fillCache();
	}//
	else
		this.html = _html;
	
	function startFiltering()
	{
		if(this.working)
			return;
		this.timeOut = setTimeout("eval('"+this.id+".preformFilter();')",700);
	}//startFiltering
	function preformFilter()
	{
		ret(this.combo).options.length = 0;
		var _criteria = ret('comboFilter_text_'+this.id).value;
		for(i=0;i<this.optionsCache.length;i++)
		{
			if(this.optionsCache[i].text.toLowerCase().indexOf(_criteria)!=-1
			|| this.optionsCache[i].text=="")
			{
				var _option = new Option(this.optionsCache[i].text,this.optionsCache[i].value);
				ret(this.combo).options[ret(this.combo).options.length] = _option;
			}//if
		}//for
		clearTimeout(this.timeOut);
	}//
} //comboFilter

//Renders Standard Image Button
function CMSButton(name, label, image, width, height, onclick) {
    this.Name = name;
    this.Label = label;
    this.Image = image;
    this.Width = width;
    this.Height = height;
    this.OnClick = onclick;

    this.ImagePath = RootPath + "Img/cms/buttons/ar/";


    this.GetHTML = GetHTML;

    this.InnerHTML = this.GetHTML();

    function GetHTML() {
        return "<img src='" + this.ImagePath + this.Image + ".gif' style='width:" + this.Width + ";height:" + this.Height +
                ";cursor:pointer;' onmouseover='this.src=\"" + this.ImagePath + this.Image + "_over.gif\"' onmouseout='this.src=\"" + this.ImagePath + this.Image + ".gif\"' onclick='" + this.OnClick + "' />";
    } //GetHTML
} //CMSButton

//Renders Standard Image Button
function GetCMSInputText(name, label, width) {
    return GetCMSInputTextEx(name, name, label, "", width, "", "", "");
} //GetCMSInputText
function GetCMSInputTextValue(name, label, value, width) {
    return GetCMSInputTextEx(name, name, label, value, width, "", "", "");
} //GetCMSInputText
function GetCMSInputTextEx(name, id, label, value, width, height, readOnly, disabled) {
    var _value = value != "" ? value : '<' + label + '>'
    var _onActivate = 'if(this.value=="<' + label + '>"){this.value=\"\";}this.className=\"FormInputActive\"';
    return "<input baseValue='" + _value + "' name='" + name + "' id='" + id + "' class='FormInputNormal' type='text' style='width:" + width + "px;height:" + height +
                "px;' value='" + _value + "' onfocus='" + _onActivate + "' onactivate='" + _onActivate + "'  onclick='" + _onActivate + "' />";
} //GetCMSInputText

function escapeEx(str) {
    if (IsNetscapeFamily()) {
        return str;
    } //if
    else
        return escape(str);
} //escapeEx

function getTDX(TDObj)
{
	var o = TDObj;
	var oLeft = o.offsetLeft;
	
	while(o.offsetParent!=null) 
	{   
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}//while
	return oLeft;
}//getTDX
function getTDY(TDObj)
{
	var o = TDObj;
	var oTop = o.offsetTop;
	while(o.offsetParent!=null) 
	{ 
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop+18;
}//getTDX

function getCurrentPageName()
{
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}
function stamp()
{
	var Stamp = new Date();
	var returnVal = "";
	
	returnVal  = String(Stamp.getFullYear());
	returnVal += String(Stamp.getMonth());
	returnVal += String(Stamp.getDay());
	returnVal += String(Stamp.getHours());
	returnVal += String(Stamp.getMinutes());
	returnVal += String(Stamp.getSeconds());
	
	return returnVal;
}
function getPageURL(url, addRandom)
{
	if(url==null || url=="")
		url=location.href;
	if(addRandom==true)
	{	
		var randIndex=url.indexOf("rand=");
		if(randIndex>=0)	
			url = url.substring(0, randIndex-1);
		
		var rand="rand="+stamp();
		if(url.indexOf("?")>=0)
			url+="&";
		else
			url+="?";
			
		url+= rand;
	}	
	return url;
}//getPageURL
function RefreshPage(url, addRandom)
{
	document.location.href=getPageURL(url,addRandom);
}
//RefreshPage	
function getRadioValue(radioObj) 
{
	if(radioObj==null)
		return "";
		
	var radioLength = radioObj.length;
	if(radioLength == "undefined")
		return "";
		
	for(i=0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
			return radioObj[i].value;
	}
	return "";
}//getRadioCheckedValue

function isThisNumber(src,arg)
{
	var obj = ret(src.controltovalidate);
	if(obj.value=="")
		arg.IsValid = true;
	if(!isNaN(obj.value)&& parseInt(obj.value)>-1)
		arg.IsValid = true;
	else
		arg.IsValid = false;
}//isThisNumber

function isCertYearMoreThanDOB(src,arg)
{
	var obj = ret(src.controltovalidate);
	var dobObj = ret(src.DOBControl);
	if(obj.value=="")
		arg.IsValid = true;
	if(parseInt(obj.value)>(parseInt(dobObj.value)+15))
		arg.IsValid = true;
	else
		arg.IsValid = false;
}//isThisNumber

///////////////////// HTTP POST REQUEST [AJAX] Componants ///////////////
function XMLHTTPRequest() {
    this.HTTP = null;
    this.load = load;
    this.GetXMLObject = GetXMLObject;

    //Initializing HTTP
    if (window.XMLHttpRequest) {
        // Mozilla, Safari,...
        this.HTTP = new XMLHttpRequest();
        if (this.HTTP.overrideMimeType) {
            // set type accordingly to anticipated content type
            this.HTTP.overrideMimeType('text/html');
        } //if
    } //if
    else if (window.ActiveXObject) {
        // IE
        try {
            this.HTTP = new ActiveXObject("Msxml2.XMLHTTP");
        } //try
        catch (e) {
            try {
                this.HTTP = new ActiveXObject("Microsoft.XMLHTTP");
            } //try
            catch (e) { }
        } //catch
    } //else
    if (!this.HTTP) {
        alert('Cannot create XMLHTTP instance');
        return false;
    } //if

    function load(iurl, iparams, onChange, loadingIndicator, loadingStr, isPost, isUpload) 
    {
        if (this.HTTP && this.HTTP.readyState != 4 && this.HTTP.readyState != 0) 
        {
            alert("Please wait, another process is running");
            return;
        } //IF
        if (ret(loadingIndicator))
            ret(loadingIndicator).innerHTML = loadingStr;

        var params = iparams;

        var _method = isPost ? "POST" : "GET";
        if (!isPost)
            iurl = iurl + "?" + params;
        this.HTTP.open(_method, iurl, true);

        //Send the proper header information along with the request
        if (isPost) {
            var _contentType = isUpload ? "" : "application/x-www-form-urlencoded";
            this.HTTP.setRequestHeader("Content-type", _contentType);
            this.HTTP.setRequestHeader("Content-length", params.length);
            this.HTTP.send(params);
        } //if
        this.HTTP.onreadystatechange = onChange;
        this.HTTP.send(params);
    } //postRequest

    function GetXMLObject() 
    {
        if (IsIE())
            _xml = this.HTTP.responseXML.documentElement;
        if (IsNetscapeFamily()) {
            var objDOMParser = new DOMParser();
            _xml = objDOMParser.parseFromString(this.HTTP.responseText, "text/xml");
        } //if
        return _xml;
    } //GetXMLObject
} //XMLHTTPRequest
var AJAXRequest = new XMLHTTPRequest();




