/***************************************************************************************************
 * Check is field is found
 * @param	frm - document.form.fieldname
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsFound(ctrl)
{
	if(typeof ctrl != 'undefined')
		return true;
	return false;
}

/***************************************************************************************************
 * Check for at least one check box is checked
 * @param	frm - document.form, chkName - Name of check box, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncCheckAtLeastOne(frm, chkName, fieldName)
{
	var i;
	for( i = 0; i < frm.elements.length; i++)
	{
		if(frm.elements[i].name.indexOf(chkName) != -1)
		{
			if(frm.elements[i].checked)
				return true;
		}
	}
	alert("Please Select " + fieldName);
	return false;
}

/***************************************************************************************************
 * Check for empty field
 * @param	frm - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncEmptyField(text1, fieldName)
{
	if ( (text1.value.charAt(0) == " ") || (text1.value.length == 0) )
	{
		alert("Please Enter " + fieldName);
		return false;
	}
	else
		return true;
}

/***************************************************************************************************
 * Check for empty field
 * @param	frm - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncEmptyNum(text1, fieldName)
{
	var i = 0;
	if ( text1.value.length == 0 )
	{
		alert(fieldName + " Should Not Be Empty");
		return false;
	}
	for( i = 0; i < text1.value.length; i++)
	{
		if(text1.value.charAt(i) == " ")
		{
			alert(fieldName + " should not have spaces");
			return false;
		}
	}
	return true;
}

/***************************************************************************************************
 * Check is field is a correct contact number
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsContact(txtField, fieldName)
{
	if ( fncIsFound(txtField) == true )
	{
		var i = 0;
		if ( ((txtField.value.charAt(0) == " ") && (txtField.value.length == 1)) || (txtField.value.length == 0) )
		{
			txtField.value = "";
			return true;
		}
		if ( fncEmptyNum(txtField, fieldName) == false )
			return false;
		else if ( fncIsPositiveNum(txtField, fieldName) == false )
			return false;
		else if ( fncIsWholeNum(txtField, fieldName) == false )
			return false;	
		else if ( fncIsThereFunnyChar(txtField, fieldName) == false )
			return false;	
		else
			return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check is field is a correct FLOAT
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsFloat(txtField, fieldName)
{
	if ( fncIsFound(txtField) == true )
	{
		var i = 0;
		if ( ((txtField.value.charAt(0) == " ") && (txtField.value.length == 1)) || (txtField.value.length == 0) )
		{
			txtField.value = "";
			return true;
		}
		if ( fncEmptyNum(txtField, fieldName) == false )
			return false;
		else if ( fncIsPositiveNum(txtField, fieldName) == false )
			return false;
		else
		{
			var funnyChar = "~`!@#$%^&*()_+=-;:'/\?,<>{}[]|\\\"";
			for(i=0;i<txtField.value.length;i++)
			{
				for(j=0;j<funnyChar.length;j++)
				{
					if(txtField.value.charAt(i) == funnyChar.charAt(j))
					{
						alert(fieldName + " Should Not Contain '" + 	funnyChar.charAt(j) + "'");
						return false;
					}
				}
			}	
		}
	}
	else
		return false;
}

/***************************************************************************************************
 * Check is field is a positive number
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsPositiveNum(txtField, fieldName)
{
	if ( (fncIsFound(txtField) && fncEmptyNum(txtField, fieldName)) == true )
	{
		if(fncIsNum(txtField,fieldName))
		{
			if(parseInt(txtField.value) < 0)
			{
				alert(fieldName + " Must Be Positive");
				return false;
			}
			else if ( (parseInt(txtField.value) == 0) && (txtField.value.indexOf("-") != -1) )
			{
				alert("Zero Should Not Have A Minus Sign");
				return false;	
			}
			else
				return true;
		}
		else
			return false;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check is field is a integer
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsWholeNum(txtField, fieldName)
{
	if ( (fncIsFound(txtField) && fncEmptyNum(txtField, fieldName)) == true )
	{
		if(fncIsNum(txtField,fieldName))
		{
			if(txtField.value.indexOf(".") != -1)
			{
				alert(fieldName + " Should Not Contain Decimal");
				return false;
			}
			else
				return true;
		}	
		else
			return false;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check is field is a number
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsNum(txtField, fieldName)
{
	if ( (fncIsFound(txtField) && fncEmptyNum(txtField, fieldName)) == true )
	{
		if(isNaN(txtField.value))
		{
			alert(fieldName + " Must Be A Number");
			return false;
		}
		else
			return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check is field has a funny character
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsThereFunnyChar(txtField, fieldName)
{
	var funnyChar = " ~`!@#$%^&*()_+=-;:'/\?.,<>{}[]|\\\"";
	if ( (fncIsFound(txtField) && fncEmptyField(txtField, fieldName)) == true )
	{
		var i=0;
		var j=0;
		for(i=0;i<txtField.value.length;i++)
		{
			for(j=0;j<funnyChar.length;j++)
			{
				if(txtField.value.charAt(i) == funnyChar.charAt(j))
				{
					alert(fieldName + " Should Not Contain '" + 	funnyChar.charAt(j) + "'");
					return false;
				}
			}
		}	
	}	
	else
		return false;
	return true;
}

/***************************************************************************************************
 * Check is both password are the same
 * @param	txtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsPasswordSame(txtField, txtField2, fieldName)
{
	if ( (fncIsFound(txtField) && fncEmptyField(txtField, fieldName)) == true )
	{
		if ( (fncIsFound(txtField2) && fncEmptyField(txtField2, fieldName)) == true )
		{
			if ( txtField.value != txtField2.value )
			{
				alert(fieldName + " Must Be The Same");
				return false;	
			}
			else
				return true;	
		}	
		else
			return false;
	}	
	else
		return false;
}

/***************************************************************************************************
 * Check Email is valid
 * @param	ttxtField - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsEmail(txtField, fieldName)
{
	if ( (fncIsFound(txtField) && fncEmptyField(txtField, fieldName)) == true )
	{
		if(txtField.value.indexOf(",") != -1)
		{
			alert(fieldName + " Contain Invalid Email Address");
			return false;
		}
		var firstIndex = txtField.value.indexOf("@");
		var secondIndex = txtField.value.indexOf(".");
		if((firstIndex == -1) || (secondIndex == -1))
		{
			alert(fieldName + " Contain Invalid Email Address");
			return false;
		}
	
		var oldIndex = 0;
		var index = 0;
		while(1)
		{
			index = txtField.value.indexOf(".",oldIndex);
			if(index == -1)
				break;
			oldIndex = index + 1;
		}

		oldIndex = oldIndex - 1;
		if(oldIndex < firstIndex)
		{
			alert(fieldName + " Contain Invalid Email Address");
			return false;
		}			
		return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if filed exceed maximum characters
 * @param	txtAreaField, maxiLen, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsExceedMaxLen(txtAreaField, maxiLen, fieldName)
{
	if(fncIsFound(txtAreaField))
	{
		if(txtAreaField.value.length > maxiLen)
		{
			alert(fieldName + " Should not exceed " + maxiLen + " Characters");
			return false;
		}
		else
			return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if day is allowed
 * @param	day - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncCheckDay(day, fieldName)
{
	if ( (day.value.length == 2) && (parseInt(day.value) < 10) )
		day.value = day.value.charAt(1);
	if ( (fncIsFound(day) && fncEmptyNum(day, "Day of " + fieldName)) == true )
	{
		if((fncIsWholeNum(day, "Day Of " + fieldName)) && (fncIsPositiveNum(day, "Day Of " + fieldName))) 
		{
			if((parseInt(day.value) > 31) || (parseInt(day.value) < 1))
			{
				alert("Day Of " + fieldName + " Must Be Between 1 And 31.");
				return false;
			}	
			else
				return true;
		}
		else
			return false;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if month is allowed
 * @param	month - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncCheckMonth(month, fieldName)
{
	if ( (month.value.length == 2) && (parseInt(month.value) < 10) )
		month.value = month.value.charAt(1);


	if ( (fncIsFound(month) && fncEmptyNum(month, "Month of " + fieldName)) == true )
	{
		if((fncIsWholeNum(month, "Month Of " +fieldName)) && (fncIsPositiveNum(month, "Month Of " + fieldName))) 
		{
			if((parseInt(month.value) > 12) || (parseInt(month.value) < 1))
			{
				alert("Month Of " + fieldName + " Must Be Between 1 And 12.");
				return false;
			}	
			else
				return true;
		}
		else
			return false;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if year is allowed
 * @param	year - document.form.fieldname, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncCheckYear(year, fieldName)
{
	if ( (fncIsFound(year) && fncEmptyNum(year,"Year of " + fieldName)) == true )
	{
		if((fncIsWholeNum(year, "Year Of " + fieldName)) && (fncIsPositiveNum(year, "Year Of " + fieldName))) 
		{
			if(parseInt(year.value) < 1900) 
			{
				alert("Year Of " + fieldName + " Must Be Later Than 1900.");
				return false;
			}	
			else
				return true;
		}
		else
			return false;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if Date is correct
 * @param	day, month, year, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncIsDate(day, month, year, fieldName)
{
	var err = 0;
	var msg = fieldName + " Contain Invalid Date";
	if((fncIsFound(day))  && (fncIsFound(month)) && (fncIsFound(year)))
	{
		if(!(fncCheckDay(day, fieldName))) 
			return false;	

		if(!(fncCheckMonth(month, fieldName))) 
			return false;	

		if(!(fncCheckYear(year, fieldName)))
			return false;	

		if((parseInt(month.value) == 4) || (parseInt(month.value) == 6) || (parseInt(month.value) == 11) || (parseInt(month.value) == 9 ))
		{
			if(parseInt(day.value) == 31)
			{
				alert(msg);
				return false;
			}
			else
				return true;
		}
		else if (parseInt(month.value) == 2)
		{
			var leap = parseInt((year.value)%4);
			if(leap != 0)
			{
				if(parseInt(day.value) > 28)
				{
					alert(msg);
					return false;
				}
				else
					return true;
			}
			else
			{
				if(parseInt(day.value) > 29)
				{
					alert(msg);
					return false;
				}
				else
					return true;
			}
		}
		else
			return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if Date is later than today
 * @param	day, month, year, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncLaterThanToday(day, month, year, fieldName)
{
	var curDate = new Date();
	var curDay = curDate.getDate();
	var curMonth = curDate.getMonth()+1;
	var curYear = curDate.getFullYear();
	var errMsg = fieldName + " Must Be Later Than Today";
	if(fncIsDate(day, month, year, fieldName))
	{

		if(parseInt(year.value) < curYear)
		{
			alert(errMsg);
			return false;
		}
		else if(parseInt(year.value) == curYear)
		{
			if(parseInt(month.value) < curMonth)
			{
				alert(errMsg);
				return false;
			}
			else if (parseInt(month.value) == curMonth)
			{
				if(parseInt(day.value) < curDay)
				{
					alert(errMsg);
					return false;
				}
				else
					return true;
			}
			else
				return true;
		}
		else
			return true;
	}
	else
		return false;
}

/***************************************************************************************************
 * Check if Date is ealier than today
 * @param	day, month, year, fieldName - Error Message
 * @return true if successfull, false if otherwise
 ***************************************************************************************************/
function fncEarlierThanToday(day, month,year, fieldName)
{
	var curDate = new Date();
	var curDay = curDate.getDate();
	var curMonth = curDate.getMonth()+1;
	var curYear = curDate.getFullYear();
	var errMsg = fieldName + " Must Be Earlier Than Today";
	if(fncIsDate(day, month, year, fieldName))
	{
		if(parseInt(year.value) > curYear)
		{
			alert(errMsg);
			return false;
		}
		else if(parseInt(year.value) == curYear)
		{
			if(parseInt(month.value) > curMonth)
			{
				alert(errMsg);
				return false;
			}
			else if (parseInt(month.value) == curMonth)
			{
				if(parseInt(day.value) > curDay)
				{
					alert(errMsg);
					return false;
				}
				else
					return true;
			}
			else
				return true;
		}
		else
			return true;
	}
	else
		return false;
}

