﻿/*
* Hide/Show controls
*/
Utility = function (){}

Utility.prototype.display = function(listShow, listHide){
    var aShow = listShow.split(",");
    
    if(listHide){
        var aHide = listHide.split(",");
        for(var i=0; i<aHide.length; i++){
            document.getElementById(aHide[i].trim()).style.display="none";
        }
    }
    
    for(var i=0; i<aShow.length; i++){
        document.getElementById(aShow[i].trim()).style.display="";
    }
}

Utility.prototype.getSelectedValue = function(ddl){
    return ddl.options[ddl.selectedIndex].value; 
}

Utility.prototype.getValue=function(oControl){
    //if dropdown list
    if (oControl.options){
        if (oControl.options.length>0){
            //return selected value
            return this.getSelectedValue(oControl);
        }
        else{
            return null;
        }
    }
    //if input (does not work for checkbox. implement later)
    else if(oControl.tagName =="INPUT"){
        return oControl.value;
    }
    else{
        alert("could not find value of control" + oControl.id);
    }
}


Utility.prototype.getWindowSize = function(){
    var size = { height: 0, width: 0};
    
    if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        size.width = document.documentElement.clientWidth;
        size.height = document.documentElement.clientHeight;
    } 
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        size.width = document.body.clientWidth;
        size.height = document.body.clientHeight;
    }
    else if(typeof( window.innerWidth ) == 'number' ) {
        //Older browser/Non-IE
        size.width = window.innerWidth;
        size.height = window.innerHeight;
    } 
    
    return size;
}

/*
* Gets the elements posisiton in  the page
*/
Utility.prototype.getPageCoords = function (element) {
    var coords = { x: 0, y: 0};
    while (element) {
        coords.x += element.offsetLeft;
        coords.y += element.offsetTop;
        element = element.offsetParent;
    }
    return coords;
}

 Utility.prototype.setControlDisplay = function( i_searchResHideControlList, i_display)
 {
    if (  i_searchResHideControlList ){   
        var controlList = i_searchResHideControlList.split(',');
        for(var cIdx=0; cIdx<controlList.length; cIdx++){
            var object = $get(controlList[cIdx]);
            if (object){
                object.style.display=i_display;
            }
        }
    }
 }
 Utility.prototype.setControlVisibility = function( i_searchResHideControlList, i_visibility)
 {
    if (  i_searchResHideControlList ){   
        var controlList = i_searchResHideControlList.split(',');
        for(var cIdx=0; cIdx<controlList.length; cIdx++){
            var object = $get(controlList[cIdx]);
            if (object){
                object.style.visibility=i_visibility;
            }
        }
    }
 }
 
 Utility.prototype.getAbsoluteLeft = function(o){
    var oLeft = o.offsetLeft;
    while(o.offsetParent!=null) 
    {
	    oParent = o.offsetParent;
	    oLeft += oParent.offsetLeft;
	    o = oParent;
    }
    return oLeft;
 }

 Utility.prototype.getAbsoluteTop = function(o){
	var oTop = o.offsetTop;
	while(o.offsetParent!=null) 
	{
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
 }

Utility.prototype.getElementHeight = function(o){
    var elementHeight=null;
	if (o.style.pixelHeight) 
	{
		elementHeight = o.style.pixelHeight;
	} 
	else 
	{
		elementHeight = o.offsetHeight;
	}
	return parseInt(elementHeight,10);
}

Utility.prototype.getPageLanguage = function(){
    //name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]language=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if( results == null ){
        return "";
    }  
    else{
        return results[1];
    }
}


Utility.prototype.isImageCheckboxChecked = function(elementName, part){
    var image;
    if(part){ 
        image = eval(part).elm(elementName);
    }else{
        image = $get(elementName);
    }
    
    if(!image){return null;}
    /* Is image checked/on (true) or unchecked/off (false) */
    var file = image.src.substring(image.src.lastIndexOf("/")+1);
    if (file.indexOf("_on")>0){
        return true;
    }
    else {
        return false;
    }    
}

/*
* Make a <separator> separated list from an array of values
*/
Utility.prototype.arrayToList = function(arrList, separator){
    var list = "";
       
    if (arrList)
    {
        if(!separator)
            separator = ";";
    
        for (var i=0; i < arrList.length; i++) 
        {
            if ( list.length > 0) 
                list += separator;

            list += arrList[i];
        }
    }
    
    return list;
}

var Utils = new Utility();


/*
* DateTime helper
*/
DateTime = function(){
    this.separator = '/';
    this.format = 'dd/mm/yyyy';
}

DateTime.prototype.init = function (f, s){
    this.format = f;
    if (s) {
        this.separator = s;
    } else {
        if (f.indexOf(":") > -1)
            this.separator = ":";
        else if (f.indexOf(".") > -1)
            this.separator = ".";
        else if (f.indexOf("-") > -1)
            this.separator = "-";
        else if (f.indexOf("/") > -1)
            this.separator = "/";
        else if (f.indexOf("\\") > -1)
            this.separator = "\\";
    }    
}

DateTime.prototype.stringToDate = function (sDate){
    var result = null;
  
    var iY=1900, iM=1, iD=1;
    var aElements = this.format.split(this.separator);
    var aValues = sDate.split(this.separator);
  
    if(aElements.length == aValues.length){
        for(var i=0; i< aElements.length; i++){
            if(isNaN(aValues[i])) return(null);
            if     (aElements[i]=="yyyy") iY = parseInt(aValues[i],10);
            else if(aElements[i]=="yy") { iY = parseInt(aValues[i],10); iY += (iY < 50)? 2000 : 1900; }
            else if(aElements[i]=="m" || aElements[i]=="mm") iM = parseInt(aValues[i],10);
            else if(aElements[i]=="d" || aElements[i]=="dd") iD = parseInt(aValues[i],10);
        }
        
        if(! (isNaN(iY)||isNaN(iM)||isNaN(iD) || iM<1 || iM>12 || iD<1 || iD>31) ) {
            try {
                result = new Date(iY, iM-1, iD);
                
                // + test for day "overflow": Date(2007, 0, 32) will result 2007/01/01
                if (result.getMonth() != iM-1)
                    result = null;
            }
            catch(e) {
                result = null;
            }
        }
    }
  
    return result;
}

DateTime.prototype.isValidDate = function (sDate, bAllowEmpty, from, to){
    if (!sDate || sDate == "") {
        return bAllowEmpty;
    }
    
    var date = this.stringToDate(sDate);
    
    if (date == null)
        return false;
    
    if (from && date < from)
        return false;

    if (to && date > to)
        return false;
        
    return true;
}

