// infra compatibility with FF>3.6
document.getBoxObjectFor = function(elem) {
    return elem.getBoundingClientRect();
}
function ReinstateSession(username)
{
	$.ajax({
		type: "POST",
		url: "Authentication.asmx/ReinstateSession",
		data: "{username:'" + username + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		async: true,
		success: function(session_refreshed) {
			alert(session_refreshed);
		}
	});
}
//****************** For Buttons********************************************************************************************************************************************************
function GetWindowEvent()
{
    return window.event;
}
function SetWindowEvent(e)
{
    if (e!=null && !IsIE())
    {
        window.event=e;
    }
}
function CancelEvent(e)
{
    if(e==null)
    {
        e=GetWindowEvent();
    }
    if (IsIE())
    {
        e.cancelBubble=true;
        e.returnValue=false;     
    }
    else
    {
	    e.preventDefault();
        e.stopPropagation();
    }    
}
function disableButton(b)
{
    resetButton(b);
    var s=b.src;
    b.src=s.replace('f1','f3');
    b.disabled=true;
}
function enableButton(b)
{
    var s=b.src;
    b.src=s.replace('f3','f1');
    b.disabled=false;
}
function highlightButton(b)
{
    var s=b.src;
    b.src=s.replace('f1','f4');
    b.disabled=false;
}
function unHighlightButton(b)
{
    var s=b.src;
    b.src=s.replace('f4','f1');
    b.disabled=false;
}

function pressButton(b)
{ 
    resetButton(b); 
    b.src=b.src.replace('f1','f2'); 
    b.disabled=false;
}

function resetButton(b)
{
    b.src=b.src.replace('f2','f1');
    b.src=b.src.replace('f4','f1');
    b.src=b.src.replace('f3','f1'); 

    b.disabled=false;
}


function disableCurrentButton()
{
    b=event.srcElement;
    disableButton(b);
}
function enableCurrentButton()
{
    b=event.srcElement;
    enableButton(b);
}
function highlightCurrentButton(event)
{
    //firefox and explorer use different functions
    if (event.srcElement!=null)
    b=event.srcElement;
    else
    b=event.target;
    highlightButton(b);
}
function pressCurrentButton()
{
    //firefox and explorer use different functions
    if (event.srcElement!=null)
    b=event.srcElement;
    else
    b=event.target;

    pressButton(b);
}
function resetCurrentButton()
{
    //firefox and explorer use different functions
    if (event.srcElement!=null)
    b=event.srcElement;
    else
    b=event.target;

    resetButton(b);
}
function unHighlightCurrentButton(event)
{
    //firefox and explorer use different functions
    if (event.srcElement!=null)
    b=event.srcElement;
    else
    b=event.target;

    unHighlightButton(b);
}
//*************************************************************************************************************************************************************************************
// It checks if the window is a popup window
function IsPopupWindow()
{
	if (window.opener != null)// Is a popup if there is an opener
		return true;
	else
		return false;
}

//*************************************************************************************************************************************************************************************
 //It checks if a field has value
 function  isEmpty( inputStr)
 {
    if (inputStr=="" || inputStr==null)  return true;
    else return false;
 }
 
//*************************************************************************************************************************************************************************************
//it Checks if the agument is a valid number
function isNumber(inputStr)
{
	if (isNaN(inputStr)) return false;
	else return true;
}
 
//*************************************************************************************************************************************************************************************
//This function Compares two numbers. It returns 1 if the first is greater, 2 if the second is greater and 0 if the are equals. if a number is not valid it returns -1
function CompareNumbers(number1,number2)
{
	if (isNaN(number1) ||isNaN(number2) ) return -1;
	else 
	{
		if (number1>number2) return 1;
		else if (number1<number2) return 2;
		else return 0;
	}
	return -1;
}
 

