/* ************************************************************
Standard cliend side data validation and interface manipulation
function

Developer:  Philip Schmitz
Date Updated:  1/3/2009
************************************************************ */

/* ************************************************************
this function opens a pop up window with several parameters
************************************************************ */
function popUp(url,width,height){
	open(url,'new','top=50,left=50,width='+width+',height='+height+',scrollbars=no,resizable=no');
} 

/* ************************************************************
this function opens a pop up window with several parameters
************************************************************ */
function popUpNew(url,top,left,width,height,scrollbars,resizeable){
	open(url,'new','top='+top+',left='+left+',width='+width+',height='+height+',scrollbars='+scrollbars+',resizable='+resizeable);
} 

/* ************************************************************
this function opens a pop up window with several parameters
************************************************************ */
function openWin(url,wName,wAtr){
	open(url,wName,wAtr);
}

/* ************************************************************
this function opens a pop up window with several parameters
************************************************************ */
function openIt(theAdd,windowName){
	open(theAdd,windowName,'width=800,height=600,toolbar=yes,location=yes,directories=no,status=no,menubar=yes,scrollbars=yes,copyhistory=no,resizable=yes');
}


/* ************************************************************
this function opens a pop up window with no options and 
returns false for .net controls
************************************************************ */
function simple_netSafe_openWin(linkUrl) {

	openWin(linkUrl, '', '');

    return false;

}


/* ************************************************************
this function opens the host computers print dialoge for page
printing
************************************************************ */
function printThisPage(){
	window.print();  
}

/* ************************************************************
this function just returns nothing
************************************************************ */
function Go(){return}

/* ************************************************************
this function will toggle the visibility of an html element
and optional associated arrow image
************************************************************ */
function ss_showHideElementTrack(elemObj,arrowObj,arrowR,arrowD,listDel,trackObj){
    
	var retVal = ss_showHideElement(elemObj,arrowObj,arrowR,arrowD,listDel);   
    
    if(trackObj != undefined){
		
		trackObj.value = retVal;
        //alert(trackObj.value);
    }
    
    return retVal;
	
}

/* ************************************************************
this function will toggle the visibility of an html element
and optional associated arrow image
************************************************************ */
function ss_showHideElement(elemObj,arrowObj,arrowR,arrowD,listDel){
    var retVal = 0;
    var strList;
    var strArr;
    var objHolder;
    
    if(listDel != undefined){
        //array loop through indexes
        strList = elemObj.toString();
        strArr = strList.split(listDel);        
        for(var x=0;x<strArr.length;x++){
            //alert(document.getElementById(strArr[x]));
            objHolder = document.getElementById(strArr[x]);
            if(objHolder.style.display=='none'){
                objHolder.style.display='';
                retVal = 1;
                
                if(x == 0 && arrowObj != undefined && arrowD != undefined){
                    arrowObj.src=arrowD;
                }
            }
            else{
                objHolder.style.display='none';
                retVal = 0;
                
                if(x == 0 && arrowObj != undefined && arrowR != undefined){
                    arrowObj.src=arrowR;
                }
            }
        }
    }
    else{
        //no array just standard
        if(elemObj.style.display=='none'){
            elemObj.style.display='';
            retVal = 1;
            
            if(arrowObj != undefined && arrowD != undefined){
                arrowObj.src=arrowD;
            }
        }
        else{
            elemObj.style.display='none';
            retVal = 0;
            
            if(arrowObj != undefined && arrowR != undefined){
                arrowObj.src=arrowR;
            }
        }
    }
    
    return retVal;
}

/* ************************************************************
this function will toggle and objects visiblity
************************************************************ */
function showHideElemSimple(elemObj) {
    if (elemObj.style.display == 'none') {
        elemObj.style.display = '';
        retVal = 1;
    }
    else {
        elemObj.style.display = 'none';
        retVal = 0;
    }
}

/* ************************************************************
this function will change teh display setting of a particular
html object
************************************************************ */
function showHideObj(eObj, dMode) {
    eObj.style.display = dMode;
}

