//**********************************************************************************************************
// validEmail - Email Validator Function Begins
//**********************************************************************************************************
function validEmail(email) {
			invalidChars = "~`!#$%^&*()=+[]{}:;\/\"'?<>, "
				
			for (i=0; i<invalidChars.length; i++) {
				// does it contain any invalid characters?
				badChar = invalidChars.charAt(i)
				if (email.indexOf(badChar,0) > -1) {
					return false
				}
			}
			atPos = email.indexOf("@",1)
				// there must be one "@" symbol
			if (atPos == -1) {
				return false
			}
			if (email.indexOf("@",atPos+1) != -1) {
				// and only one "@" symbol
				return false
			}

			aftat = (email.indexOf("@",atPos))
			aftat = aftat + 1
		    if ((aftat == email.indexOf(".")))
			{
			  return false
			}
			tot_length = email.indexOf("@")+2;
//----------------------------------------------------------
			gan_mailvalue = document.forms[0].elements[3].value;
			gan_periodPos = email.indexOf(".",atPos)
			
//----------------------------------------------------------
//			if (tot_length > (email.indexOf(".")))
			if (tot_length > gan_periodPos)
			{
				//there must be atleast 1 characters after @ and before .
				return false
			}

			periodPos = email.indexOf(".",atPos)
			if (periodPos == -1) {
				// and atleast one "." after the "@"
				return false
			}

			if (periodPos+3 > email.length)	{
				// must be atleast 2 characters of category after "."
				return false
			}
			
			return true
		}
//######################################### Email Validator Function Ends ###################################################

//**********************************************************************************************************
// validName - Special character validation
//**********************************************************************************************************
function validName(name) {
	invalidChars = "!@#$%^&*()+=_~`:;\/|[]{},<>?\""

	for (i=0; i<invalidChars.length; i++) {
		// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (name.indexOf(badChar,0) > -1) {
			return false
		}
	}
	return true
}
//**********************************************************************************************************
// validateDates - Date Validation Begins
//**********************************************************************************************************
function validateDates(date)
{  
	var Proceed = 0;
	// Create a variable to hold the correct format.
	var CorrectFormat = /\b[0-1][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]\b/; //mm/dd/yyyy format
	
	// If the field has value, validate the date.
	   if (date)
	        {	var message = "You must enter a value for the date.";
		        // Test to see if the format of the date is correct.
		              if (CorrectFormat.test(date))
		                    {	// Split out the month, day and year variables.
			                    var Month = date.substr(0,2);
			                    var Day = date.substr(3,2);
			                    var Year = date.substr(6,4);
        	
                    			  sYear = Year;
								  sMonth = Month;
								  sDay = Day;
			
			           			// Ensure all the values are greater than 0
			                          if (Month > 0 && Day > 0 && Year > 0 && Month<=12)
											{	// Find the max day for the month
												// The default max day is 31
													var maxDays = 31;
												// If the month is April, June, September or November the max day is 30
												if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
														{ maxDays = 30;
														}
														if (Month == 2)
															{
																if (Year % 4 > 0) 
																	maxDays =28;
																else
																	if (Year % 100 == 0 && Year % 400 > 0) 
					  														maxDays = 28;
																	else
																		  	maxDays = 29;
															}// Month if ends
													// Determine if the day entered is less than the max days for that month.
																if (Day <= maxDays)
																	{ Proceed = 1;	}
											} //Month Day year if ends
								} //CorrectFormat if ends
			} //date if ends
							if (Proceed == 0)
									{ return false; }
							else
									{ return true;	}
} // validateDates function ends here

