﻿// The Following function used to create an AJAX request
function CreateXmlReq(){
    var objXmlReq;
	try{
		objXmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			objXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(oc){
			objXmlReq = null;
		}
	}
	if(!objXmlReq && typeof XMLHttpRequest != "undefined"){
		objXmlReq = new XMLHttpRequest();
	}
	return objXmlReq;
}

// The Following function used to set the XML request
function setRequest(Params){
    var RanNum = Math.random();
    var UrlSplit = new Array();
    UrlSplit = Params.Url.split("?");    
    if(UrlSplit.length>1){
        Params.Url = Params.Url + "&Rnum=" + RanNum;        
    }else{
        Params.Url = Params.Url + "?Rnum=" + RanNum;
    }
    var RequestServer = CreateXmlReq();
    if(RequestServer){
        RequestServer.onreadystatechange = function(){
            if(RequestServer.readyState==4){
                if(RequestServer.status==200){
                    var retval = new retParams();
                    retval.Request = RequestServer;
                    retval.readyState = RequestServer.readyState;
                    retval.status = RequestServer.status;
                    retval.statusText = RequestServer.statusText;
                    retval.Url = Params.Url;                    
                    Params.CallBackFunction(retval);
                }else{
                    var retval = new retParams();
                    retval.Request = null;
                    retval.readyState = RequestServer.readyState;
                    retval.status = RequestServer.status;
                    retval.statusText = RequestServer.statusText;
                    retval.Url = Params.Url;                    
                    Params.CallBackFunction(retval);
                }
            }else{
                var retval = new retParams();
                    retval.Request = null;
                    retval.readyState = RequestServer.readyState;
                    retval.status = 0;
                    retval.statusText = "Loading";
                    retval.Url = Params.Url;
                    Params.CallBackFunction(retval);
            }
        }        
        RequestServer.open(Params.Method,Params.Url);
        var contlength = 0;
        if(Params.Parameters!=null){
            contlength = Params.Parameters.length;
        }
        RequestServer.setRequestHeader('Content-Length',contlength);
        RequestServer.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        RequestServer.send(Params.Parameters);
    }else{
        var retval = new retParams();
        retval.Request = null;
        retval.Url = Params.Url;
        retval.MessageId = 0;
        retval.Message = "No Ajax Support";
        Params.CallBackFunction(retval);
    }
}

function RequestAttr(){
    this.Url = "";
    this.Parameters = "";
    this.Method = "POST";
    this.CallBackFunction = null;
    return this;
}

function retParams(){
    this.Request = null;
    this.readyState = 0;
    this.status = 0;
    this.statusText = "";
    this.Url = "";    
    return this;
}

// The Following function used to Convert XML request like a datatable
function DataTable(xmlNodes){
    var Nodes = xmlNodes;    
    var row = new Array();
    for(i=0;i<Nodes.length;i++){        
        row[i] = new cols(Nodes[i]);
    }
    this.columncount = 0;
    this.columnName = null;
    if(Nodes.length>0){
        this.columncount = Nodes[0].childNodes.length;
        var colname = new Array();
        for(i=0;i<this.columncount;i++){
            colname[i] = Nodes[0].childNodes[i].tagName;
        }
        this.columnname = colname;
    }
    this.rowcount = Nodes.length;
    this.rows = row;
    return this;
}

function cols(row){      
    for(j=0;j<row.childNodes.length;j++){        
       var colname = row.childNodes[j].tagName;
       if(typeof colname!="undefined"){
          eval("this." + colname + "= '"+ row.getElementsByTagName(colname)[0].childNodes[0].nodeValue +"';");
       }        
    }
    this.columns = this;    
    return this;
}

// The Following function used for trim the Given input string
function trim(str){
    if(typeof str=="undefined"){
        return "";
    }
    var start = 0;
    var end = str.length;
    for(trimstart=0;trimstart<str.length;trimstart++){
        if(str.charAt(trimstart)!=" "){
            start = trimstart;
            break;
        }
    }
    for(trimend=str.length-1;trimend>0;trimend--){
        if(str.charAt(trimend)!=" "){
            end = trimend + 1;
            break;
        }
    }
    return str.substring(start,end);
}