function toggleTreeNode(arrObj,triggerObj,triggerTextExpand,triggerTextContract,ref_trackOpenNodeIds) {

	//alert(arrObj.length);
	//alert(triggerObj);
	
	var retVal = '';
	
	if (arrObj.length > 0 && triggerObj != undefined) {            

		for (var q = 0, len = arrObj.length; q < len; ++q) {

			//alert('arrStr:'+arrObj[q][0].toString());
			//alert('triggerObj.id:'+triggerObj.id);

			var elemIdStr = arrObj[q][0].toString();
			var childrenIdArr = arrObj[q][1].toString().split('|');
			var anscestorIdArr = arrObj[q][2].toString().split('|');

			if (triggerObj.id == elemIdStr){
				
				//alert('trigger is: ' + triggerObj.id);				

				//check node status for toggles
				var currentNodeStatus = 'hidden';
				
				//check first child to check status
				if (childrenIdArr.length > 0){

					//get child object from name
					var thisFirstChildObj = document.getElementById(childrenIdArr[0]);
					
					if (thisFirstChildObj.style.display == 'none') {
						currentNodeStatus = 'hidden';
					}
					else {
						currentNodeStatus = 'visible';
					}
					
				}				

				//alert('curent state is: ' + currentNodeStatus);

				//we have the trigger, check state for next step
				if (currentNodeStatus == 'hidden') {
					//hidden - show

					retVal = 'visible';

					//set trigger lable
					if (triggerTextContract != undefined) {
						if (triggerTextContract.length > 0) {
							triggerObj.value = triggerTextContract;	
						}
					}

					//alert('show:' + childrenIdArr.length);
					//loop through direct children and show those only
					for (var r = 0, len2 = childrenIdArr.length; r < len2; ++r) {

						//get child object from name
						var thisChildObj = document.getElementById(childrenIdArr[r]);

						if (thisChildObj != undefined) {                            
															
							thisChildObj.style.display = '';                                

						} //thisChildObj != undefined
												

					} //for r

				}
				else{
					//visible - hide

					retVal = 'hidden';

					//set trigger lable
					if(triggerTextExpand != undefined){
						if (triggerTextExpand.length > 0) {
							triggerObj.value = triggerTextExpand;	
						}
					}

					//alert('hide:' + anscestorIdArr.length);
					//loop through all ancestors and hide everyone
					for (var r = 0, len2 = anscestorIdArr.length; r < len2; ++r) {

						//get child object from name
						var thisAnscestorObj = document.getElementById(anscestorIdArr[r]);

						if (thisAnscestorObj != undefined) {                            
												 
							thisAnscestorObj.style.display = 'none';

							//now loop through all other array elements to see if ancsestor is in other anscestors and check for those triggers to reset
							//------//------//------
							for (var qt = 0, lenQt = arrObj.length; qt < lenQt; ++qt) {

								//alert(arrObj[qt][2].toString());

								if (arrObj[qt][1].toString().indexOf(anscestorIdArr[r]) > -1 && arrObj[q][0] != arrObj[qt][0]) {

									//alert('find button:' + arrObj[qt][0]);

									var thisRelatedAnscestorTriggerObj = document.getElementById(arrObj[qt][0]);

									if (thisRelatedAnscestorTriggerObj != undefined) {

										//set trigger lable
										if (triggerTextExpand != undefined) {
											if (triggerTextExpand.length > 0) {
												thisRelatedAnscestorTriggerObj.value = triggerTextExpand;
											}
										}

									}                                        

								}

							} //for qt
							//------//------//------
							//now loop through all other array elements to see if ancsestor is in other anscestors and check for those triggers to reset                                

						} //thisAnscestorObj != undefined
												

					} //for r

				}

				
				//ss_showHideTreeNodeTableRow('ctl00_mainFormContentArea_Div1Container|ctl00_mainFormContentArea_Tablerow1|ctl00_mainFormContentArea_Tablerow3|ctl00_mainFormContentArea_Tablerow4', document.getElementById('ctl00_mainFormContentArea_Button1'), '+', '-', '|')
			
			}                

		} //for q

	} //arrObj.length > 0 && triggerObj != undefined

	return retVal;

}


function doesStringListContainValue(strList,strSearch,strDel){

    var retVal = false;

    if(strList.length > 0 && strSearch.length > 0)
    {

        var strDelUse = '|';
        if(strDelUse != undefined)
        {
        
            if(strDelUse.length > 0)
            {
        
                switch (strDelUse)
                {
                case '|':
                    strDelUse = '|';
                    break;
                case ',':
                    strDelUse = ',';
                    break;              
                default:
                    strDelUse = '|';
                }
        
            }     

        } // strDelUse != undefined

        var valSplitArr = strList.split(strDelUse);

        //alert('search: ' + strSearch + ' in ' + strList);

        for (var r = 0, len2 = valSplitArr.length; r < len2; ++r) 
        {

            if (valSplitArr[r] == strSearch)
            {
        
                retVal = true;

            }
												

	    } //for r


    } // strList.length > 0 && strSearch.length > 0

    
    return retVal;

}

/* ************************************************************
NAME: getCookie
PARAMETERS
--c_name: cookie name string
RETURNS: cookie value as string
************************************************************ */
function getCookie(c_name){
	
	var i,x,y;
	var ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
  	x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  	y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  	x = x.replace(/^\s+|\s+$/g,"");
  
  	if (x==c_name)
    	return unescape(y);

	}
}

/* ************************************************************
NAME: setCookie
PARAMETERS
--c_name: cookie name string
--value: cookie value as string
--exdays: expiration days as integer
RETURNS: cookie value as string
************************************************************ */
function setCookie(c_name,value,exdays)
{
	
var exdate=new Date();

exdate.setDate(exdate.getDate() + exdays);

var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

document.cookie=c_name + "=" + c_value;

}

/* ************************************************************
NAME: showHidePageElement
PARAMETERS
--in_elemObj: html element object
--in_showObj: 1 = show, 0 = hide
RETURNS: 0,1 for show/hide state
************************************************************ */
function showHidePageElement(in_elemObj,in_showObj){
		
	var showAttribute = 'inline-block';	
	var hideAttribute = 'none';
		
	if(in_elemObj != undefined){
		
		if(in_showObj == 1){
			in_elemObj.style.display = showAttribute;				
		}
		else{
			in_elemObj.style.display = hideAttribute;	
		}
			
	}
	
	return in_showObj;
		
}