//**********************************************************************************************************
// DtFieldkeyPress() - Making textboxes non-editable for date fields
//**********************************************************************************************************
function DtFieldkeyPress(e, obj)
{
	alert("Click the calendar icon to choose a date, you cannot key in on this field. ");
	return false;
}
//**********************************************************************************************************
// SubmitIt - Fields Validator Function Begins
//**********************************************************************************************************
function SubmitIt(theform)
{	
		var retVal = true;	
		
		if (document.theform.elements["txtEmpName"].value == "") {
			alert("Enter Employee Name");
			document.theform.elements["txtEmpName"].focus();
			return false;
		}

		if (!validName(document.theform.elements["txtEmpName"].value)) {
			alert("No special characters allowed in the Employee Name field");
			document.theform.elements["txtEmpName"].focus();
			return false;
		}

		if (document.theform.elements["dtWeekOf"].value == "") {
			alert("Enter Week Of Date");
			document.theform.elements["dtWeekOf"].focus();
			return false;
		}
		if (((document.theform.elements["dtWeekOf"].value>="a") && (document.theform.elements["dtWeekOf"].value<="z")) || ((document.theform.elements["dtWeekOf"].value>="A") && (document.theform.elements["dtWeekOf"].value<="Z"))){
			alert("Enter Week Of Date in the format - mm/dd/yyyy");
			document.theform.elements["dtWeekOf"].focus();
			return false;
		}


		// Loop through Table Rows to validate dynamic fields
		var tbl = document.getElementById('tblActivity');
		var rowlen = tbl.rows.length;

		document.theform.elements["NoOfRows"].value = rowlen-1; // Set the number of rows hidden column value (Do not account for header)

		for(var i=1; i<rowlen; i++)
		{

			var msgRowIdentifier = " - For Item #:" + i;

			var dt		= document.theform.elements["dtVisit" + i];
			var name	= document.theform.elements["txtName" + i];
			var addr	= document.theform.elements["txtAddress" + i];
			var cmts	= document.theform.elements["txtComments" + i];
	
			var addrLen		= addr.value.length;
			var cmtsLen	= cmts.value.length;
										
			if (dt.value == "") {
				alert("Enter Date" + msgRowIdentifier);
				dt.focus();
				retVal = false;
				break;
			}
			if (((dt.value>="a") && (dt.value<="z")) || ((dt.value>="A") && (dt.value<="Z"))){
				alert("Enter Date in the format - mm/dd/yyyy" + msgRowIdentifier);
				dt.focus();
				retVal = false;
				break;
			}

			if (name.value == "") {
				alert("Enter Name" + msgRowIdentifier);
				name.focus();
				retVal = false;
				break;
			}

			if (!validName(name.value)) {
				alert("No special characters allowed in the name field" + msgRowIdentifier);
				name.focus();
				retVal = false;
				break;
			}
			
			if (addrLen <= 10) {
				var prompt = "The address you entered has only " + addrLen + " characters.\n";
				prompt = prompt + "Please provide with correct address including city and zip code";
				alert(prompt + msgRowIdentifier);
				addr.focus()
				retVal = false;
				break;
			}
			if (cmtsLen <= 5) {
				var prompt = "The comments you entered has only " + cmtsLen + " characters.\n";
				prompt = prompt + "The contents of this field should be atleast more than 5 characters.";
				alert(prompt+ msgRowIdentifier);
				cmts.focus()
				retVal = false;
				break;
			}
		}

	   if(retVal == true)
	   {	
		    var msg = "Activity log submitted cannot be modified. \n";
			msg = msg + "Are you sure you want to submit your activity log ?";
			var msgConfirm = confirm(msg);
			if(msgConfirm == true)
				return retVal;
			else
				return false;
	   }
	   else
	   {
			return false;
	   }
}

//**********************************************************************************************************
// retVal - Used to retrieve the querystring parameter value
//**********************************************************************************************************
function retVal(sName)
{
  var sURL = new String(window.location);
  var iQMark= sURL.lastIndexOf('?');
  var iLensName=sName.length;
  
  //retrieve loc. of sName
  var iStart = sURL.indexOf('?' + sName +'=') //limitation 1
  if (iStart==-1)
        {//not found at start
        iStart = sURL.indexOf('&' + sName +'=')//limitation 1
        if (iStart==-1)
           {//not found at end
            return 0; //not found
           }   
        }
        
  iStart = iStart + + iLensName + 2;
  var iTemp= sURL.indexOf('&',iStart); //next pair start
  if (iTemp ==-1)
        {//EOF
        iTemp=sURL.length;
        }  
  return sURL.slice(iStart,iTemp ) ;
  sURL=null;//destroy String
}
//**********************************************************************************************************
// validatePage - Check whether the page loads for Emailing or Re-directed after Successful Emailing.
//**********************************************************************************************************
function validatePage()
{
	var strQryStringVal = retVal('msg')
	if(strQryStringVal == 1)
	{
		var msg = "Thank You for submitting your activity log. \n";
		msg = msg + "Do you want to submit another activity log ?";
		var msgConfirm = confirm(msg);
			if(msgConfirm == true)
				window.location.href('emplyoeezone.html');
			else
				window.location.href('index.html');
	}
}
//**********************************************************************************************************
