/*********************************************************************
'	File name			UserControlScripts.js
'	Description			This is the script file used to execute some 
'						common functions for all user controls
'
'   Modification Log
'   Date				Author			    Remarks     
' 	30-Dec-2006		  Devender Mallya		Initial version
' 	19-Jan-2007		  Devender Mallya		Added validation functions 
'											Changed functions for validations (Ver 1.1)
'   26-Apr-2007       Mervyn C Fernandes    Added validation function for 
'                                           registery (ver 2.0)
'	26-Apr-2007		  Nilesh Lokhande		Modified function to include Employee email (Ver 3.0)
'   11-Feb-2008		  Renuka Bedre			Modified function to display selected gender in 
'											in saved or printed form (Ver 3.1)
'   26-Feb-2008		  Renuka Bedre			Added function for maxlength check (Ver 3.2)
*********************************************************************/

/*********************************************************************
'	Function Name		fnFocus				
'
'	Description			This function is used to set the focus
'						on a particular control
'
'	Modification Log
'   Date                Author				Modification 
' 	30-Dec-2006		  Devender Mallya		Initial version
'*********************************************************************/
function fnFocus(sTextBoxId)
{
	if (document.getElementById(sTextBoxId).disabled == false)
		document.getElementById(sTextBoxId).focus();
	return true;
}

/*********************************************************************
'	Function Name		fnGetHTML				
'
'	Description			This function is used to set the html content
'						of the user control and add it to a hidden field.
'
'	Modification Log
'   Date                Author				Modification 
' 	04-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnGetHTML(ucName)
{
	var hdnHTML;
		
	fnSetLabels(ucName);

	hdnHTML = document.getElementById(ucName+"hdnHTML");
			
	var txtRegExp = new RegExp(/(<INPUT[^>]*\s*[^>]*>)/i);
	var txtHTML = document.getElementById(ucName+"spnUserControl").innerHTML;
	
	while (txtRegExp.test(txtHTML))
		txtHTML = txtHTML.replace(RegExp.$1,'');
		
	var txtDivision = document.getElementById("spnRdoDivision").innerHTML;
	while (txtRegExp.test(txtDivision))
		txtDivision = txtDivision.replace(RegExp.$1,'');
		
	txtHTML = txtHTML.replace(txtDivision,'');
	
	//** Begin Mod v1.1 **//	
	txtDivision = document.getElementById("tblValSummary").innerHTML;
	while (txtRegExp.test(txtDivision))
		txtDivision = txtDivision.replace(RegExp.$1,'');
		
	txtHTML = txtHTML.replace(txtDivision,'');
			
	var txtFont = '<font color="Red">*</font>';
	while (txtHTML.indexOf(txtFont) > -1)
		txtHTML = txtHTML.replace(txtFont,'');
	//** End Mod v1.1 **//
	
	hdnHTML.value = txtHTML;
	return true;
}

/*********************************************************************
'	Function Name		fnPrintYouthForm				
'
'	Description			This function is used print the Youth Program
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version (rendered obsolete)
'*********************************************************************/
//** Begin Mod v1.1 **//
/*function fnPrintYouthForm(ucName)
{
	var frmYouthProgram = document.forms["frmYouthProgram_PC"];
		
	if (frmYouthProgram.all[ucName+"valDOB"] != null)
		ValidatorEnable(frmYouthProgram.all[ucName+"valDOB"],true);
	
	if (frmYouthProgram.all[ucName+"valHeight"] != null)
		ValidatorEnable(frmYouthProgram.all[ucName+"valHeight"],true);
	
	if (Page_IsValid == true)
		fnSetLabels(ucName);	
		
	return true;
}*/
//** End Mod v1.1 **//