//*************************************************************************************************************************************************************************************
//This function checks to see if a number falls within a specified range. It returns true if the answer is yes, false otherwise
//In case of error it returns false;
//if we don't want specify an upperBound or a lowerBound, we can give the value inf (infinite)
function NumberInRange(num,lowerBound,upperBound)
{
	//first Check to see if num is a Number
	if (isNaN(num)) return false;
	//case without lower bound
	if (lowerBound =="inf" )
	{
		//upperBound should be a number
		if (isNaN(upperBound)) return false;
		//check to see if the number is less then the upperBound
		if (num<=upperBound) return true;
		else return false;   
	}//end if for infinite lower bound

	//case without upperBound
	else if (upperBound =="inf" )
	{
		//lowerBound should be a number
		if (isNaN(lowerBound)) return false;
		//check to see if the number is greater then the lowerBound
		if (num>=lowerBound) return true;
		else return false;   
	}//end if for infinite upper bound
	else// both upper and lower bound are specified
	{
		//lowerBound should be a number
		if (isNaN(lowerBound)) return false;   
		//upperBound should be a number
		if (isNaN(upperBound)) return false;   
		if (num>=lowerBound && num<=upperBound ) return true;
		else return false;
	}
	return false;
}//end function NumberInRange


//************************************************************************************************************************************************************************************* 
//This function selects a specific record in a group of radio buttons. It selects the record with  value equals to the second argument
//The first argument is a reference to the checkbox group contol and the second the value of the record we want to be selected
function selectGroupRecord(groupComp,val)
{
	if (groupComp==null) return;
	for (var i=0;i<groupComp.length;i++)
	{
	   if (groupComp[i].value==val)
	   {
		  groupComp[i].checked=true;
		  break;
	   }
	}
}

function findRadioGroupValue(radioGroupRef)
{
	var radioComp=radioGroupRef;
	if (radioComp==null) return;
	for (var i=0;i<radioComp.length;i++)
	{
		if (radioComp[i].checked==true)
		{
			return radioComp[i].value;
		}
	}
	return null;
}
 


function findRadioGroupText(radioGroupRef)
{
	var radioComp=radioGroupRef;
	if (radioComp==null) return;
	for (var i=0;i<radioComp.length;i++)
	{
		if (radioComp[i].checked==true)
		{
			return radioComp[i].text;
		}
	}
	return null;
}

function findCheckBoxGroupValues(checkBoxGroupRef)
{
	var checkComp=checkBoxGroupRef;
	if (checkComp==null) return;
	var result= new Array();
	for (var i=0;i<checkComp.length;i++)
	{
		if (checkComp[i].checked==true)
		{
			result[result.length]=checkComp[i].value;
		}
	}
	return result;
}
 

/*
******************************************COMBO FUNCTIONS*********************************
*/
 
//*************************************************************************************************************************************************************************************  function selectAllComboValues(comboId)
function selectAllComboValues(comboId)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	for (var i=0;i<comboComp.options.length;i++)
	{
		comboComp.options[i].selected=true;
	}
}

//*************************************************************************************************************************************************************************************  function findComboValues(comboId)
function findComboValues(comboId)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	var result= new Array();
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].selected==true)
		{
			result[result.length]= new Array();
			result[result.length-1][0]=comboComp.options[i].value;
			result[result.length-1][1]=comboComp.options[i].text;     
		}
	}
	return result;
}

//*************************************************************************************************************************************************************************************  function findComboValues(comboId)
function GetAllComboValues(comboId)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	var result= new Array();
	for (var i=0;i<comboComp.options.length;i++)
	{
		result[result.length]= new Array();
		result[result.length-1][0]=comboComp.options[i].value;
		result[result.length-1][1]=comboComp.options[i].text;
	}
	return result;
}

 
//*************************************************************************************************************************************************************************************  function addRecordToCombo(comboId,value,text)
function addRecordToCombo(comboId,value,text) 
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	var result= new Array();
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].value==value)
		{
			comboComp.options[i]=null;
		}
	}  
	comboComp.options[comboComp.options.length]=new Option(text,value);
}
 
//*************************************************************************************************************************************************************************************  function removeRecordFromCombo(comboId,value)
function removeRecordFromCombo(comboId,value) 
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	var result= new Array();
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].value==value)
		{
			comboComp.options[i]=null;
		}
	}  
}
//************************************************************************************************************************************************************************************* 
//This function selects a specific record in a combo box. It selects the record with option value equals to the second argument
//The first argument is a reference to the cobo contol and the second the value of the record we want to be selected
function selectComboRecord(comboId,val)
{
	var comboComp=document.getElementById(comboId);
	//var comboComp=comboId;
	if (comboComp==null) return;
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].value==val)
		{
			comboComp.options[i].selected=true;
			break;
		}
	}
}
//*************************************************************************************************************************************************************************************
function ClearCombo(comboId)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	for (var i=0;i<comboComp.options.length;i++)
	{
		comboComp.options[i]=null;
	}
	comboComp.length=0;
}
 
