
//---------------------------------------------------------------------------------------------------------------------------------
//-- Popup "I want to Box" functions
//---------------------------------------------------------------------------------------------------------------------------------


function F_SelectRow (obj_Element)
{	
	var obj_Parent = JS_Utils_GetParent (  JS_Utils_GetParent ( obj_Element ) );

	if ( obj_Element.checked ) obj_Parent.className = "Highlighted";
	else obj_Parent.className = "";
}

function F_CheckRows ( str_Element )
{
	var obj_Element = document.getElementById ( str_Element );
	var ar_Inputs = obj_Element.getElementsByTagName ( "input" );

	for ( var i=0; i<ar_Inputs.length; i++ )
	{
		F_SelectRow ( ar_Inputs[i] );
	}	
}

function JS_Utils_SelectAll ( str_Element )
{
	var obj_Element = document.getElementById ( str_Element );
	var ar_Inputs = obj_Element.getElementsByTagName ( "input" );
	
	for ( var i=0; i<ar_Inputs.length; i++ )
	{
		ar_Inputs[i].checked = true;
		F_SelectRow ( ar_Inputs[i] );
	}	
}

function JS_Utils_DeselectAll ( str_Element )
{
	var obj_Element = document.getElementById ( str_Element );
	var ar_Inputs = obj_Element.getElementsByTagName ( "input" );
	
	for ( var i=0; i<ar_Inputs.length; i++ )
	{
		ar_Inputs[i].checked = false;
		F_SelectRow ( ar_Inputs[i] );
	}	
}


function JS_Utils_GetParent ( obj_Element )
{
	 if ( obj_Element.parentElement ) { return obj_Element.parentElement };
	 if ( obj_Element.parentNode ) { return obj_Element.parentNode };
	 if ( obj_Element.parent ) { return obj_Element.parent };
	 return;
}

function F_ShowHide ( obj_Element, b_Show )
{
	//alert ( "show hide" );
	//alert ( obj_Element );
	//alert ( b_Show );        
    
    if (typeof obj_Element == "string") 
    {
        obj_Element = document.getElementById(obj_Element);
    }
    

    if ( obj_Element )
	{
		if ( b_Show == null )
		{
			if ( obj_Element.style.display == "none" ) obj_Element.style.display = "block";
			else obj_Element.style.display = "none";
		}
		else
		{
			if ( b_Show ) obj_Element.style.display = "block";
			else obj_Element.style.display = "none";
		}
		
	}
	else alert ( "Couldn not find element: " + obj_Element );
}

//---------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------

//handle radio buttons in repeaters
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Popup Modal
//---------------------------------------------------------------------------------------------------------------------------------

function JS_Utils_HideSelects ()
{
	// hide selects if IE6
	bIsIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if ( bIsIE6 )
	{
		var arSelects = document.getElementsByTagName("select");
		for ( var n=0; n<arSelects.length; n++ ) arSelects[n].style.display = "none";
	}
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Folder Functions
//---------------------------------------------------------------------------------------------------------------------------------

function F_Folder_Toggle ( objLink )
{
	var objContainer = JS_Utils_GetParent ( JS_Utils_GetParent ( objLink ) );
	var arInnerDivs= objContainer.getElementsByTagName ( "div" );
	
	F_Folder_ShowHide ( arInnerDivs[0] );
}

function F_Folder_ShowHide ( objElement, b_Show )
{
	//alert ( objElement.className );
	
	if ( b_Show == null )
	{
		if ( objElement.className == "Hide" ) objElement.className = "Show";
		else objElement.className = "Hide";
	}
	else
	{
		if ( b_Show ) objElement.className = "Show";
		else objElement.className = "Hide";
	}
	
}
	
function F_Folder_Hide ()
{
	var arInnerDivs = document.getElementsByTagName ( "div" );
	for ( var i=0; i<arTables.length; i++ ) F_ShowHide ( arInnerDivs[i] );
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Copy to Clipboard
//---------------------------------------------------------------------------------------------------------------------------------

function JS_CopyToClipboard (objElement)
{
    if ( window.clipboardData )
    {
        var strData = document.getElementById(objElement);
        var ar_TDs = strData.getElementsByTagName("td");
        //user_pref("signed.applets.codebase_principal_support", true);
        
        if (ar_TDs.length > 0)
        {
            //Copy content
            clipboardData.setData("Text", strData.innerHTML);
            //Show a message
            alert("Data copied to clipboard");
        }
        else
        {
            //Show a message
            alert("Sorry, but the grid is empty");
        }
    }
    else
    {
        //Show a message
        alert("Sorry, your browser doesn't support this feature. Please select the items you want to copy and use CTRL+C")
    }
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- JS Date Validation
//---------------------------------------------------------------------------------------------------------------------------------

function IsValidDate ( strElementIDs )//, objEndDate)
{
    //Source: http://www.jsmadeeasy.com/javascripts/Forms/Date%20Validation/template.htm
    //dateStr
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    // Also separates date into month, day, and year variables
    
    var IDsArray = strElementIDs.split(",") //split IDs
    for (i=0; i<IDsArray.length; i++)
    {
        //alert(IDsArray[i]);
        //alert ( "string = "  + strElementID );
        var objElement = document.getElementById(IDsArray[i]);    
        //alert ( "element = " + objElement );
        var dateStr = objElement.value;
        //alert("date = " + dateStr);
    
        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
        // To require a 4 digit year entry, use this line instead:
        // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
        //Martin's: (0[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1})\/(0[1-9]{1}|1[1-2]{1})\/20[0-1]{1}[0-9]{1}    
        
        var matchArray = dateStr.match(datePat); // is the format ok?
        if (matchArray == null) {
            alert("Date is not in a valid format.")
            return false;
        }
        
        day = matchArray[1]; // parse date into variables
        month = matchArray[3];
        year = matchArray[4];
        
        if (month < 1 || month > 12) { // check month range
            alert("Month must be between 1 and 12.");
            return false;
        }
        
        if (day < 1 || day > 31) {
            alert("Day must be between 1 and 31.");
            return false;
        }
        
        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            alert("Month "+month+" doesn't have 31 days!")
            return false
        }
    
        if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day==29 && !isleap)) {
                alert("February " + year + " doesn't have " + day + " days!");
                return false;
            }
        }
    }
    
    return true;  // date is valid
}