/* ************************************************************
NAME: toggleMcLeftSubNav
PARAMETERS
--in_elemObj: html element object
RETURNS: false
************************************************************ */
function toggleMcLeftSubNav(in_elemObj,in_rightArrowObj){
	
	if(in_elemObj != undefined){
	
		if(in_elemObj.style.display == 'none'){
				
			//toggle on
			showHidePageElement(in_elemObj,1);
			
			//toggle arrow visibility
			showHidePageElement(in_rightArrowObj,0)
			
			//set cookie
    		setCookie("bis_mc_lstn_visible", "1", 30);
			
		}	
		else{
		
			//toggle off
			showHidePageElement(in_elemObj,0);
			
			//toggle arrow visibility
			showHidePageElement(in_rightArrowObj,1)
			
			//set cookie
    		setCookie("bis_mc_lstn_visible", "0", 30);
			
		}
		
	}
	
	return false;
			
}

/*
** Returns the caret (cursor) position of the specified text field.
** Return value range is 0-oField.length.
*/
function doGetCaretPosition(oField) {

	// Initialize
	var iCaretPos = 0;

	// IE Support
	if (document.selection) {

		// Set focus on the element
		oField.focus();

		// To get cursor position, get empty selection range
		var oSel = document.selection.createRange();

		// Move selection start to 0 position
		oSel.moveStart('character', -oField.value.length);

		// The caret position is selection length
		iCaretPos = oSel.text.length;
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0')
		iCaretPos = oField.selectionStart;

	// Return results
	return (iCaretPos);
}


/*
** Sets the caret (cursor) position of the specified text field.
** Valid positions are 0-oField.length.
*/
function doSetCaretPosition(oField, iCaretPos) {

	// IE Support
	if (document.selection) {

		// Set focus on the element
		oField.focus();

		// Create empty selection range
		var oSel = document.selection.createRange();

		// Move selection start and end to 0 position
		oSel.moveStart('character', -oField.value.length);

		// Move selection start and end to desired position
		oSel.moveStart('character', iCaretPos);
		oSel.moveEnd('character', 0);
		oSel.select();
	}

	// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0') {
		oField.selectionStart = iCaretPos;
		oField.selectionEnd = iCaretPos;
		oField.focus();
	}
}

/*
** Function is used to replace HTML escaped ampersands within a URL
*/
function replaceUrlEncodedAmpersandInUrls(rawContent) {
	
	var newContent = rawContent;

	var re = new RegExp("(<A[^<>]*?href\\s*=\\s*['\"])(.*)([^'\"]*?['\"][^>]*?>)", "ig");

	newContent = newContent.replace(re,
		function ($0, $1, $2, $3) {
			re = /&amp;/gi
			return $1 + $2.replace(re, "&") + $3;
		}
	);	

	return newContent;
	
}

/*
** Function is used to replace HTML escaped CharityEngine token characters (^^)
*/
function replaceCharityEngineEscapeCharacters(rawContent) {
	
	var newContent = rawContent;

	newContent = newContent.replace(/%5E%5E/g, "^^");

	return newContent;
	
}

/*
** Function is used to intercept rad combobox AJAX server calls and set data values to send to server
*/
//event for combo box autocomplete
function RadComboBox_OnClientItemsRequesting(sender, eventArgs) {
	var context = eventArgs.get_context();
	context["FilterString"] = eventArgs.get_text();
}
//event for combo box autocomplete (include ID parameter)
function RadComboBox_OnClientItemsRequesting_IncludeID(sender, eventArgs) {
	var context = eventArgs.get_context();
	context["FilterString"] = eventArgs.get_text();

	context["option_include_id"] = "1";
}
//event for combo box autocomplete (exclude ID parameter)
function RadComboBox_OnClientItemsRequesting_ExcludeID(sender, eventArgs) {
	var context = eventArgs.get_context();
	context["FilterString"] = eventArgs.get_text();

	context["option_include_id"] = "0";
}

/*
** Function is used to replace client submitted content dangerous string patterns
*/
function mc_ui_replaceSqlInjectionPatternsWithSafeContent(rawContent) {
	
	var newContent = rawContent;

	newContent = newContent.replace(/;set/gi, "; set");
	newContent = newContent.replace(/;delete/gi, "; delete");	
	
	return newContent;
	
}

/*
** Function is used to loop thorugh an array of client input elements and pass them to the strip sql injection function
*/

function mc_ui_replaceSqlInjectionPatternsBeforeSubmit(in_objectArray) {

	if(in_objectArray.length > 0){
		
		for (var i = 0; i < in_objectArray.length; i++) {
			if(in_objectArray[i] != undefined)
				in_objectArray[i].value = mc_ui_replaceSqlInjectionPatternsWithSafeContent(in_objectArray[i].value);			
		}	
		
	}	
	
	return true;
	
}