// The Following function gets an element from the document
function GetElement(ElementId){
    return document.getElementById(ElementId);
}

// The Following Function is used to click a component when pressing a key inside other Component
function clickComponent(ComponentId, event, KeyCode){
    var e = window.event? window.event : event;
    var Key = new Number();    
    if(KeyCode){
       if(!isNaN(KeyCode)){
         if(parseFloat(KeyCode)>=0 && parseFloat(KeyCode)<=255){
            Key = parseFloat(KeyCode);
         }else{
            alert("invalid keycode...");
            return;
         }
       }
    }else{
        Key = 13;
    }
    var Component = GetElement(ComponentId);
    if(Component){
        var CurrKey = e.keyCode ? e.keyCode : e.which;        
        if(CurrKey==Key){
            Component.click();
            return false;
        }else{
            return true;
        }
    }
}

// The Following function shows and hides the given elements
function show(showObjId,hideObjId){
    var showObj = GetElement(showObjId);
    var hideObj = GetElement(hideObjId);
    if(showObj!=null && hideObj!=null){
        showObj.style.display = "block";
        hideObj.style.display = "none";
    }else{
        if(showObj==null && hideObj==null){
            alert("Error from show function : " + showObjId + " & " + hideObjId + " are not found");
        }else if(showObj==null){
            alert("Error from show function : " + showObjId + " is not found");
        }else if(hideObj==null){
            alert("Error from show function : " + hideObjId + " is not found");
        }
    }
}

// The Following function gets an element from the document
function GetElement(ElementId){		
    return document.getElementById(ElementId);
}

// The Following function gets the mouse position
function getMousePosition(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};
}

// The Following function gets the Left of an object
function getLeft(obj){
    return obj.offsetLeft + (obj.offsetParent ? getLeft(obj.offsetParent) : obj.x ? obj.x : 0);
}
function getLeftNonIE(obj)
{
    var curleft = 0;
    //obj = document.getElementById(objname)
			//alert(obj);
			if (obj.offsetParent) 
			{
				curleft = obj.offsetLeft
				while (obj = obj.offsetParent) 
				{
					curleft += obj.offsetLeft
				}
			}
			return(curleft);
}
// The Following function gets the Top of an object
function getTop(obj){
    return (obj.offsetParent ? obj.offsetTop + getTop(obj.offsetParent) : obj.y ? obj.y : 0);    
}
function getTopNonIE(obj)
{
    var curtop = 0;
    //obj = document.getElementById(objname)
			//alert(obj);
			if (obj.offsetParent) 
			{
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) 
				{
					curtop += obj.offsetTop
				}
			}
			return(curtop);
}
// The Following function gets the Right and width of an object
function getRight(obj){
	var leftpos = getLeft(obj);
	return {right:obj.offsetWidth + leftpos, width:obj.offsetWidth };
}
// The Following function gets the Bottom and height of an object
function getBottom(obj){
	var bottompos = getTop(obj);
	return {bottom:obj.offsetHeight + bottompos, height:obj.offsetHeight };	
}

// The Following function gets the Bounds of an object
function getBounds(obj){
	var LeftPos = getLeft(obj);
	var TopPos = getTop(obj);
	var RightPos = getRight(obj).right;
	var BottomPos = getBottom(obj).bottom;
	var Width = getRight(obj).width;
	var Height = getBottom(obj).height;
	return{left: LeftPos, top: TopPos, right: RightPos, bottom: BottomPos, width: Width, height: Height};
}

// The Following Function is used to Submit a Form
function submitForm(FormId,ranDivId){
    var Frm = GetElement(FormId);
    if(Frm){
        if(Frm.tagName.toUpperCase()=="FORM"){
            var ranDiv = GetElement(ranDivId);
            if(ranDiv){
                ranDiv.innerHTML = "<input type='hidden' id='PostedFromJS' name='PostedFromJS' value='True' />";            
            }
            //alert(Frm.tagName);
            Frm.submit();
        }
    }
}