/*********************************************************************
'	Function Name		fnSetLabels				
'
'	Description			This function is used to set the hidden labels
'						of the Youth program with entered values
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version
'	26-Apr-2007		  Nilesh Lokhande		Modified function to include Employee email (Ver 3.0)
'   11-Feb-2008		  Renuka Bedre			Modified function to display selected gender in 
'											in saved or printed form (Ver 3.1)
'*********************************************************************/
function fnSetLabels(ucName)
{
	var txtTemp;
	
	txtTemp = document.getElementById(ucName+"txtChildName").value;
	document.getElementById("spnChildNamePrint").innerHTML = "&nbsp;" + txtTemp;


	//Begin Ver 3.1
	if (document.getElementById(ucName+"rdoDivision_0").checked)
	{
		txtTemp = "[ X ]&nbsp;" + document.getElementById(ucName+"rdoDivision_0").value + 
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;&nbsp;&nbsp;]&nbsp;" + 
					document.getElementById(ucName+"rdoDivision_1").value;
	}
	else if (document.getElementById(ucName+"rdoDivision_1").checked)
	{	
		txtTemp = "[&nbsp;&nbsp;&nbsp;]&nbsp;" + document.getElementById(ucName+"rdoDivision_0").value + 
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ X ]&nbsp;" + 
					document.getElementById(ucName+"rdoDivision_1").value;
	}
	else
	{	
		txtTemp = "[&nbsp;&nbsp;&nbsp;]&nbsp;" + document.getElementById(ucName+"rdoDivision_0").value + 
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;&nbsp;&nbsp;]&nbsp;" + 
					document.getElementById(ucName+"rdoDivision_1").value;
	}

	document.getElementById("spnDivisionPrint").innerHTML = "&nbsp;" + txtTemp;
	//End Ver 3.1
	
	txtTemp = document.getElementById(ucName+"txtDOBMonth").value;
	if (fnTrim(txtTemp) != "")
	{ 
		txtTemp+= "-" + document.getElementById(ucName+"txtDOBDay").value;
		txtTemp+= "-" + document.getElementById(ucName+"txtDOBYear").value;
		document.getElementById("spnDOBPrint").innerHTML = "&nbsp;" + txtTemp + 
			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	else
	{
		txtTemp = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + 
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		document.getElementById("spnDOBPrint").innerHTML = txtTemp;
	}

	txtTemp = document.getElementById(ucName+"txtAge").value;
	document.getElementById("spnAgePrint").innerHTML = "&nbsp;" + txtTemp;

	txtTemp = document.getElementById(ucName+"txtHeightFeet").value;
	if (fnTrim(txtTemp) != "")
	{
		txtTemp+= "'" + document.getElementById(ucName+"txtHeightInch").value;
		txtTemp+= "''";
	}
	document.getElementById("spnHeightPrint").innerHTML = "&nbsp;" + txtTemp;

	txtTemp = document.getElementById(ucName+"txtWeight").value;
	if (fnTrim(txtTemp) != "")
		document.getElementById("spnWeightPrint").innerHTML = "&nbsp;" + txtTemp;
	else
	{
		txtTemp = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		document.getElementById("spnWeightPrint").innerHTML = txtTemp;
	}
	
	txtTemp = document.getElementById(ucName+"txtEmployeeName").value;
	document.getElementById("spnEmployeeNamePrint").innerHTML = "&nbsp;" + txtTemp;

	txtTemp = document.getElementById(ucName+"txtEmployeePhone").value;
	document.getElementById("spnEmployeePhonePrint").innerHTML = "&nbsp;" + txtTemp;
	
	//Begin Ver 3.0
	txtTemp = document.getElementById(ucName+"txtEmployeeEmail").value;
	document.getElementById("spnEmployeeEmailPrint").innerHTML = "&nbsp;" + txtTemp;
	//End Ver 3.0

	txtTemp = document.getElementById(ucName+"txtYearLeague").value;
	if (fnTrim(txtTemp) != "")
		document.getElementById("spnYearLeaguePrint").innerHTML = "&nbsp;" + txtTemp;
	else 
	{	
		txtTemp = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		document.getElementById("spnYearLeaguePrint").innerHTML = txtTemp;
	}
	
	txtTemp = document.getElementById(ucName+"txtLeague").value;
	document.getElementById("spnLeaguePrint").innerHTML = "&nbsp;" + txtTemp;
		
	txtTemp = document.getElementById(ucName+"txtTeam").value;
	document.getElementById("spnTeamPrint").innerHTML = "&nbsp;" + txtTemp;
	
	txtTemp = document.getElementById(ucName+"txtAccidentNotify").value;
	document.getElementById("spnAccidentNotifyPrint").innerHTML = "&nbsp;" + txtTemp;
	
	txtTemp = document.getElementById(ucName+"txtAccidentPhone").value;
	document.getElementById("spnAccidentPhonePrint").innerHTML = "&nbsp;" + txtTemp;
}