//*************************************************************************************************************************************************************************************
//This function fills a combo with the contents of an array after having cleared it.
//The first argument is the Id of the combo and the second an array with n row and 2 columns. column 0 is for value and column 1 for text
function fillCombo(comboId,comboContents)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;

	//first clear it
	for (var i=0;i<comboComp.options.length;i++)
	{
		comboComp.options[i]=null;
	}
	comboComp.length=0;

	//Now fill it with the new contents
	for (var i=0;i<comboContents.length;i++)
	{
		comboComp.options[comboComp.options.length]= new Option(comboContents[i][1],comboContents[i][0]);
	}
}
 
/*
This function returns the value of the selected option of comboComponent
*/
function getComboValue(comboId)
{
	var selectComp=document.getElementById(comboId);
	if (selectComp.selectedIndex==-1)
		return null;
	var val=selectComp.options[selectComp.selectedIndex].value;  
	return val;
}
/*
This function returns the text of the selected option of comboComponent
*/
function getComboText(comboId)
{ 
	var selectComp=document.getElementById(comboId);
	if (selectComp.selectedIndex==-1)
		return null;
	var val=selectComp.options[selectComp.selectedIndex].text;  
	return val;
}
 
/*
This function return the text of an option of a combo which has a specific value
*/
function findComboRecord(comboId,value)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;

	//first clear it
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].value==value)
			return comboComp.options[i].text;
	}
	return null;
}
 
function findComboRecords(comboId,value)
{
	var comboComp=document.getElementById(comboId);
	if (comboComp==null) return;
	var result = new Array();
	//first clear it
	for (var i=0;i<comboComp.options.length;i++)
	{
		if (comboComp.options[i].value==value) 
			result[result.length]=comboComp.options[i].text;
	}
	return result;
}
 
 //We can pass as argument either a reference to the element or the id of the element directly
function getElementPosition(el) 
{ 
    var offsetTrail=null;
    if (el.id)
    {   
        offsetTrail = el;             
    }
    else
    {
        offsetTrail = document.getElementById(el); 
    }    
    var offsetLeft = 0; 
    var offsetTop = 0; 
    var offsetWidth=offsetTrail.offsetWidth;
    var offsetHeight=offsetTrail.offsetHeight;

    while (offsetTrail) 
    { 
        offsetLeft += offsetTrail.offsetLeft; 
        offsetTop += offsetTrail.offsetTop; 
        offsetTrail = offsetTrail.offsetParent; 
    } 

    if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") 
    { 
        offsetLeft += document.body.leftMargin; 
        offsetTop += document.body.topMargin; 
    } 
    return {left:offsetLeft, top:offsetTop,width:offsetWidth,height:offsetHeight}; 
} 


//BROWSER TYPE
var browserVersion="";
function IsIE()
{
    if (browserVersion=="")
    {
        browserVersion=navigator.userAgent;
    }
    return browserVersion.indexOf('MSIE')!=-1;
}
function IsMAC()
{
    if (browserVersion=="")
    {
        browserVersion=navigator.userAgent;
    }
    return browserVersion.indexOf('Macintosh')!=-1;
}
//alternative methods to highligh buttons

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 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];
    }
}

