// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function newImageLoader(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MoveItem(sourceID,desID,option,max){
	var source = document.getElementById(sourceID);
	var destination = document.getElementById(desID);
	if(option=='all') //move all
		while(source.length>0){
			newoption = new Option(source.options[0].text, source.options[0].value);
			if(!lstExistItem(destination,newoption.text))
			    destination.options[destination.length]=newoption;
			source.options[0]=null;
		}
	else//move selected ones
		for (i=0; i<source.length; i++)
			if(source.options[i].selected==true){
				newoption = new Option(source.options[i].text, source.options[i].value);
				if(max > 0){
				    if(destination.length < max){
				        destination.options[destination.length]=newoption;
				        source.options[i]=null;
				    }
				    else
				        break;
				}
				else {//the destination has no limit the number of items
				    if(!lstExistItem(destination,newoption.text))
				        destination.options[destination.length]=newoption;
				    source.options[i]=null;
				}
				i--;
			}
	Sort(destination);
}
function lstExistItem(destination,txtItem){

        var i=0;var exist=false;
		while(i<destination.length){
		    if(destination.options[i].text==txtItem){
		        exist=true;
		        break;
		    }
		    i++;
		}
		return exist;
}
function Sort(obj)
{
	var content = new Array();
	for (i=0;i<obj.length;i++) 
	    content.push(new Option(obj.options[i].text, obj.options[i].value));
	content.sort(SortByText);
	
	for(i=0;i<content.length;i++) 
	    obj.options[i]=content[i];
}

function SortByText(obj1, obj2)
{
	var a = obj1.text.toLowerCase();
	var b = obj2.text.toLowerCase();
	return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}

function setSelect(rowID,checked)
{		
	var	row = (rowID=='') ? null : document.getElementById(rowID);
	if(checked) css = "rowSelected";
	else css = "rowLine";
	if(row!=null)
	{
		if(!checked) row.childNodes[1].className="rowRowHeader"; else row.childNodes[1].className="rowSelected";
		for(k=1;k<row.childNodes.length;k++) row.childNodes[k].className=css;			
	}
}
function setSelectGridView(objID,containerID)
{
	var objContainer = document.getElementById(containerID);
	for(i=0;i<objContainer.rows.length;i++)
	{
		if(objContainer.rows[i].id!=objID) setSelect(objContainer.rows[i].id,false);
	}
	setSelect(objID,true);
}
function ShowWindow(pnlControlMinId, pnlControlMaxId, objId, pnlBackgroundId, pnlTitleId)
{
	var pnlBackground = document.getElementById(pnlBackgroundId);
	var pnlTitle = document.getElementById(pnlTitleId);

	pnlBackground.className = "boxContent";
	pnlTitle.className = "txtTitle";
	ShowHideObject(objId,true);
	ShowHideObject(pnlControlMinId,true);
	ShowHideObject(pnlControlMaxId,false);
}
function HideWindow(pnlControlMinId, pnlControlMaxId, objId, pnlBackgroundId, pnlTitleId)
{
	var pnlBackground = document.getElementById(pnlBackgroundId);
	var pnlTitle = document.getElementById(pnlTitleId);
	
	pnlBackground.className = "boxContentCollapse";
	pnlTitle.className = "txtTitleInvert";
	ShowHideObject(objId,false);		
	ShowHideObject(pnlControlMinId,false);
	ShowHideObject(pnlControlMaxId,true);

}
function ShowHideTooglePanel(id){
    try{
        var obj=document.getElementById(id);
        if (obj.style.display=='block') 
           obj.style.display='none'; 
        else obj.style.display='block'; 
    }catch(err){
        alert(err);
    }
    
    return false;
}
//this function is tested for working
function getSelectedValueRadioButtonListByClientID(clientid) {
 var list = document.getElementById(clientid); //Client ID of the radiolist 
 var inputs = list.getElementsByTagName("input"); 
 var selected; 
 for (var i = 0; i < inputs.length; i++) { 
      if (inputs[i].checked) { 
          selected = inputs[i]; 
          break; 
       } 
  } 
  if (selected) { 
       return selected.value;
   }
   return '-1';
}
function GetSelectedValueRadioList(nameRadioList) {
    var radioButtons = document.getElementsByName(nameRadioList);
    for (var x = 0; x < radioButtons.length; x++)
        if (radioButtons[x].checked) {
            return radioButtons[x].value;
        }
    if (radioButtons.length == 0) alert('There is no radio button found');
    return '';
}
function GetRadioButtonSelectedValue(nameRadioList) {
    var radio = document.getElementsByName(nameRadioList);
    var inputArr = radio.getElementsByTagName('input');
    var labelArr = radio.getElementsByTagName('label');
    for (var i = 0; i < inputArr.length; i++) {
        if (inputArr[i].checked) {
            return labelArr[i].innerText;
        }
    }
    return '';
}  
function ShowHideObject(objId, isShow)
{
	var obj = document.getElementById(objId);
	if(isShow) obj.className = "";
	else obj.className = "hide";
}
window.scrollTo = function() { } // refine to focus the validationsummary when submit failed
  function ShowHideContent(id1, show1,id2,show2)
      {
        var elem1 = document.getElementById(id1);
        var elem2 = document.getElementById(id2);
        if (elem1 && elem2) 
        {
          if (show1) 
          {
            elem1.style.display = 'block';
            elem1.style.visibility = 'visible';
          } 
          else
          {
            elem1.style.display = 'none';
            elem1.style.visibility = 'hidden';
          }
          if (show2) 
          {
            elem2.style.display = 'block';
            elem2.style.visibility = 'visible';
          } 
          else
          {
            elem2.style.display = 'none';
            elem2.style.visibility = 'hidden';
          }
        }
      }        
//this function will be remove. Please use the one in Common.js
function ChangeCheckAll(objSource,desTableID){
	var form = document.forms[0];
	for(i=0;i<form.length;i++)
		if(form[i].id.indexOf("chkRow")!=-1 && form[i].disabled==false) 
		{
			form[i].checked=objSource.checked;
			setSelect("row"+form[i].id,objSource.checked)
		}
}

function OpenWindow(theURL,winName,features,width,height) { //v2.0
	if (width=='' || height=='') position=''
	else position = "width=" + width + ",height=" + height + ",top=" + (screen.height-height)/2 + ",left=" + (screen.width-width)/2;
	if (position != '')
		features += (features=='') ? position : "," + position;
		
	if (features == '')
		window.open(theURL,winName);
	else 
		window.open(theURL,winName,features);
	
}

function trim(s)
{
	return rtrim(ltrim(s));
}

function ltrim(s)
{
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s)
{
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

}
function SwitchView(imgMinusPlusID,divPanelID,buttonRefreshUpdatePanelID) {
    //var ElementID=eid + '1';
    var divPanel = document.getElementById(divPanelID);
    var imgMinusPlus = document.getElementById(imgMinusPlusID);

    if (divPanel.style.display == "none") {
        divPanel.style.display = "block";
        imgMinusPlus.src = "Images/IconMinus.png";
        imgMinusPlus.alt = "Close node";
        __doPostBack(buttonRefreshUpdatePanelID, '');
        //Use UniqueID instead
    }
    else {
        divPanel.style.display = "none";
        imgMinusPlus.src = "Images/IconExpanding.png";
        imgMinusPlus.alt = "Expand node";
    }
    return false;
}