/*********************************************************************
'	Function Name		fnValidatePlayers				
'
'	Description			This function is the validation function
'						for entering atleast one player
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnValidatePlayers(source, arguments)
{
	var bValid = false;
	var sCtlName;
	var sCtlValue;
	var dgPlayers = document.getElementById('Roster_UA1_dgPlayers');
	
	for (var i=2;i<=dgPlayers.rows.length;i++)
	{
		sCtlName = 'Roster_UA1_dgPlayers__ctl' + i + '_txtPlayerName';
		sCtlValue = fnTrim(document.getElementById(sCtlName).value);
		if (sCtlValue != '')
		{
			bValid = true;
			break;
		}
	}	
	if (bValid == true)
		arguments.IsValid = true;
	else
	{
		sCtlName = 'Roster_UA1_dgPlayers__ctl2_txtPlayerName';
		fnFocus(sCtlName);
		arguments.IsValid = false;
	}	
	return true;
}

/*********************************************************************
'	Function Name		fnValidatePlayers				
'
'	Description			This function is the validation function
'						for entering atleast one player
'
'	Modification Log
'   Date                Author				Modification 
' 	12-Jan-2009		  Devender Mallya		Initial version
'*********************************************************************/
function fnValidateTournamentPlayers(source, arguments)
{
	var bValid = false;
	var sCtlName;
	var sCtlValue;
	var dgPlayers = document.getElementById('Tournament_UA1_dgPlayers');
	
	for (var i=2;i<=dgPlayers.rows.length;i++)
	{
		sCtlName = 'Tournament_UA1_dgPlayers__ctl' + i + '_txtPlayerName';
		sCtlValue = fnTrim(document.getElementById(sCtlName).value);
		if (sCtlValue != '')
		{
			bValid = true;
			break;
		}
	}	
	if (bValid == true)
		arguments.IsValid = true;
	else
	{
		sCtlName = 'Tournament_UA1_dgPlayers__ctl2_txtPlayerName';
		fnFocus(sCtlName);
		arguments.IsValid = false;
	}	
	return true;
}