// The following function sets the position of an object
function PlaceCompAt(CompId,x,y){
    var comp = GetElement(CompId);
    if(comp){
        var Bounds = getBounds(comp.parentNode);        
        comp.style.position = "relative";
        comp.style.left = (x) + "px";
        comp.style.top = (y) + "px";
    }else{
        alert(CompId + " is null or not an object.");
    }
}

// The Following Function used to open a popup window
function winopen(url){
    window.open(url,"tiles","left=200,top=100,width=480,height=371");
}

// The Following function is used to pre load an image
function Preload(imagePath){
    var PreloadedImage = new Image();
    PreloadedImage.src = imagePath;
}

var Width = 0; Height = 0; ran = 0; resizeRate = 6; //Resize rate per minute
var ObjId = "";
function SetWindowDimNCenter(BoundObjId){
    //alert("b");
    resizeRate = Math.round(60000/resizeRate);    
    ObjId = BoundObjId;    
    if(document.getElementById(ObjId)){
        var comp = document.getElementById(ObjId);
        if(comp){
            comp.style.position='absolute';
            comp.style.left='0px';
            comp.style.top='0px';
        }
        SetDifference();
        SetWidthHeight(BoundObjId);
        windowWidth = Width + diffWidth;
        windowHeight = Height + diffHeight;
        if (window.screen){
            window.moveTo((screen.availWidth - windowWidth) / 2,(screen.availHeight - windowHeight) / 2);
        }        
        keepWindowSize();
    }
}
var KeepWindowInt;
function keepWindowSize(){
    if(document.getElementById(ObjId)){
       if(ran!=0){    
        SetWidthHeight(ObjId);
        windowWidth = parseFloat(Width) + parseFloat(diffWidth);
        windowHeight = parseFloat(Height) + parseFloat(diffHeight);
       }
       ran++;
       //document.title = ran + " times runned....";   
       window.resizeTo(windowWidth,windowHeight);
       //window.defaultStatus = "Window is resized with " + windowWidth + "px Width and " + windowHeight + "px Height";
       clearInterval(KeepWindowInt);
       KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
   }else{
       clearInterval(KeepWindowInt);
   }   
}

var checkParentPresentOnce = 50; // milliseconds
function closeMeWhenParentClosed(){
    if(window.opener.closed){
        window.close();
    }
    //initCloseOnBlur();
    //initialized = true;
    setTimeout("closeMeWhenParentClosed();",checkParentPresentOnce);
}

//var initialized = false;
//function initCloseOnBlur(){
//    window.onblur = function(){
//        window.focus();
//    }
//}

function SetWidthHeight(ObjName){
    var Obj = document.getElementById(ObjName);
    if(Obj!=null){
        Width = Obj.offsetWidth;
        Height = Obj.offsetHeight;        
    }else{
        window.defaultStatus = ObjName + " was not found..";
    }
}

function SetDifference(){
    window.resizeTo(500,500);
    diffWidth = 500 - GetInnerWidth();
    diffHeight = 500 - GetInnerHeight();    
}

function closewindow(){
    window.close();
}

onerror = function(err,url,l){
    alert(err + " on line " + l);
  window.defaultStatus = err + " on line " + l;
  if(KeepWindowInt && ObjId){
    clearInterval(KeepWindowInt);
    KeepWindowInt = setTimeout("keepWindowSize();",resizeRate);
  }
  return true;
}

function GetInnerWidth(){
   var WinInnerWid = 630;
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerWid = window.innerWidth;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerWid = document.documentElement.clientWidth;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerWid = document.body.clientWidth;    
  }  
  return WinInnerWid;   
}

function GetInnerHeight(){
  var WinInnerHei = 460;  
  if( typeof( window.innerWidth ) == 'number' ) {    
    WinInnerHei = window.innerHeight;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
    WinInnerHei = document.documentElement.clientHeight;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
    WinInnerHei = document.body.clientHeight;    
  }  
  return WinInnerHei;
}