﻿// JScript File
//These are browser Compatibility scripts


  //These are browser Compatibility scripts

// Layer Compatibility
    function getElementFromID(ID){ 
      var element = null; 
      if (document.getElementById) 
        element = document.getElementById(ID) 
      else if (document.layers) 
        element = document.layers[ID] 
      else if (document.all) 
        element = document.all[ID]; 

      return element; 
    } 


    function getElementByID(ID) 
    {
    return document.getElementById(ID)
    }
    
 
    function getElementById(ID) 
    {
    return document.getElementById(ID)
    }
       

//Layer Compatibily
    var DHTML = (document.getElementById || document.all || document.layers);

    //Get different layer definition for different browsers
    function getObj(name)
    {
      if (document.getElementById)
      {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
      }
      else if (document.all)
      {
        this.obj = document.all[name];
        this.style = document.all[name].style;
      }
      else if (document.layers)
      {
        this.obj = document.layers[name];
        this.style = document.layers[name];
      }
    }
      
  
//XML DOM compatibility    
   function BrowserXML()
    {
        //load xml file code for IE
        if (window.ActiveXObject)
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
        }
        // code for Mozilla, etc.
        else if (document.implementation && document.implementation.createDocument)
        {
            xmlDoc= document.implementation.createDocument("","",null);
        }
        else
        {
            alert('Your browser cannot handle this script - update browser');
        }
    }
        
   
//---------------------------
     

//Add "xml" and "text" attributes for DOM (mainly for Mozzilla)

if (window.Node && Node.prototype)
{
    Node.prototype.__defineGetter__("xml", _Node_getXML);
    Node.prototype.__defineGetter__("text", _Get_InnerText);
}


if (window.Node && Node.prototype && !Node.prototype.contains)
{
	Node.prototype.contains = function (arg) {
		return !!(this.compareDocumentPosition(arg) & 16)
	}
}

function _Node_getXML() {
    
    //create a new XMLSerializer
    var objXMLSerializer = new XMLSerializer;
    
    //get the XML string
    var strXML = objXMLSerializer.serializeToString(this);
    
    //return the XML string
    return strXML;
}

   

function _Get_InnerText() { 
   if (typeof this.textContent != 'undefined') 
   { 
     return this.textContent; 
   } 
   else if (typeof this.innerText != 'undefined') 
   { 
     return this.innerText; 
   } 
   else if (typeof this.text != 'undefined') 
   { 
     return this.text; 
   } 
      
 }     
  
     
 /* COMMENTED OUT

 if (document.implementation.createDocument)
 { 
       //add the loadXML() method to the Document class
      Document.prototype.loadXML = function(strXML) 
      {
        //create a DOMParser
        var objDOMParser = new DOMParser();
            
        //create new document from string
        var xmlDoc = objDOMParser.parseFromString(strXML, "text/xml");
     
      } //End: function
 
     //make sure to remove all nodes from the document
    while (this.hasChildNodes())
      this.removeChild(this.lastChild);
      
      
    //add the nodes from the new document
    for (var i=0; i < objDoc.childNodes.length; i++) {
                
      //import the node
      var objImportedNode = this.importNode(objDoc.childNodes[i], true);
                
      //append the child to the current document
      this.appendChild(objImportedNode);
            
    } //End: for
     */
     
 //}
 
 //
 
 /*/------------------- /* COMMENTED OUT

//XML DOM compatibility    
   function BrowserloadXML()
    {alert("infunct")
     if (document.implementation.createDocument)
     { 
          // Mozilla, create a new DOMParser
         
           
          //add the loadXML() method to the Document class
          Document.prototype.loadXML = function(strXML) 
          {
            //create a DOMParser
            var objDOMParser = new DOMParser();
                
            //create new document from string
            var xmlDoc = objDOMParser.parseFromString(strXML, "text/xml");
         
          } //End: function
         
      // var parser = new DOMParser();  
      // xmlDoc = parser.parseFromString(result, "text/xml"); 
      } 
     else if (window.ActiveXObject)
     {
       // Internet Explorer, create a new XML document using ActiveX 
       // and use loadXML as a DOM parser. 
       xmlDoc = new ActiveXObject("Microsoft.XMLDOM") 
       xmlDoc.async="false"; 
       //xmlDoc.loadXML(result);   
     } 
 
  }
    
    //-------------------------------      

*/