/*********************************************************************
'	Function Name		fnPrintPage				
'
'	Description			This function is used print a page
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnPrintPage()
{	
	window.print();	
	return false;
}

/*********************************************************************
'	Function Name		fnAlert				
'
'	Description			This function is used to display an
'						alert message
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnAlert(sMsg)
{
	alert(sMsg);
}

/*********************************************************************
'	Function Name		fnTrim				
'
'	Description			This function is used to trim a string of
'						white spaces
'
'	Modification Log
'   Date                Author				Modification 
' 	02-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnTrim(value) 
{
	return value.replace(/^\s+|\s+$/, '');
}

//********** Begin Mod v1.1 ****************//
/*********************************************************************
'	Function Name		fnValidateDOB				
'
'	Description			This function is the validation function
'						for entering a valid date of birth
'
'	Modification Log
'   Date                Author				Modification 
' 	19-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnValidateDOB(source, arguments)
{
	var txtDOBMonth = '';
	var txtDOBDay = '';
	var txtDOBYear = '';
	var dtRegExp = new RegExp(/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/);
	
	txtDOBMonth = fnTrim(document.getElementById('YouthProgram_UA1_txtDOBMonth').value);
	txtDOB = txtDOBMonth;

	txtDOBDay = fnTrim(document.getElementById('YouthProgram_UA1_txtDOBDay').value);
	txtDOB = txtDOB + "/" + txtDOBDay;

	txtDOBYear = fnTrim("19" + document.getElementById('YouthProgram_UA1_txtDOBYear').value);
	txtDOB = txtDOB + "/" + txtDOBYear;
	
	if (txtDOB == '//19')
		arguments.IsValid = true;
	else 
	{
		if (dtRegExp.test(txtDOB))
			arguments.IsValid = IsValidDate(txtDOBDay,txtDOBMonth,txtDOBYear);	
		else
			arguments.IsValid = false;
	}
	return true;
}

/*********************************************************************
'	Function Name		fnValidateHeight				
'
'	Description			This function is the validation function
'						for entering a valid height
'
'	Modification Log
'   Date                Author				Modification 
' 	19-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function fnValidateHeight(source, arguments)
{
	var txtFeet = '';
	var txtInch = '';
	var txtHeight = '';
	var txtRegExp = new RegExp(/[0-9]{1,2}\~[0-9]{1,2}/);
			
	txtFeet = fnTrim(document.getElementById('YouthProgram_UA1_txtHeightFeet').value);
	txtInch = fnTrim(document.getElementById('YouthProgram_UA1_txtHeightInch').value);
	txtHeight = txtFeet + "~" + txtInch;
		
	if (txtHeight == '~')
		arguments.IsValid = true;
	else
	{
		if (txtRegExp.test(txtHeight))
		{
			bValid = true;
			var iFeet = parseInt(txtFeet, 10)
			if (isNaN(iFeet) || iFeet < 0)
				bValid = false;
			else
			{
				var iInch = parseInt(txtInch, 10)
				if (isNaN(iInch) || iInch < 0 || iInch > 11)
					bValid = false;
			}			
			arguments.IsValid = bValid;
		}
		else
			arguments.IsValid = false;
	}
	return true;	
}

/*********************************************************************
'	Function Name		IsValidDate				
'
'	Description			This function is used to check whether date passed
'						is a valid date
'
'	Modification Log
'   Date                Author				Modification 
' 	19-Jan-2007		  Devender Mallya		Initial version
'*********************************************************************/
function IsValidDate(sDay,sMonth,sYear)
{
    var sDate = sMonth + "/" + sDay + "/" + sYear;
    var dtDate = new Date(sDate);
    var iMonth;
                
    if (isNaN(sMonth))
		iMonth = -1;
	else
		iMonth = parseInt(sMonth, 10);
				           
    if (isNaN(dtDate))
		return false;
	else
	{		
		if(dtDate.getDate()!=sDay)
			return false;
		else if(dtDate.getMonth()!=iMonth-1)
			return false;
		else if(dtDate.getFullYear()!=sYear)
			return false;
        
		return true;
	}
 }
//********** End Mod v1.1 ****************//
//*** Begin Mod v2.0 ***'

/*******************************************************************************
' Function Name		fnbtnSubmitValidate
'
' Description		This function checks if all the validations on the form 
'                   Register_PC are satisfied.     
'
' Modification Log
'
'       Date                Author                           Modification 
' 	26-April-2007   Mervyn C Fernandes,Infosys Tech Ltd.      Initial Version
'*******************************************************************************/
function fnbtnSubmitValidate(source, arguments)
{
	var sDate;   
	var sCtlName;
	var sCtlValue;
	var dgDependants = document.getElementById('Register_UA1_dgDepandants');
		
	for (var i=2;i<=dgDependants.rows.length;i++)
	{
		sCtlName = 'Register_UA1_dgDepandants__ctl' + i + '_txtDependantsDOB';
		sCtlValue = fnTrim(document.getElementById(sCtlName).value);
		if (sCtlValue != '')
		{
			sDate = sCtlValue.split("/");
			
			if (!(IsValidDate(sDate[1],sDate[0],sDate[2])))    
			{
				fnFocus(sCtlName);
				arguments.IsValid = false;
			}
		}
	}	
	return true;   
}

//*** End Mod v2.0 ***'
//Begin ver 3.2
/*******************************************************************************
' Function Name		fnCount
'
' Description		This function checks if all the validations on the form 
'                   Register_PC are satisfied.     
'
' Modification Log
'
'       Date                Author                           Modification 
' 	26-Feb-2008   Renuka Bedre,Infosys Tech Ltd.      Initial Version
'*******************************************************************************/
function fnCount(text,numchars) 
{

	var maxlength = new Number(numchars); // Change number to your max length.

	if (text.value.length > maxlength)
	{
		text.value = text.value.substring(0,maxlength);
		alert("Please limit your comments to " + numchars + " characters");
	}

}


//End Ver 3.2