﻿// JScript File
var publicWebserviceUrl = "webservice.asmx";
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function Querystring(qs) 
{
	this.params = new Object();
	this.get=Querystring_get;
	this.names = [];
	if (qs == null)
		qs=location.search.substring(1,location.search.length);
	
	if (qs.length == 0) return;

	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &

// split out each name=value pair
	for (var i=0;i<args.length;i++) 
	{
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);

		if (pair.length == 2)
			value = unescape(pair[1]);
		else
			value = name;
		this.names.push(name);
		this.params[name] = value;
	}
	
    function Querystring_get(key, default_) {
	    // This silly looking line changes UNDEFINED to NULL
	    //if (default_ == null) default_ = null;
    	
	    var value="";
	    if(isNaN(key))
		    value=this.params[key];
	    else
		    value=this.params[this.names[key]];
    	
	    if (value==null) value=default_;
    	
	    return value;
    }	
}

function getQueryStringValue(key)
{
	var qs = new Querystring();
	if(qs.params[key] == null)
		return "";

	return qs.params[key];
}

function openWindow(url,name)
{
	window.open(url,name);
}

function FormFields()
{
    var fields = [];
    var self = this;
    
    this.add = function(control, caption, controlContainerName, regExp, paramName)
    {
        fields.push(new Field(control, caption, controlContainerName, regExp, paramName));
    }   
    
    this.get = function(index)
    {
        return fields[index];    
    }
    
    this.getById = function(id)
    {
        for(var i=0;i<fields.length;i++)
        {
            var field = this.get(i);
            if(field.id == id)
                return field;
        }
        
        return null;
    }
    
    this.validate = function()
    {
        var isValid = true;
        for(var i=0;i<this.count();i++)    
        {
            var field = this.get(i);
            if(!(field.enable))
                continue;
            if(!(field.validate()))
            {
                isValid = false;
            }
        }
        return isValid;
    }    
    
    this.count = function()
    {
        return fields.length;
    }
    
    this.toObject = function()
    {
        var o = {};
        for(var i=0;i<this.count();i++)
        {
            var field = this.get(i);
            if(field.enable)
            {
                //alert(field.id + ":" + field.control.value);
                o[field.paramName] = field.control.value;
            }
        }
        return o;
    }

    this.disable = function()
    {
        for(var i=0;i<this.count();i++)
        {
            var field = this.get(i);
            field.control.disable();
        }
    }
    
    this.removeById = function(id)
    {
        for(var i=0;i<this.count();i++)
        {
            if(this.get(i).id == id)
                fields.splice(i,1);
        }
    }
    
    function Field(control, caption, controlContainerName, regExp, paramName)
    {
        this.control = $(control);
        this.id = control.id;
        this.paramName = this.id;
        
        if(paramName != null)
            this.paramName = paramName;
        this.caption = caption;
        this.regExp = regExp;
        this.controlContainer = $(controlContainerName);
        this.enable = true;
        this.validate = function ()
        {
            if(this.regExp==null)
                return true;
            if(this.control==null)
                return false;
            if(this.controlContainer == null)
                this.controlContainer = this.control;

            this.controlContainer.removeClassName("invalid");
            if(!(this.regExp.test(this.control.value)))
            {
                this.controlContainer.addClassName("invalid");
                return false;
            }
            return true;
        }
        
        this.disable = function()
        {
            this.enable = false;
            if(this.control == null || this.controlContainer == null)
                return;
            this.controlContainer.setStyle({display:"none"});
        }
    }
}

function XmlParser()
{
    this.title = "";
    this.isSuccess = false;
    this.xml = null;
    this.status = "";
    this.dataTable = new DataTable();

    this.load = function(xml)
    {
        xml = xml.responseXML;
        this.xml = xml;
        var node;
        
        try
        {
            node = xml.getElementsByTagName("response")[0];
            this.title = node.getAttribute("title");
            node = node.getElementsByTagName("status")[0];
            this.status = node.firstChild.nodeValue;
            if(this.status.toLowerCase() == "ok!")
                this.isSuccess = true;   
        }catch(e){}
        if(this.isSuccess)
        {   
            node = null;
            var rowNodes;
            node = xml.getElementsByTagName("rows")[0];
            if(node==null)
                return;
            try
            {
                    node = Element.cleanWhitespace(node);
            }catch(e){}
            
            rowNodes = node.childNodes;
            
            if(rowNodes.length>0)
            {
                for(var i=0;i<rowNodes.length;i++)
                {
                    var rowNode = rowNodes[i];
                    try
                    {
                        rowNode = Element.cleanWhitespace(rowNode);
                    }catch(e){}

                    var fieldNodes = rowNode.childNodes;
                    
                    var fields = new this.dataTable.Fields();
                    for(var j=0;j<fieldNodes.length;j++)
                    {
                        var name = "";
                        var value = "";
                        try
                        {
                            name = fieldNodes[j].nodeName;
                        }catch(e){}

                        try
                        {
                            value = Element.cleanWhitespace(fieldNodes[j]).firstChild.nodeValue;
                        }
                        catch(e){value = fieldNodes[j].firstChild.nodeValue}
                        fields.add(name, value);
                    }
                    this.dataTable.add(fields);
                }
            }
        }
    }
}

function DataTable()
{
    var rows = [];
    
    this.add = function(fields)
    {
        rows.push(fields);
    }
    
    this.get = function(index)
    {
        return rows[index];
    }
    
    this.count = function()
    {
        return rows.length;
    }
    
    this.Fields = function ()
    {
        var fields = [];
    
        this.add = function(name, value)
        {
            var field = new this.Field(name,value);
            fields.push(field);
        }
        
        this.get = function(index)
        {
            if(typeof index == "number")
                return fields[index];
            else if(typeof index == "string")
                return fields[getIndexOf(index)];
        }
        
        function getIndexOf(index)
        {
            index = index.toLowerCase();
            for(var i=0;i<fields.length;i++)
            {
                if(index == fields[i].name.toLowerCase())
                    return i;
            }
            return -1;
        }
        
        this.count = function()
        {
            return fields.length;
        }
        
        this.Field = function(name, value)
        {
            this.name = name;
            this.value = value;
        }
    }
}

function doRedirect(url)
{
    if(url.indexOf("http") == -1)
    {
        var items = window.location.href.split("/");
        items.pop();
        url = items.join("/") + "/" + url;
    }
    window.location.href = url;
}

function setResultText(div, text)
{
    if(div==null)
        return;
    div.innerHTML = text;
    //if(div.getStyle("display")!="none")
    //return;
    if(text.length > 0)
    {    
        div.appear({duration:2});
        setTimeout(fadeOut,2500);
    }
    else
    {
        fadeOut();
    }
    
    function fadeOut()
    {
        div.fade({duration:2});
    }
}