//---------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------------------------
//-- Regular Expression String
//-- This is replaced by [Resources:Global, RegExString] value
//-- RegExString soure: http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5
//---------------------------------------------------------------------------------------------------------------------------------

function getRegExString ( )
{
    return "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Popup Options
//---------------------------------------------------------------------------------------------------------------------------------

var objCurrentPopupItem = null;
var bPopupIsVisible = false;

function JS_Utils_PopupOptions_Show ( strElement )
{
	JS_Utils_PopupOptions_Hide ();
	
	objCurrentPopupItem = document.getElementById(strElement);
	objCurrentPopupItem.style.display = "block";
	bPopupIsVisible = true;
}

function JS_Utils_PopupOptions_Hide ()
{
	if ( objCurrentPopupItem ) 
	{
		if ( bPopupIsVisible )
		{
			objCurrentPopupItem.style.display = "none";
			bPopupIsVisible = false;
		}
	}
}


function JS_Utils_FixHeight(obj) {
    if (navigator.userAgent.indexOf('Firefox') != -1 || navigator.userAgent.indexOf('Chrome') != -1) {
        obj.style.height = '78px';
        obj.style.height = (obj.scrollHeight + 2) + 'px';
    }
    else if (navigator.userAgent.indexOf('MSIE') != -1) {
        obj.style.height = (obj.scrollHeight + 2) + 'px';
        if ((obj.scrollHeight + 20) < 80) {
            obj.style.height = '80px';
        }
    }
}

function JS_Utils_HSStandards_SelectAll(str_Element) {
    var obj_Element = document.getElementById(str_Element);
    var ar_Inputs = obj_Element.getElementsByTagName("input");

    for (var i = 0; i < ar_Inputs.length; i++) {
        if (ar_Inputs[i].type == "checkbox") 
            ar_Inputs[i].checked = true;
    }
}

function JS_Utils_HSStandards_DeselectAll(str_Element) {
    var obj_Element = document.getElementById(str_Element);
    var ar_Inputs = obj_Element.getElementsByTagName("input");

    for (var i = 0; i < ar_Inputs.length; i++) {
        if (ar_Inputs[i].type == "checkbox")
            ar_Inputs[i].checked = false;
    }
}

// get substring from end for a num of chars
function JS_Utils_GetLast(input, num) {
    if (input != null && num > 0) {
        input = input.substring(input.length - num, input.length);
    }

    return input;
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Trim Functions
//---------------------------------------------------------------------------------------------------------------------------------

function JS_Utils_Trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function JS_Utils_LTrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}

function JS_Utils_RTrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- Generic Confirm Delete with Modal
//---------------------------------------------------------------------------------------------------------------------------------

function JS_Utils_ConfirmDelete ( strAction, strItem )
{	
	if ( strAction == undefined || strAction == "" ) strAction = "delete";
	if ( strItem == undefined || strItem == "" ) strItem = "item";
	
	if ( JS_Confirm ( "Are you sure you wish to " + strAction + " this " + strItem + "?"  ) )
	{
		JS_Modal_Toggle ();
		return true;
	}
	else return false;
}

//---------------------------------------------------------------------------------------------------------------------------------
//-- JS Number Validation
//---------------------------------------------------------------------------------------------------------------------------------

function IsValidPositiveNumber ( numberToValidate )
{
    //Source: http://www.java2s.com/Code/JavaScript/Form-Control/Validateanumber.htm
    // First trims leading zeros, then checks for the following:
    // - Checks number is >= 0
    // - Checks number is <=2,147,483,674 (int max)
    
    var inpVal = trimNumber (numberToValidate);
    inpVal = parseInt(inpVal);
    if (isNaN(inpVal)) {
        return false;
    }
    
    if((inpVal >= 0) == false)
    {
        return false;
    }
    
    if ((inpVal > 2147483674) == true)
    {
        return false;
    }
    var re = /^[0-9]+$/;
    if (re.test(numberToValidate) == false) {
        return false;
    }
        
    return true;  // number is valid
}

function trimNumber ( s ) 
{
    //Source: http://www.delphifaq.com/faq/f1059.shtml
    //Trim leading 0 (zero) from text input fields e.g. 00123 becomes 123 

    while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
    return s;
}


//---------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------
//This is how we do JavaScript confirms so that the modal doesn't stay there if you click Cancel
function JS_Confirm(message) {
    var doPostback = confirm(message)
	if (doPostback){
	    return true;
	}
	else{
	    showModal = false;
	    return false;
	}
}