function CreateXMLDOMDocument(strXML)
{ 
     if (document.implementation.createDocument)
     { 
       // Mozilla, create a new DOMParser
     
       var parser = new DOMParser();  
       xmlDoc = parser.parseFromString(strXML, "text/xml"); 
     } 
     else if (window.ActiveXObject)
     {
     
       // Internet Explorer, create a new XML document using ActiveX 
       // and use loadXML as a DOM parser. 
       xmlDoc = new ActiveXObject("Microsoft.XMLDOM") 
       xmlDoc.async="false"; 
       xmlDoc.loadXML(strXML);   
     } 

}

      
        



    //######################################################################
    // Trim function to remove empty spaces at end and begining of text
    //#####################################################################

        String.prototype.trim = function() {

        return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");

        }

        String.prototype.fulltrim = function() {

        return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");

        }


    //######################################################################
    // Trim function to remove empty spaces at end and begining of text
    //#####################################################################

        function Trim(Text)
        {
        return Text.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
        }

        function Fulltrim(Text) 
        {
        return Text.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
        }


    //---------------------------------------------------------
    
    //#################################
    //Convert Block To CDATA
    //################################
    
    function CDATA(Data)
    {
    
    return "<![CDATA[" + Data + "]]>"
    
    
    }
    
    //#################################
    //Remove CDATA markup
    //################################
    
    
    function RemoveCDATATag(CData, Tag)
    {
    StartTag = "<" + Tag + ">" + "<![CDATA[";
    EndTag = "]]>" + "</" + Tag + ">";
    ReplaceStartTag = CData.replace(StartTag, "")
    ReplaceEndTag = ReplaceStartTag.replace(EndTag, "");
    
    return ReplaceEndTag;
    
    
    }
    
    
    function RemoveCDATA(CData)
    {
    StartTag = "<![CDATA[";
    EndTag = "]]>" ;
    ReplaceStartTag = CData.replace(StartTag, "")
    ReplaceEndTag = ReplaceStartTag.replace(EndTag, "");
    
    return ReplaceEndTag;
    
    
    }    
    
    //#######################################################
    // Get OuterHTML - Need a hidden layer "TempDiv" on page
    //#######################################################
    /*function getOuterHTML(obj)
    {
     temp=document.getElementById(obj).cloneNode(true)
     document.getElementById('TempDiv').appendChild(temp)
     outer=document.getElementById('TempDiv').innerHTML
     document.getElementById('TempDiv').innerHTML=""
     return outer
    }
    */
    
  
  //#########################################################
  // Get Selected Radio Value from Object document.forms[0].
  //#########################################################  
    
  function getSelectedRadio(RadioObject)
    {  

    var SelectedRadioValue = "";
    for (i=0;i<RadioObject.length;i++)
    {
	    if (RadioObject[i].checked)
	    {
		    SelectedRadioValue = RadioObject[i].value;
	    }
    }

    return SelectedRadioValue;    
       
    }
    
 function setSelectedRadio(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}   
    
    
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}



function OpenNewWindow(newURL, newName, newFeatures, parentName) {
 
  var remote = window.open(newURL, newName, newFeatures);
  if (remote.opener == null) // if something went wrong in opening new window, revert to current window
    remote.opener = window;
  //give a name to the parent window so that can reference it by name eg. using "target" in href 
   remote.opener.name = parentName; 
 
  return remote;
}    

// find out where an element is on the page. 
//This script finds the real position, so if you resize the page and run the script again, 
//it points to the correct new position of the element
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];
}


//#############################################
//      DROPDOWN
//############################################




function SetOptionValue(Me, LabelName )
{
    var DropDownList = Me.id
    var SelectedIndex = getElementFromID(DropDownList).selectedIndex
    var Value = getElementFromID(DropDownList).options[SelectedIndex].value
 
    getElementFromID(LabelName).value = Value

}


//Sets dropdown menu with server name 
function SetServerDropDownValue(ClientDropdownNameLabel, LabelName, Value) 
{
    var DropDownID = getElementFromID(ClientDropdownNameLabel).value
    SetDropDownValue(getElementFromID(DropDownID), Value)

    getElementFromID(LabelName).value = Value;

}



//Get Selected value
function GetDropDownValue(DropdownID) 
{
    var SelectedIndex = DropdownID.selectedIndex
    return DropdownID.options[SelectedIndex].value
    
}



function GetDropDownText(DropdownID) 
{
    var SelectedIndex = DropdownID.selectedIndex
    return DropdownID.options[SelectedIndex].text
    
}


//Set a dropdown to a specific value - 
//leaving "SetDropDownIndex" for backward compatibility - same as "SetDropDownValue"
function SetDropDownIndex(DropdownID, Value) 
{
    for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if ( DropdownID.options[i].value == Value ) 
        {
            DropdownID.options[i].selected = true;
            return;
        }
    }
}

//same as above, just different function name
function SetDropDownValue(DropdownID, Value) 
{
    for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if ( DropdownID.options[i].value == Value ) 
        {
            DropdownID.options[i].selected = true;
            return;
        }
    }
}


function SetDropDownText(DropdownID, Text) 
{
    for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if ( DropdownID.options[i].text == Text ) 
        {
            DropdownID.options[i].selected = true;
            return;
        }
    }
}


