//function to change the
//back color of the row containing checked checkbox
function highLight(checkBox)
{
	//if the checkbox is checked
	//change its backgroundcolor
	if(checkBox.checked == true)
	{
		checkBox.parentElement.parentElement.style.backgroundColor = '#F1F1F1';
		checkBox.parentElement.parentElement.style.color = 'black';
	}
	//else restore
	//previous state on uncheck
	else
	{
		checkBox.parentElement.parentElement.style.backgroundColor = '';
		checkBox.parentElement.parentElement.style.color = 'black';
	}
}

function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal)
{
	reg = new RegExp(':' + aspCheckBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		//check if the element is checkbox
		var	elm = document.forms[0].elements[i]
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//check or uncheck the check boxes 
				//depending upon the status of 
				//select All checkbox
				elm.checked = checkVal
				
				//change the back color
				//of all checkboxes rows
				highLight(elm);
			}
		}
	}
}

function CheckAllDataGridCheckBoxesAccpetDisabled(aspCheckBoxID, checkVal)
{
	reg = new RegExp(':' + aspCheckBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		//check if the element is checkbox
		var	elm = document.forms[0].elements[i]
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				if(elm.disabled != true)
				{
					//check or uncheck the check boxes 
					//depending upon the status of 
					//select All checkbox
					elm.checked = checkVal
					
					//change the back color
					//of all checkboxes rows
					highLight(elm);
				}
			}
		}
	}
}


// function to uncheck the Select Checkbox
// if any child checkbox is unchecked
function Disable(checkBoxID)
{
	var checkedCount = 0;
	var checkBoxesCount = 0;
	
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//a counter to count all the checkboxes in the checkbox column of repeater or datagrid
				checkBoxesCount = checkBoxesCount + 1;
				
				//check for checked checkboxes
				if(elm.checked == true)
				{
					//get count of checked checkboxes
					checkedCount = checkedCount + 1;
				}
			}
		}
	}
	
	//if the count of checked checkboxes
	//and total checkboxes match
	if(checkBoxesCount != checkedCount)
	{
		return true;
	}
	return false;
}

function ChkUnchkTopCheckBox(checkBoxID)
{
	var checkedCount = 0;
	var checkBoxesCount = 0;
	
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				if(elm.disabled != true)
				{
					//a counter to count all the checkboxes in the checkbox column of repeater or datagrid
					checkBoxesCount = checkBoxesCount + 1;
					
					//check for checked checkboxes
					if(elm.checked == true)
					{
						//get count of checked checkboxes
						checkedCount = checkedCount + 1;
					}
				}
			}
		}
	}
	
	//if the count of checked checkboxes
	//and total checkboxes match
	if(checkBoxesCount != checkedCount)
	{
		return true;
	}
	return false;
}



// function to check if 
// at least one check box is checked
function isChecked(checkBoxID)
{
	
	var checkedCount = 0;
	var checkBoxesCount = 0;
	
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//a counter to count all the checkboxes in the checkbox column of repeater or datagrid
				checkBoxesCount = checkBoxesCount + 1;
				
				//check for checked checkboxes
				if(elm.checked == true)
				{
					return true;
				}
			}
		}
	}
	return false;
}

function ChkAlreadyActiveInactive(checkBoxID,chkText)
{
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//check for checked checkboxes
				if(elm.checked == true)
				{
					if(elm.Text == chkText)
					{
						return true;
					}
				}
			}
		}
	}
	return false;
}


// function to check if 
// at least one Employee selected is already de-activated
function isDeactivated(checkBoxID)
{
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//check for checked checkboxes
				if(elm.checked == true)
				{
					if(elm.Text == '0')
					{
						return true;
					}
				}
			}
		}
	}
	return false;
}


// function to check if 
// at least one Employee selected is already de-activated
function isActivated(checkBoxID)
{
	reg = new RegExp(':' + checkBoxID + '$')
	
	//check all elements in the form
	for(i = 0; i < document.forms[0].elements.length; i++)
	{ 
		var	elm = document.forms[0].elements[i]
		
		//check if the element is checkbox
		if (elm.type == 'checkbox') 
		{ 
			//if the name of the checkbox matches
			if (reg.test(elm.name)) 
			{
				//check for checked checkboxes
				if(elm.checked == true)
				{
					if(elm.Text == '1')
					{
						return true;
					}
				}
			}
		}
	}
	return false;
}


