var myRe = "key:\\w+\\b";
var requestControl;
var docModified;
var dataError;

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: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			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 GetStateValue (state, key)
{
    var regEx = key + ":" + "\\w*;";
    return state.value.match (regEx).toString();
}

function GetStateValueValue (state, key)
{
    var regEx = key + ":" + "\\w*;";
    
    if (state != null)
    {
        if (state.value.match (regEx) != null)
        {
            var str = state.value.match (regEx).toString();
            
            regEx = ":\\w*";
            return str.match (regEx).toString().replace(":", "");
        }
    }
    
    return null;
}

function SetStateValue (state, key, value)
{
    var regEx = key + ":" + "\\w*;";
    var match = state.value.match (regEx);
    if (match)
    {
        state.value = state.value.replace (match, key + ":" + value + ";");
    }
    else
    {
        state.value += key + ":" + value + ";";
    }
}

function RemoveStateValue (state, key)
{
    var regEx = key + ":" + "\\w*;";
    var match = state.value.match (regEx);
    if (match)
    {
        state.value = state.value.replace (match, "");
    }
}

function GetAttribute (element, name)
{
    if (element == null)
        return "";
        
    return element.getAttribute (name);
}

function SetAttribute (element, name, value, state)
{
    if (element.attributes.getNamedItem (name) != null)
    {
        element.attributes.getNamedItem (name).value = value;
    
        if (state != null)
            SetStateValue (state, name, value);
    }
}

function getWindowHeight ()
{
    var windowHeight = 0;
    if (typeof(window.innerHeight)=='number')
    {
        windowHeight=window.innerHeight;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientHeight)
        {
            windowHeight = document.documentElement.clientHeight;
        }
        else
        {
            if (document.body&&document.body.clientHeight)
            {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    
    return windowHeight;
}

function getWindowWidth ()
{
    var windowWidth = 0;
    
    if (typeof(window.innerWidth)=='number')
    {
        windowWidth = window.innerWidth;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientWidth)
        {
            windowWidth = document.documentElement.clientWidth;
        }
        else
        {
            if (document.body && document.body.clientWidth)
            {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    
    return windowWidth;
}

function getScrollXY () 
{
    var scrOfX = 0, scrOfY = 0;
    
    if ( typeof( window.pageYOffset ) == 'number' ) 
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function CallCustomFunction (ctrl, fnc)
{   
    var str = GetAttribute (ctrl, fnc);
    var result = true;
    
    if (str != null)
    {
        var params = str.split (",");
        if (params.length == 2)
		{
			customFnc = new Function (params [0], params [1]);
		    result = customFnc (ctrl);
		}
    }		
    
    return result;
}

function SetValue (ctrl, value)
{
    if (ctrl.value != null)
        ctrl.value = value;
    else if (GetAttribute (ctrl, "ctrlType") == "GlCalendar")
    {
        SetAttribute (ctrl, "value", value);
        if (value == "")
        {
            var ctrls = GetAttribute (ctrl, "textControls").split (";");
            document.getElementById(ctrls [0]).value = "";
            document.getElementById(ctrls [1]).value = "";
            document.getElementById(ctrls [2]).value = "";
            
            SetAttribute (ctrl, "dd", "");
            SetAttribute (ctrl, "MM", "");
            SetAttribute (ctrl, "yyyy", "");
        }            
        else
        {
            var ctrls = GetAttribute (ctrl, "textControls").split (";");
            var dateParts = value.split ("/");
            document.getElementById(ctrls [0]).value = dateParts [0];
            document.getElementById(ctrls [1]).value = dateParts [1];
            document.getElementById(ctrls [2]).value = dateParts [2];
            
            SetAttribute (ctrl, "dd", dateParts [0]);
            SetAttribute (ctrl, "MM", dateParts [1]);
            SetAttribute (ctrl, "yyyy", dateParts [2]);
        }
    }
    else
    {
        ctrl.innerHTML = value;
    }
}

function GetDDLText (ddl)
{
    return ddl.options [ddl.selectedIndex].innerHTML;
}

function mainPageUnload(pageName)
{
    AjaxBasePage.AjaxDispose (document.getElementById("sessionState").value, pageName); 
}

function positionLoadingLabel (b)
{
	window.status = b ? "Loading..." : "";
	var loading = document.getElementById("loading");
	var offset = getWindowHeight () / 2  - loading.offsetHeight + document.documentElement.scrollTop;
	loading.style.top = offset + 'px';
	offset = getWindowWidth () / 2 - loading.offsetWidth;
	loading.style.left = offset + 'px';
	loading.style.position="absolute";
	loading.style.display = b ? "inline" : "none";
}

function onDocChanged ()
{
    docModified = true;
}

function ShowHideElement (elementName, show)
{
    if (show)
        ShowElement (elementName);
    else
        HideElement (elementName);
}

function ShowElement (elementName)
{
    var el = document.getElementById (elementName);

    if (el != null)
    {   
        el.style.display = "";
//        // el.style.visibility = "visible";
//        el.style.position = "";
//        
//        if (BrowserDetect.browser == "Explorer")
//        {
//            el.style.left = "";
//            el.style.top = "";
//        }
    }
}

function HideElement (elementName)
{
    var el = document.getElementById (elementName);
    
    if (el != null)
    {
        el.style.display = "none";
        // el.style.visibility = "hidden";
//        el.style.position = "absolute";
//        
////        if (BrowserDetect.browser == "Explorer")
////        {
//            el.style.left = "-10000px";
//            el.style.top = "-10000px";
////        }
    }
}

function RequiredMissing (ctrlName)
{
    var ctrl = document.getElementById (ctrlName);
    
    if (ctrl.value.length == 0)
        return true;
        
    return false;
}

function IsVisible (ctrl)
{
    var parent = ctrl;
    
    while (parent != null)
    {
        if (parent.style != null && parent.style.display == "none")
            return false;
            
        parent = parent.parentNode;
    }
    
    return true;
}

function $ (element)
{
    return document.getElementById (element);
}

function GetElementPos (element)
{
	var curleft = curtop = 0;
	if (element.offsetParent) 
	{
		curleft = element.offsetLeft;
		curtop = element.offsetTop;
		
		while (element = element.offsetParent)
		{
			curleft += element.offsetLeft;
			curtop += element.offsetTop;
		}
	}
	
	return [curleft, curtop];
}