//Check of dropdown contains a specific value
function CheckDropDownValue(DropdownID, Value) 
{
    for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if ( DropdownID.options[i].value == Value ) 
            return true;
    }
    
    return false
}

function CheckDropDownText(DropdownID, Text) 
{
    for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if ( DropdownID.options[i].text == Text ) 
            return true;
    }
    
    return false
    
}


function AddDropDownItem(DropdownID, Value, Text) 
{
    DropdownID.options[DropdownID.options.length].text == Text
    DropdownID.options[DropdownID.options.length].Value == Value
    
}

//dropdown with multiple select
function GetMultipleDropDownValue(DropdownID) 
{
    var Value = "";
    
   for ( var i = 0; i < DropdownID.options.length; i++ ) 
    {
        if (DropdownID.options[i].selected == true) 
        { 
         Value += DropdownID.options[i].value + "," 
        }
    }
    
    return Value
}

function SetMultipleDropDownValue(DropdownID, Values) 
{

   var Value = Values.split(",");

    for ( var i = 0; i < Value.length; i++ ) 
    {
        SetDropDownValue(DropdownID, Value[i])
    }
    
    return;

}


//#########################################################################
//ASP.NET MULTIPLE CALLBACK PROBLEM  FIX
//http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1741483&SiteID=1
//This enable multiple call back 
//#######################################################################

//ORIGINAL ASP CALL BACK FUNCTION
/*
function WebForm_CallbackComplete() {
    for (i = 0; i < __pendingCallbacks.length; i++) {
        callbackObject = __pendingCallbacks[i];
        if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
            WebForm_ExecuteCallback(callbackObject);
            if (!__pendingCallbacks[i].async) {
                __synchronousCallBackIndex = -1;
            }
            __pendingCallbacks[i] = null;
            var callbackFrameID = "__CALLBACKFRAME" + i;
            var xmlRequestFrame = document.getElementById(callbackFrameID);
            if (xmlRequestFrame) {
                xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
            }
        }
    }
}


*/


//Fixing this requires just moving the WebForm_ExecuteCallback statement as the last statement in 
//WebForm_CallbackComplete routine. This ensures that __pendingCallbacks global array is updated 
//before the OnComplete function gets invoked.
// 
//The callback should be executed after releasing all resources 
// associated with this request. 
// Originally if the callback gets executed here and the callback 
// routine makes another ASP.NET ajax request then the pending slots and
// pending callbacks array gets messed up since the slot is not released
// before the next ASP.NET request comes.

function WebForm_CallbackComplete_SyncFix() {
    for (i = 0; i < __pendingCallbacks.length; i++) {
        callbackObject = __pendingCallbacks[i];
        if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
           
           // FIX: This statement has been moved below
           // WebForm_ExecuteCallback(callbackObject);
            if (!__pendingCallbacks[i].async) {
                __synchronousCallBackIndex = -1;
            }
            __pendingCallbacks[i] = null;
            var callbackFrameID = "__CALLBACKFRAME" + i;
            var xmlRequestFrame = document.getElementById(callbackFrameID);
            if (xmlRequestFrame) {
                xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
            }
            
            // SyncFix: the following statement has been moved down from above;
            WebForm_ExecuteCallback(callbackObject);
            
        }
    }
}


//Lastly, in order to make sure this function is called instead of original version, 
//the following statement should be executed as startup script.

if (typeof (WebForm_CallbackComplete) == "function") {
  // set the original version with fixed version
  WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
}