//function to validate 
//page no for paging
function ValidatePages()
{

	
	
	var strError = '';
	var cnt = 0;
	var lblTot = document.getElementById('lblTotalPage');
	var lblCur = document.getElementById('lblCurrentPage');
	
	reg = new RegExp("^[0-9]+$");


	if(document.forms[0].txtPageNo.value == '' )
	{
		alert('Please enter the Page No.');
		//SetErrorLabel('lblError','Please enter the Page No.');
		cnt = cnt + 1;
	}
	else if(reg.test(document.forms[0].txtPageNo.value) == false)
	{
		alert('Page no. should be a positive integer value');
		//SetErrorLabel('lblError','Page no. should be a positive integer value');
		cnt = cnt + 1;
	}
	else if(parseInt(document.forms[0].txtPageNo.value) < 1)
	{
		alert('Page no. should be a positive value greater than or equal to 1');
		//SetErrorLabel('lblError','Page no. should be a positive value greater then or equal to 1');
		cnt = cnt + 1;
	}
	else if( parseInt(document.forms[0].txtPageNo.value) > parseInt(lblTotalPage.innerText))
	{
		alert('Page no. should be equal to or less than total pages');
		//SetErrorLabel('lblError','Page no. should be equal to or less then total pages');
		cnt = cnt + 1;
	}
	else if(parseInt(document.forms[0].txtPageNo.value) == parseInt(lblCur.innerText))
	{
		alert('Already at same page');
		//SetErrorLabel('lblError','Already at same page');
		cnt = cnt + 1;
	}
	if(cnt >=1)
	{
		document.forms[0].txtPageNo.focus();
		return false;
	}
	else
	{
		return true;
	}
}

