/*
Program Purpose
**********************************************************************
Used to validate form data,retrieve values, etc.
**********************************************************************

Bug Fix notes or code changes
**********************************************************************
Original Programmer: Jason R. Sebring, 1/3/2002
1)
programmer:
date:
lineNumber:
purpose:
**********************************************************************
*/

function isBlank(str) {
	var blankPattern = /^\s*$/;
	return blankPattern.test(str);
}

 // checks/unchecks all checkboxes in a checkBox group, from a given checkbox
function checkAll(checkControl, checkGroup) {
	var checkAll = checkControl.checked;
	if(checkGroup) {
		checkGroup.checked = checkAll;
	}
	for(var i = 0; i < checkGroup.length; i++) {
		checkGroup[i].checked = checkAll;
	}
}

// checks/unchecks all checkboxes on a form, from a given checkbox
function selectAllCheckBox(frm, checkControl) {
	var checkAll = checkControl.checked;
	for(var i = 0; i < frm.length; i++) {
		if(frm[i].type.toUpperCase() == 'CHECKBOX') {
			frm[i].checked = checkAll;
		}
	}
}

// checks/unchecks all checkboxes on a form, from a given checkbox
function selectSelectAllCheckBox(frm) {
	var checkAll = true;
	for(var i = 0; i < frm.length; i++) {
		if((frm[i].type == 'checkbox') && (frm[i].name != 'selectAll')) {
			if(!frm[i].checked) {
				checkAll = false;
				break;
			}
		}
	}
	if(frm.selectAll) {
		frm.selectAll.checked = checkAll;
	}
}

// uncheckes another checkbox based on if itself is unchecked
function unCheck(checkBoxThis, checkBoxAnother) {
	if(!checkBoxThis.checked) {
		checkBoxAnother.checked = false;
	}
}

// used to handle checkboxes where 'selectAll' exists
function handleCheckBoxes(frm, checkBox) {
	if(checkBox.name == 'selectAll') {
		selectAllCheckBox(frm,checkBox);
	} else if(checkBox.name.indexOf('All') != -1) {
		checkAll(checkBox, frm[checkBox.name.substring(0,(checkBox.name.length-3))]);
		checkGroupAll(frm, checkBox);
		selectSelectAllCheckBox(frm);
	} else {
		checkGroupAll(frm, checkBox);
		selectSelectAllCheckBox(frm);		
	}
}

// binds the onclick event to all the checkboxes of a given form
// used in conjuction with handleCheckboxes
function initCheckBoxes(frm) {
	for(var i = 0; i < frm.length; i++) {
		if(frm[i].type.toUpperCase() == 'CHECKBOX') {
			frm[i].onclick = function(){handleCheckBoxes(frm, this);};
		}
	}
}

// used to check all items in a checkbox group from a given checkBox
function checkGroupAll(frm,checkBox) {
	var allChecked = true;
	if(!checkBox.checked) {
		allChecked = false;
	}
	for(var i = 0; i < frm[checkBox.name].length; i++) {
		if(!frm[checkBox.name][i].checked) {
			allChecked = false;
			break;
		}
	}

	if(frm[checkBox.name + 'All']) {
		frm[checkBox.name + 'All'].checked = allChecked;
	}
}

// checks if the radio group has a checked item or not
function hasCheckedItem(radioGroup) {
	var checked = radioGroup.checked;
	for(var i = 0; i < radioGroup.length; i++) {
		checked = radioGroup[i].checked;
		if(checked) {
			break;
		}
	}
	return checked;
}

// retrieves the checked item within a radio group
function getCheckedItem(radioGroup) {
	var checkedItem = '';
	if(radioGroup.length)
	{
		for(var i = 0; i < radioGroup.length; i++) {
			if(radioGroup[i].checked) {
				checkedItem = radioGroup[i].value;
				break;
			}
		}
	} 
	else
	{
		checkedItem = radioGroup.value;
	}
	return checkedItem;
}

// retrieves the first selected value in a listbox
function getFirstSelectedValue(listBox) {
  
	var intLength,intIndex;
	var value;
    alert(listBox);
    if(listBox != null)
    {
		alert(listBox.length)
		if(listBox.length > 0)
		{
			
			intLength = listBox.length;
			for(intIndex = 0; intIndex < intLength; intIndex++)
			{
				if(listBox[intIndex].selected)
				{
					value = listBox[intIndex].value;
				}
			}
		}
		else
		{
			value = listBox.value;
		    
		}
	}
	return value;
}

// retrieves the first selected text value in a listbox
function getFirstSelectedText(listBox) {
	var intLength, intIndex;
	var text;
	if(listBox.length)
	{
		intLength = listBox.length;
		for(intIndex = 0; intIndex < intLength; intIndex++)
		{
			if(listBox[intIndex].selected)
			{
				text = listBox[intIndex].text;
			}
		}
	}
	else
	{
		text = listBox.text;
	}
	return text;
}

/*
	Checks if blank or just noisewords. If found, alerts and returns false.
*/
function ValidateTextboxForBlankOrNoiseWords(textbox, blankMessage, noiseWordMessage) 
{
	var success = true;
	var objStopWordPattern = new RegExp(strStopWordPatternLeftNav, 'gi');
	if(isBlank(textbox.value))
	{
		alert(blankMessage);
		success = false;
	} 
	else if(objStopWordPattern.test(textbox.value)) 
	{
		alert(noiseWordMessage);
		success = false;
	}
	
	if(success)
		return true;
	else 
	{
		textbox.focus();
		textbox.select();
		return false;
	}
}

 // Check list box selected values size. It should not exceed the configurable value.
    function CheckListboxSize(listBox,size,msg1,msg2) {
	    var intLength, intIndex,intselcount;
	    intselcount=0;	    
	   
	    intLength = listBox.length;
	    for(intIndex = 0; intIndex < intLength; intIndex++)
	    {
		    if(listBox[intIndex].selected==true)
		        intselcount++;			    
	    }
	       	
	    if (intselcount==0)
	    {
	        alert(msg1);
            return false;
	    }    
	    else if(intselcount>size)
        {
            alert(msg2);
            return false;
        }
        else
	        return true;
    	
    }


    //This function validates whether user had selected any item in the listbox control	    
    function ValidateListBoxCtrl(ListBox,Msg){
            if (ListBox.options.selectedIndex==-1)
            {
                alert(Msg);
                return false;
            }
            else
                return true;
    } 

    function ValidateTextControl(txtbox,defaultMsg,validationMsg) 
    	{
		var formElement = document.getElementById(txtbox);

		if( formElement.value == defaultMsg || isBlank(formElement.value) || (formElement.value == "") )
		{			
			alert(validationMsg);
			formElement.focus();				
			return false;
		} else {
			return true;
		}

	}			 