//###################################################################################################################
        
      // STRING VALIDATION
    
    //#####################################################################
  
  // sample alternative string expression test
 //    var IsHeader = /^Header/;
  //  var IsBody = /^Body/;
  // 
  //  if(IsHeader.test(Attribute))
 
  
  
    
  //I recommend that you do not use the RegExp constructor with a literal string, because in literal strings, 
  //backslashes must be escaped. The regular expression \w+ can be created as re = /\w+/ or as re = new RegExp("\\w+"). 
  //The latter is definitely harder to read. The regular expression \\ matches a single backslash. In JavaScript, 
  //this becomes re = /\\/ or re = new RegExp("\\\\").  
    
    function ValidTestString(Str)
    {
            var reg = new RegExp("^([a-zA-Z]+)$");
            
            return reg.test(Str)
            
    }
   
   
    function CheckEmptyString(Str)
    {
         //Check string contains a character
         
          if(Trim(Str).length == 0)
            return true
          else
            return false
            
    }
   
   
   //var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
   //               ~!@#$%^&*()+  {}|:"<>?`-=[]\;',./
   
    function CheckSpecialCharString(Str)
    {
            //var reg = new RegExp("!@#$%^&*()+=-[]\\\';,./{}|\":<>?~`");
            //return reg.test(Str)
            
            var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~`";

            for (var i = 0; i < Str.length; i++) 
            {
            if (iChars.indexOf(Str.charAt(i)) != -1)
                return true;  //reutrn true if special character found
            }   
             return false;

    }
   
     //Validate user name- some special characters not allowed in names 
     //allowed special characters :   -'`
    function ValidNameString(Str)
    {
    
            //first check for empty string
            if(Trim(Str).length == 0)
                return false
    
            //var reg = new RegExp("[!@#$%^&*()+=_[]\\;,./{}|\":<>?~]");
            //return reg.test(Str)
            
            var iChars = "!@#$%^&*()+=_[]\\;,./{}|\":<>?~";

            for (var i = 0; i < Str.length; i++) 
            {
            if (iChars.indexOf(Str.charAt(i)) != -1)
                return false;       //reutrn false if special character found
            }
            
             return true;
            
    }
    
   //Validate user name- some special characters not allowed in names 
   //allowed special characters :   -'` . Cant have french or other character name with this
   
   function ValidNameString2(Str)
    {
    
            //first check for empty string
            if(Trim(Str).length == 0)
                return false
    
            var reg = new RegExp("^[\\w-']+$");
            return reg.test(Str)
            
            
    }
      
    
   
   function ValidEmailString(Str)
    {
        //This is a Email validation
          
            var reg = new RegExp("^([\\w-.]+@[\\w-]+[\.]{1}[\\w]+[\.]{0,1}[\\w]+)$"); //RegExp("^[\\w-]+@[\\w-]+\\.(com|gh|net|org|edu|mil)$");
            return reg.test(Str)
            
    }
   
   
   function ValidEmailListString(Str)
    {
        //Email list sepeated by space
          
            var reg = new RegExp("^(([\\w-.]+@[\\w-]+[\.]{1}[\\w]+[\.]?[\\w]+)[\\s]*)*$"); 

            return reg.test(Str)
            
    }
      
   
   
    function ValidSentenceString(Str)
    {
        //This is a sentensce - word + spaces
          
            var reg = new RegExp("^([\\s]*[\\w]+[\\s]*)+$");
            return reg.test(Str)
            
    }
   
     
   
   function ValidWordString(Str)
    {
          //Number should be a word
          
            var reg = new RegExp("^[\\w]+$");
            return reg.test(Str)
            
    }
    
    
    function ValidNumericString(Str)
    {
          //This can be an interger or floating number
            var reg = new RegExp("^([\\d]+[\.]{0,1}[\\d]*)$");
            return reg.test(Str)
            
    } 
    
    function ValidIntegerString(Str)
    {
          //Number should be an interger
          
            var reg = new RegExp("^[\\d]+$");
            return reg.test(Str)
            
    } 
   
    function ValidMoneyString(Str)
    {
          //This can be an interger or floating number
            var reg = new RegExp("^([\\d]+[\.]{0,1}[\\d]{0,2})$");
            return reg.test(Str)
            
    } 
    
      //Check that alpha or characters
    function ValidAlphaString(Str)
    {
          
            var reg = new RegExp("^([a-zA-Z]+)$");
            
            return reg.test(Str)
            
    }
    
    //******************************************  
    //Check that alpha or characters
    function ValidAlphaNumString(Str)
    {
          
            var reg = new RegExp("^([a-z]|[A-Z])(([a-zA-Z0-9_]){0})");
            
            return reg.test(Str)
            
    }




function ValidUrlString(Str) {

    //("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
   // /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

	
    var reg = new RegExp("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
    
    return reg.test(Str)

	
}


    /*

    //Check that only alphabets are entered
    function ValidateAlphaString(Str)
    {
          
            var reg = new RegExp("^([a-z]|[A-Z])(([a-zA-Z_]){0})");
            return reg.test(Str)
            
    }
    
   function ValidateNumString(Str)
    {
          
            var reg = new RegExp("^(([0-9]){0})");
            return reg.test(Str)
            
    }
    
        //Check that alpha or characters
    function ValidateWordsString(Str)
    {
          
            var reg = new RegExp("^([a-z]|[A-Z])(([a-zA-Z0-9_]){0})");
            return reg.test(Str)
            
    }
  
  */
  
   //#####################################################################


//DRAG AND DROP
//http://www.webreference.com/programming/javascript/mk/column2/



//MOUSE  COORDINATE

//http://www.quirksmode.org/js/events_properties.html

//http://www.quirksmode.org/js/events_mouse.html

//document.onclick = SetMouseCordinates;

function SetMouseCordinates(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information

    getElementFromID("MouseX").value = posx
    getElementFromID("MouseY").value = posy


}






/*
document.onmousemove = mouseMove;

function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}



function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
	
		alert("x = " + posx + " y = " + posy)

}

*/