//function to validate
//TextBox inside DataGrid
function ValidateTextBoxInDataGrid(elemDG, iTxt, iErrLbl, regularExp, option)
{
	//elemDG = document.forms[0].all.<DataGridID>.all
	var i, cnt = 0;
	var reg = new RegExp("^" + regularExp + "$");
	var txt, lbl;
	
	//############ option 1 for NewRequest.aspx ##################
	if(option == '1')
	{
		var iManLbl = 'lblDetailsRequired';
		var ctlID;
		
		for(i = 0; i < elemDG.length; i++)
		{
			//fetch '<DataGridID>__<CtlID>_' into <ctlID>
			ctlID = elemDG[i].id.substring(0,elemDG[i].id.lastIndexOf('_')+1);
			
			//if mandatory label is present
			if(elemDG[i].id.substring(elemDG[i].id.lastIndexOf('_')+1, elemDG[i].id.length) == iManLbl)
			{
				txt = document.getElementById(ctlID + iTxt);
				lbl = document.getElementById(ctlID + iErrLbl);
				
				if(reg.test(txt.value) == false)
				{
					cnt = cnt + 1;
					lbl.innerText = 'Details should contain 1 to 100 characters {a-z, A-Z, 0-9, spaces}';
				}
				else
				{
					lbl.innerText = '';
				}
			}
		}
	}
	//###############################################################
	
	//############ option 2 for ForceAssigned.aspx ##################
	else if(option == '2')
	{
		
		if(CheckInputInDataGrid(elemDG, iTxt, iErrLbl) == false)
		{
			lbl = document.getElementById('lblErrTop');
			lbl.innerText = 'Please enter at least one TextBox';
			cnt = cnt + 1;
		}
		else
		{
			lbl = document.getElementById('lblErrTop');
			lbl.innerText = '';
		}

		for(i = 0; i < elemDG.length; i++)
		{
			//fetch '<DataGridID>__<CtlID>_' into <ctlID>
			ctlID = elemDG[i].id.substring(0,elemDG[i].id.lastIndexOf('_')+1);

			txt = document.getElementById(ctlID + iTxt);
			lbl = document.getElementById(ctlID + iErrLbl);
			
			if(txt != null)
			{
				if(txt.value != "")
				{
					if(reg.test(txt.value) == false)
					{
						cnt = cnt + 1;
						lbl.innerText = 'Field should contain digits only {1-9}';
					}
					else
					{
						lbl.innerText = '';
					}
				}
				else
				{
					lbl.innerText = '';
				}
			}
		}
	}
	//###############################################################
	
	if(cnt >= 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}


//check if any TextBox inside DataGrid is not empty
function CheckInputInDataGrid(elemDG, iTxt, iErrLbl)
{
	var txt, lbl, i;
	
	for(i = 0; i < elemDG.length; i++)
	{
		//fetch '<DataGridID>__<CtlID>_' into <ctlID>
		ctlID = elemDG[i].id.substring(0,elemDG[i].id.lastIndexOf('_')+1);

		txt = document.getElementById(ctlID + iTxt);
		lbl = document.getElementById(ctlID + iErrLbl);
		
		if(txt != null)
		{
			if(txt.value != "")
			{
				return true;
			}
		}
	}
	return false;
}


//function to validate
//TextBox inside DataGrid
function ValidateDataListControls(elemDL, regularExp)
{
	//elemDG = document.forms[0].all.<DataGridID>.all
	var i, cnt = 0;
	var reg = new RegExp("^" + regularExp + "$");
	var cmb, txt, lbl;
	var strError;
	
	//############ for view-cart.aspx ##################
	var ctlID;
	
	for(i = 0; i < elemDL.length; i++)
	{
		strError = "";
		//fetch '<DataGridID>__<CtlID>_' into <ctlID>
		ctlID = elemDL[i].id.substring(0,elemDL[i].id.lastIndexOf('_')+1);
		
		//casting of cmbDuration
		cmb = document.getElementById(ctlID + "cmbDuration");
		
		//casting of txtPackages
		txt = document.getElementById(ctlID + "txtPackages");

		if(txt != null)
		{
			lbl = document.getElementById(ctlID + "lblErrPackages");
			if(reg.test(txt.value) == false)
			{
				cnt = cnt + 1;
				strError = 'Field should contain digits only {1-9}';
			}
			lbl.innerText = strError;
		}
		else if(cmb != null)
		{
			lbl = document.getElementById(ctlID + "lblErrDuration");
			
			if(cmb.selectedIndex == 0)
			{
				cnt = cnt + 1;
				strError = 'Duration is not selected';	
			}
			lbl.innerText = strError;
		}
	}
	
	if(cnt >= 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}


function UncheckRadionButtoninDataList(elemDL, itemIndex, radioButtonID)
{
	var ctlID, ctrl, index;
	for(i = 0; i < elemDL.length; i++)
	{
		//fetch '<DataGridID>__<CtlID>_' into <ctlID>
		ctlID = elemDL[i].id.substring(0, elemDL[i].id.lastIndexOf('_')+1);
		index = elemDL[i].id.substring(elemDL[i].id.lastIndexOf('_') - 1, elemDL[i].id.lastIndexOf('_'));

		if(elemDL[i].id == ctlID + radioButtonID && index != parseInt(itemIndex))
		{
			ctrl = document.getElementById(ctlID + radioButtonID);
			ctrl.checked = false;
		}
	}
}


function getDateFormat(dateValue)
{
	dateValue = dateValue + 1;
	if(dateValue <= 9)
	{
		return "0" + dateValue;
	}
	return dateValue;
}
		
function CompareDates(objFromDate, objToDate)
{
	var currDate, fromDate, toDate;
	
	if(objFromDate.value != "" && objToDate.value == "")
	{
		currDate = new Date();
		objToDate.value = getDateFormat(currDate.getMonth()) + "-" + currDate.getDate() + "-" + currDate.getFullYear();
		toDate = new Date(getDateFormat(currDate.getMonth()) + "-" + currDate.getDate() + "-" + currDate.getFullYear());
		fromDate = new Date(objFromDate.value);
		return DateCompareResult(toDate, fromDate)
	}
	else if(objFromDate.value != "" && objToDate.value != "")
	{
		toDate = new Date(objToDate.value);
		fromDate = new Date(objFromDate.value);
		return DateCompareResult(toDate, fromDate)
	}
	else
	{
		return true;
	}
}

function DateCompareResult(toDate, fromDate)
{
	if(toDate < fromDate)
	{
		alert("To date should be greater than from date");
		return false;
	}
	else
	{
		return true;
	}
}


function ValidateCartItems(objControlList, strErrorLabel, strErrorMsg)
{
	var i, cnt = 0;
	if(objControlList.length == 0)
	{
		return false;
	}
	else
	{
		for(i = 0; i < objControlList.length; i++)
		{
			if(objControlList[i].type == "checkbox")
			{
				if(objControlList[i].checked == true)
				{
					cnt = cnt + 1;
					//alert(objControlList[i].id);
					//return true;
				}
			}
		}
	}
	if(cnt >= 1)
	{
		return true;
	}
	else
	{
		SetErrorLabel(strErrorLabel, strErrorMsg);
		return false;
	}
}