//*************************** POPUPS **************************************************************************************************************************************************
function navigateToURL(targetUrl, targetFrame)
{
	if(targetUrl == null || targetUrl.length == 0)
		return;
	var newUrl=targetUrl.toLowerCase();
	if(newUrl.indexOf("javascript") != -1)
	{
		eval(targetUrl);
	}
	else if(targetFrame != null && targetFrame!="")
	{
		if(document.getElementById(targetFrame) != null) 
		{
			document.getElementById(targetFrame).src = targetUrl;
		}
		else if(eval("parent.frames."+targetFrame) != null)
		{
			eval("parent.frames."+targetFrame+".location=\""+targetUrl+"\";");
		}
		else if(targetFrame == "_self" 
			|| targetFrame == "_parent"
			|| targetFrame == "_media"
			|| targetFrame == "_top"
			|| targetFrame == "_search")
		{
			window.open(targetUrl,targetFrame);
		}
		else if ( targetFrame == "_blank")
		{

			var width=800;
			var height=600;
			if (screen.width==1600)
				width=1280;
			else if (screen.width==1280)
				width=1024;					
			else if (screen.width==1024)
				width=800;
			if (screen.height==1200)
				height=1024;
			else if (screen.height==1024)
				height=768;					
			else if (screen.width==768)
				height=600;
			width=700;
			height=717;
			var xCenter = (screen.width - width) / 2;
			var yCenter = (screen.height - height) / 2; 
			
			window.open(targetUrl,targetFrame,'height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
		}
		else
		{
			window.open(targetUrl);
		}
	}
	else {
		try {
			location.href = targetUrl;
		}
		catch (x) {
		}
	}
}   
    //it shows a popup for the specified url ; window dimensions derive from the screen analysis
function ShowPopup(url)
{
    var width=800;
    var height=600;
    if (screen.width==1600)
    width=1280;
    else if (screen.width==1280)
        width=1024;     
    else if (screen.width==1024)
        width=800;
    if (screen.height==1200)
        height=1024;
    else if (screen.height==1024)
        height=768;
    else if (screen.width==768)
        height=600;
    var xCenter = (screen.width - width) / 2;
    var yCenter = (screen.height - height) / 2;  

	if (url.lastIndexOf('#') == url.length-1)
		url = url.substring(0,url.length-1);
		
    if (url.indexOf('?')!=-1)
        url+='&';
    else
        url+='?';

    url+='Height='+(height-10)+'&Width='+(width-10);
    var w = window.open(url,'_blank','height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
    AddPopupToArray(w);
    if (event!=null)
    {
        event.cancelBubble=true;
        event.returnValue=false;
    }
}
//it shows a popup for the specified url ; window is full screen and it is NOT resizable but it is scrollable (we don't want surprises!)
function ShowMaximizedPopup(url, ev)
{
    var width=screen.availWidth - 6;
    var height=screen.availHeight - 50;
    var xCenter = (screen.width - width) / 2;
    var yCenter = (screen.height - height) / 2;

	if (url.lastIndexOf('#') == url.length-1)
		url = url.substring(0,url.length-1);

    if (url.indexOf('?')!=-1)
		url+='&';
    else
		url+='?';

    url+='Height='+(height)+'&Width='+(width);
    var w = window.open(url,'_blank','resizable=no, scrollbars=yes, height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
    AddPopupToArray(w);
    if (ev!=null)
    {
        CancelEvent(ev);
    }
 
}
var popupList = new Array();
function AddPopupToArray(p)
{
	popupList[popupList.length]=p;
}
function ExistOpenPopups()
{
	for (var i=0;i<popupList.length;i++)
	{
		 if (!popupList[i].closed)
		 {
			return true;
		 }
	}
	return false;
}
//it shows a popup for the specified url ; window dimensions are explicitly defined
function ShowPredefinedPopup(url, width, height, ev)
{
    SetWindowEvent(ev);
    var xCenter = (screen.width - width) / 2;
    var yCenter = (screen.height - height) / 2;  
    var w=window.open(url,'_blank','resizable=no,scrollbars=no,height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
    AddPopupToArray(w);
    if (ev!=null)
    {
        CancelEvent(ev);
    }
}

//it shows a popup for the specified url ; window dimensions are explicitly defined and the window is resizable & scrollable
function ShowResizablePopup(url, width, height, ev)
{
    var xCenter = (screen.width - width) / 2;
    var yCenter = (screen.height - height) / 2;  
    var w = window.open(url,'_blank','resizable=yes,scrollbars=yes,height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
    AddPopupToArray(w);
    if (ev!=null)
    {
        CancelEvent(ev);
    }
}

//it shows a popup for the specified url ; window dimensions are explicitly defined and the window is resizable & scrollable
function ShowScrollablePopup(url, width, height, ev)
{
    var xCenter = (screen.width - width) / 2;
    var yCenter = (screen.height - height) / 2;  
    var w = window.open(url,'_blank','resizable=no,scrollbars=yes,height=' + height + ',left=' + xCenter+ ',top=' +yCenter +  ',width=' + width );
    AddPopupToArray(w);
    if (ev!=null)
    {
        CancelEvent(ev);
    }
}

var _iframeID;

function ShowFramePopup(caller, iframeid, url)
{
 var popup = document.getElementById(iframeid);
    
    if (popup.className == "visible")
    {
        popup.className = "hidden";
        return;
    }
 if (popup.src.length==0) popup.src = url;

    popup.className = "visible";
    _iframeID = iframeid;
}
function HideFramePopup() 
{
    var popup = document.getElementById(_iframeID);
    popup.className = "hidden";
}

// The double negation forces the Javascript to evaluate the return value as true or false.
// If it isn’t defined, the null value is converted to false. Any other value will return true.
function IsElementDefined(elementName)
{
    return (!(!(document.getElementById(elementName))))
}


// ************************************************************************************************************************************************************************************
// MOUSE-RELATED
// ************************************************************************************************************************************************************************************
// this function determines whether the event is the equivalent of the microsoft
// mouseleave or mouseenter events.
function isMouseLeaveOrEnter(e, handler) { 
    if (e.type != 'mouseout' && e.type != 'mouseover') 
        return false; 
        
    var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement; 
    
    while (reltg && reltg != handler) 
        reltg = reltg.parentNode; 
        
    return (reltg != handler); 
}

// ************************************************************************************************************************************************************************************
// VISUAL EFFECTS
// ************************************************************************************************************************************************************************************
// sets the opacity of the specified element to the specified level
function setOpacity(elm, level) {
    if ((elm == null) || (level < 0) || (level>100))
        return;
    elm.style.opacity = level/100;                         // FF
    elm.style.filter = 'alpha(opacity=' + level + ')';     // IE
}

// *************************************************************************************************************************************************************************************
// DIV-RELATED  
// *************************************************************************************************************************************************************************************

var cancelClickCheck=false;
var registeredElemenetIds = new Array();
var visibleDivs= new Array();

function CancelClickCheck()
{
    cancelClickCheck=true;
}

function RegisterElementForClickCheck(elementId)
{
    if (document.onclick==null)
    {
        document.onclick=check;
    }
    registeredElemenetIds[registeredElemenetIds.length]=elementId;
}
function check(e)
{
    if (cancelClickCheck)
    {
        cancelClickCheck=false;
        return;
    }
    for (var i=0;i<registeredElemenetIds.length;i++)
    {
        var target = (e && e.target) || (event && event.srcElement);
        var obj = document.getElementById(registeredElemenetIds[i]);
        if (checkParent(target,registeredElemenetIds[i]))
        {            
            HideDiv(registeredElemenetIds[i]);
        }
    }
    cancelClickCheck=false;
}
function checkParent(t,id)
{
    while(t.parentNode)
    {
        if(t==document.getElementById(id))
        {
            return false;
        }
        t=t.parentNode
    }
    return true;
}
//Shows div with id divId next to el (el can be either the element id or reference to the element)
function ShowDiv(divId, el, relativeTop, relativeLeft)
{
    //the function has a toggle behavior. If you click the button that triggers the Show Div action  and the div is already open we should close it
    if (el != null)
	{
		var pos=getElementPosition(el);    
		var top=pos.top+(relativeTop==null?0:relativeTop);
		var left=pos.left+(relativeLeft==null?0:relativeLeft);		
	}
	else
    {
		//Show in center of window (approximately)
		if (IsIE()) // IE
		{
			left=document.body.offsetWidth/2-100;
			top=document.body.offsetHeight/2-100;
		}
		else
		{
			left=window.innerWidth/2-100;
			top=window.innerHeight/2-100;
		}
    }

	// no point in hiding the divs if el is null,
	// which means that the div will be shown in the center of the window and will always have the same top and left values.
	if (el != null)
	{
		for (var i=0;i<visibleDivs.length;i++)
		{
			if (visibleDivs[i][0]==divId && visibleDivs[i][1]==top && visibleDivs[i][2]==left)
			{
				HideDiv(divId);
				return;
			}
		}
    }

    document.getElementById(divId).style.display='inline'; 
    document.getElementById(divId).style.top=top;    
    document.getElementById(divId).style.left=left;     
    var insertPos=visibleDivs.length;
   
    visibleDivs[insertPos]= new Array();
    visibleDivs[insertPos][0]= divId;
    visibleDivs[insertPos][1]= top;
    visibleDivs[insertPos][2]= left;

    CancelClickCheck();
}

function HideDiv(divId)
{
    document.getElementById(divId).style.display='none';
    for (var i=0;i<visibleDivs.length;i++)
    {
        if (visibleDivs[i][0]==divId)
        {
            visibleDivs[i][0]=null;
            visibleDivs[i][1]=null;
            visibleDivs[i][2]=null;
            return;
        }
    }
}

function IsDivVisible(divId)
{
    if (document.getElementById(divId).style.display=='none')
    {
        return false;
    }
    else
    {
        return true;
    }
}

function ToggleDiv(divId)
{
    var div = document.getElementById(divId) ;

    if (div.style.visibility=='hidden') {
        div.style.visibility='visible';
    }
    else {
        div.style.visibility='hidden';
    }
}

function SplitString(str,delim) {
	var result = new Array();
	var index=0;
	var token;
	while (str!=null&&str!='')
	{
		index=str.indexOf(delim);
		if (index==-1)
		{
			token=str;
			str=null;
		}
		else
		{ 
			token=str.substring(0,index);
			str=str.substring(index+delim.length);
		}
		result[result.length]=token;
	}
	return result;
}

function clearJSONSpecial(s) {
	while (s.indexOf('\'') > 0)
		s = s.replace('\'', '');
	return s;
}

//********************************************** Membership.ascx *************************************************************************************************************************
function saveGroupIDs() {
	var lst = document.getElementById(g_lstMemberOfGroups) ;
	var hdn = document.getElementById(g_hdnMemberOfGroupsIDs) ;
	hdn.value = '' ;
	for (i=0; i<lst.options.length; i++)
		hdn.value += lst.options[i].value.split('_')[0] + (i < lst.options.length-1 ? ',' : '') ;
}

// moves all the selected items of the lstMemberOfGroups to the lstNonMemberOfGroups listbox
function moveManyFromMembers() {
	var vals = findComboValues(g_lstMemberOfGroups) ;
	for (i=0; i<vals.length; i++) {
		addRecordToCombo(g_lstNonMemberOfGroups, vals[i][0], vals[i][1]);
		removeRecordFromCombo(g_lstMemberOfGroups, vals[i][0]);
	}

	saveGroupIDs() ;
}

// moves all the selected items of the lstNonMemberOfGroups to the lstMemberOfGroups listbox
function moveManyFromNonMembers() {
	var vals = findComboValues(g_lstNonMemberOfGroups) ;

	for (i=0; i<vals.length; i++) {
		var groupsize = parseInt(vals[i][0].split('_')[1]) ;
		var wasmember = parseInt(vals[i][0].split('_')[2]) ;
		addRecordToCombo(g_lstMemberOfGroups, vals[i][0], vals[i][1]);
		removeRecordFromCombo(g_lstNonMemberOfGroups, vals[i][0]);
	}

	saveGroupIDs() ;
}

// moves the single selected item of the lstMemberOfGroups to the lstNonMemberOfGroups listbox
// used for dblclick where only one item is left selected
function moveFromMembers() {
	var lst = document.getElementById(g_lstMemberOfGroups) ;
	if (lst.options.selectedIndex < 0)
		return ;

	var o = lst.options[lst.options.selectedIndex] ;
	addRecordToCombo(g_lstNonMemberOfGroups, o.value, o.text) ;
	removeRecordFromCombo(g_lstMemberOfGroups, o.value) ;

	saveGroupIDs() ;
}

// moves the single selected item of the lstNonMemberOfGroups to the lstMemberOfGroups listbox
// used for dblclick where only one item is left selected
function moveFromNonMembers() {
	var lst = document.getElementById(g_lstNonMemberOfGroups) ;
	if (lst.options.selectedIndex < 0)
		return ;
	var o = lst.options[lst.options.selectedIndex] ;

	addRecordToCombo(g_lstMemberOfGroups, o.value, o.text) ;
	removeRecordFromCombo(g_lstNonMemberOfGroups, o.value) ;

	saveGroupIDs() ;
}
//*********************** hrs.aspx *************************************************************************************************************************************************************
function SendCredentials()
{
	var uid = $('#'+g_hdnUserId).val();
	if (uid != undefined)
	{
		$.ajax({
			type: "POST",
			url: "hrs.aspx/SendCredentials",
			data: "{userId:'" + uid + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			async: true,
			success: function(result)
			{
				$('#'+g_lblError).show(); // TODO: Does not show.
				//$('#'+g_lblError).attr('style', 'display:visible');
				//$('#'+g_lblError).attr('visible', true);
				if (result == null || result == 0)
				{
					$('#'+g_lblError).text(SENDMSG_NOTSENT);
				}
				else
				{
					$('#'+g_lblError).text(SENDMSG_SUCCESS);
				}
			}
		});
	}
}
//**********************************************************************************************************************************************************************************************
