﻿

function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent) 
    {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


 
 
var tabsClass = {
	tabSetArray: 	new Array(),
	classOn: 		"tabs_on",
	classOff: 		"tabs_off",
	
	addTabs: function (tabsContainer) {
	    for(var i=0; i < document.getElementById(tabsContainer).cells.length; i++)
	    {
	        if(typeof(document.getElementById(tabsContainer).cells[i].id) != "undefined")
	        {
	            this.tabSetArray[this.tabSetArray.length] = document.getElementById(tabsContainer).cells[i].id;   
	        }
	    }
	/*
		tabs = document.getElementById(tabsContainer).cells;
		for (x in tabs) {
			if (typeof(tabs[x].id) != "undefined") {
				this.tabSetArray.push(tabs[x].id);
			} else {}
		}
	*/
	},

	switchTab: function (element) {
		for (var i=0; i<this.tabSetArray.length;i++) {
			tabItem = this.tabSetArray[i];
			//alert(tabItem);
			if(tabItem.toString() == "")
			    continue;
			dataElement = document.getElementById(tabItem + "_data");
			if (dataElement) {
				if (dataElement.style.display != "none") {
					dataElement.style.display = "none";
				} else {}
			} else {}
            
			tabElement = document.getElementById(tabItem);
			
			if (tabElement) {
				if (tabElement.className != this.classOff) {
					tabElement.className = this.classOff;
				} else {}
			} else {}
		}

		document.getElementById(element.id + "_data").style.display = "";
		element.className = this.classOn;
	}
};


function ChangeImage(mainLnk, imageLink, target)
{
    var objTarget = document.getElementById(target);
    try{
        objTarget.getElementsByTagName("img")[0].src = imageLink;
        objTarget.getElementsByTagName("a")[0].href = mainLnk;
    }
    catch(e)
    {
    
    }
    
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


function SerializeContainer(containerID, xmlRoot, xmlElementAttribute, errorAttribute, ValidationAttribute)
{
    var objContainer = $(containerID);
    var objInputList = objContainer.getElementsByTagName("INPUT");
    var objSelList = objContainer.getElementsByTagName("SELECT");
    var objtextAreaList = objContainer.getElementsByTagName("TEXTAREA");
    
    var strError = "";
    var objXml = new Array();
    objXml.push("<");
    objXml.push(xmlRoot);
    objXml.push(">");
    
    
    for(var i=0; i < objInputList.length; i++)
    {
        if(objInputList[i].getAttribute(xmlElementAttribute)!= null)
        {
            var xmlElement = objInputList[i].getAttribute(xmlElementAttribute);
            var objVal = getInputValue(objInputList[i]);
            if(objVal == "" && objInputList[i].getAttribute(errorAttribute)!= null)
            {
                strError +=  objInputList[i].getAttribute(errorAttribute) +"\n"; 
            }
            else if(objVal != "" && objInputList[i].getAttribute(ValidationAttribute) != null && !ValidateExpression(objInputList[i].getAttribute(ValidationAttribute), objVal))
            {
                strError +=  "Invalid: "+ xmlElement +"\n";  
            }
            else
            {
                objXml.push("<");
                objXml.push(xmlElement);
                objXml.push("><![CDATA[");
                objXml.push(objVal);
                objXml.push("]]></");
                objXml.push(xmlElement);
                objXml.push(">");            
            }
        }
    
    }
    
    for(var i=0; i < objtextAreaList.length; i++)
    {
        if(objtextAreaList[i].getAttribute(xmlElementAttribute)!= null)
        {
            var xmlElement = objtextAreaList[i].getAttribute(xmlElementAttribute);
            var objVal = getInputValue(objtextAreaList[i]);
            if(objVal == "" && objtextAreaList[i].getAttribute(errorAttribute)!= null)
            {
                strError +=  objtextAreaList[i].getAttribute(errorAttribute) +"\n"; 
            }
            else if(objVal != "" && objtextAreaList[i].getAttribute(ValidationAttribute) != null && !ValidateExpression(objtextAreaList[i].getAttribute(ValidationAttribute), objVal))
            {
                strError +=  "Invalid: "+ xmlElement +"\n";  
            }
            else
            {
                objXml.push("<");
                objXml.push(xmlElement);
                objXml.push("><![CDATA[");
                objXml.push(objVal);
                objXml.push("]]></");
                objXml.push(xmlElement);
                objXml.push(">");            
            }
        }
    
    }
    
    
    for(var i=0; i < objSelList.length; i++)
    {
        if(objSelList[i].getAttribute(xmlElementAttribute)!= null)
        {
            var xmlElement = objSelList[i].getAttribute(xmlElementAttribute);
            var objVal = getInputValue(objSelList[i]);
            if((objVal == "" || objVal=="-1") && objSelList[i].getAttribute(errorAttribute)!= null)
            {
                strError +=  objSelList[i].getAttribute(errorAttribute) +"\n"; 
            }
            else
            {
                objXml.push("<");
                objXml.push(xmlElement);
                objXml.push("><![CDATA[");
                objXml.push(objVal);
                objXml.push("]]></");
                objXml.push(xmlElement);
                objXml.push(">");            
            }
        }
    
    }
    
     //   strIds += objInputList[i].id + "\n";
    
    
    
    objXml.push("</");
    objXml.push(xmlRoot);
    objXml.push(">");
    
    if( strError != "")
    {
        alert(strError);
        return null;
    }
    else
        return objXml.join("");

}

function ValidateExpression(regEx, value)
{
    var myregexp = new RegExp(regEx)
    var matches = myregexp.exec(value);
    return ((matches != null) && (matches.length > 0));

}


    function getInputValue(id)
    {
        if(id == null)
            return null;
        
        var obj = id;
        if(typeof(id) == "string")
            obj = $(id);
        
        if(obj && obj.tagName.toUpperCase() == "SELECT")
        {
            var strVal = "";
            
            for(var i=0; i < obj.length; i++)
            {
                
                if(obj[i].selected)
                {
                    if(strVal.length > 0)
                        strVal += ",";
                    
                    strVal += obj[i].value;
                }
            }
            return strVal;
        }
        else if(typeof(obj.type) != "undefined" && (obj.type.toUpperCase() == "CHECKBOX" || obj.type.toUpperCase() == "RADIO"))
            return obj.Checked? "1" : "0";
        else if(typeof(obj.value) != "undefined")
        {
            return obj.value.trim();
        }
        
        return null;
            
    }
    
    function CreateWaitDiv(objSource)
    {
        if(typeof(objSource.tagName) == "undefined")
            objSource = $(objSource);
        //alert(objSource);   
        var pos = findPos(objSource)
        //Utilitytransparent
        var objDIV = $("___Div__WAIT__");
        if(!objDIV)
        {
            objDIV = document.createElement("div");
            objDIV.setAttribute("id", "___Div__WAIT__");
            document.body.appendChild(objDIV);
            objDIV.className = 'utilitytransparent';
            objDIV.innerHTML  = "<table ><tr><td><img src='"+ ___waitImageLocation__ +"'></td><td>Please wait....</td></tr></table>";
        }
        
        objDIV.style.top = pos[1] +"px";
        objDIV.style.left = pos[0] +"px";
        objDIV.style.height = objSource.offsetHeight +"px";
        objDIV.style.width = objSource.offsetWidth +"px";
        objDIV.style.display = 'block';
    
    }
    
    function  HideWaitDiv()
    {
        
        var objDIV = $("___Div__WAIT__");
        if(objDIV)
        {
            objDIV.style.display = 'none';  
        }
    }
    
    function AddUpdateUser(containerID)
        {
            var str = SerializeContainer(containerID, "root", "xmlElement", "errorText", "validationText");
           if(str)
           {
                CreateWaitDiv(containerID);
                var clt = new ClientMethods(HideWaitDiv, null);
                clt.AddUser(str);
           }
        }
   
    function CheckUniqueUser(userName, targetDiv)
    {
        var clt = new ClientMethods(null, targetDiv);
        clt.CheckUniqueUser(userName);  
    }
    
    
    function DeleteGBookEntry(gbook, ID)
    {
        if(confirm("Are you sure you want to delete this entry?"))
        {
            CreateWaitDiv(gbook);
            var mthds = new ClientMethods( HideWaitDiv, GuestBookSpan());
            mthds.DeleteGuestBook(ID);
            
        }
    }
 
    function GuestBookSpan()
    {
        return $("spnGuestBook");
    }
    
    function ShowAddGuestBook()
    {
        var obj = new MainClientMethods(DisplayAddGuestBook);
        var dv = CreateTempDiv();
        CreateWaitDiv(GuestBookSpan());
        obj.GetAddGuestBookControl();   
    }
    
    function HideTempDivs()
    {
      var __tempDiv = document.getElementById("___Div__TEMP__BACK");
      __tempDiv.style.display = "none";
      __tempDiv = document.getElementById("___Div__TEMP__");
      __tempDiv.style.display = "none";
    }
    
    function CreateTempDiv()
    {
        var __tempDiv = document.getElementById("___Div__TEMP__BACK");
        if(__tempDiv == null)
        {
            __tempDiv = document.createElement("div");
            __tempDiv.setAttribute("id", "___Div__TEMP__BACK");
            document.body.appendChild(__tempDiv);
            __tempDiv.className = 'utilitytransparent';
        }
        __tempDiv.style.top = "0px";
        __tempDiv.style.left = "0px";
        __tempDiv.style.width = document.documentElement.clientWidth + "px";
        __tempDiv.style.height = document.documentElement.clientHeight + "px";
        __tempDiv.style.display = "block";
        return __tempDiv;
        
    }
    function CreateTempTopDiv()
    {
        var __tempDiv = document.getElementById("___Div__TEMP__");
        if(__tempDiv == null)
        {
            __tempDiv = document.createElement("div");
            __tempDiv.setAttribute("id", "___Div__TEMP__");
            document.body.appendChild(__tempDiv);
            __tempDiv.className = 'utilityTempdiv';
        }
        __tempDiv.style.top = "0px";
        __tempDiv.style.left = "0px";
        __tempDiv.style.display = "block";
        return __tempDiv;
        
    }
    
    function FixTempTopDiv()
    {
        var __tempDiv = document.getElementById("___Div__TEMP__");
        if(__tempDiv != null)
        {
            __tempDiv.style.top = (document.documentElement.clientHeight/2 - __tempDiv.offsetHeight/2) +"px";
            __tempDiv.style.left = (document.documentElement.clientWidth/2 - __tempDiv.offsetWidth/2) +"px";
              
        }   
    
    }
    
    function DisplayAddGuestBook(str)
    {
        HideWaitDiv();
        CreateTempTopDiv().innerHTML = str;
        FixTempTopDiv();
        
    }
    
    function SaveGuestBookEntry(tbl, rsltSpan, root, element, required, regex)
    {
        var strXML = SerializeContainer(tbl, root, element, required, regex);
        if(strXML)
        {
            var main = new MainClientMethods(AddGuestBookHandler, new Array(rsltSpan));
            main.InsertGuestBook(strXML);
        }
        
    }
    
    function AddGuestBookHandler(str, obj)
    {
        if(str.indexOf("SUCCESS") < 0)
        {
            $(obj[0]).innerHTML = str;
        }
        else
        {
            HideTempDivs(); 
            CreateWaitDiv(GuestBookSpan());
            var main = new MainClientMethods(HideWaitDiv, GuestBookSpan());
            main.GetGuestBookControl();  
        }
    }
    

