// JavaScript Document

/*
	This function is to check if a object is valid or not
	input:
		objToTest: Object to check
	output:
		return true if object else return false
*/
function testIsValidObject(objToTest) {
		if (null == objToTest) {
			return false;
		}
		if ("undefined" == typeof(objToTest) ) {
			return false;
		}
		return true;
}

/*
	This function is to alert a message
	input:
		objToTest		 : Object to focus
		alertMessage	: This is the message to be displayed in case of error
	output:
		alert a message and return false
*/
function displayAlertMessage(objToTest,alertMessage) {
	alert(alertMessage);
	objToTest.focus();
	return false;
}

/* To check whether a value is null or not
 arguments passed:
	ObjToTest		: This is the referance to the object whose value is to be checked
 Output:
 	If object value is Null than return false else return true
*/
function valueIsNull(objToTest) {
	if (testIsValidObject(objToTest)) {
		if (objToTest.value == "" || objToTest.value==null) {
			return true;
		}
	}
	return false;
}

/* To validate Radio Button
 Input:
	ObjToTest	: This is the referance to the object whose value is to be checked
 Output:
 	Return the value of checked radio button
*/
function validateRadioButton(objToTest) {
	var result = true; 
	var radioSelected=99; 
	if (testIsValidObject(objToTest)) {
		if (objToTest.length == 1){
			if(objToTest.checked == true){
				isChecked = true;
			}
		}
		else{
			for (var i=0;i<objToTest.length;i++) {
				if (objToTest[i].checked) {
					radioSelected = objToTest[i].value;
				}
			}
		}
	}
	return radioSelected;
}

/*
	This function is used to do specific task depending on the value returned by validateRadioButton function
	input:
		radioSelected: Value of the selected radio button
		alertMessage: This is the message to be displayed in case of error
		ObjToTest	: Object to be focused
	Output:
			
*/
function radioButtonSpecificTask(radioSelected,alertMessage,objToTest){
	if (!radioSelected){ 
		displayAlertMessage(objToTest,alertMessage);
	} 
	else {
		//call a function do some specific task such as call show() function
	}
	return true;
}

/*
	This function is to validate the check boxes
	input:
		objToTest: Object to be checked
	output:
		return true if any check box is checked else return false
*/
function validateCheckBoxes(objToTest)
    {
        var isChecked = false;
		if (testIsValidObject(objToTest)) {
			if (objToTest.value.length == 1){
				if(objToTest.checked == true){
					isChecked = true;
				}
			}
			else{
				for (var counter=0; counter<objToTest.value.length; counter++){
					if (objToTest[counter].checked == true){
						isChecked = true;
					}
				}
			}
		}
		return isChecked;
    }

/*
	This function is used to do specific task depending on the value returned by validatecheckBoxes function
	input:
		radioSelected: Value of the selected radio button
		alertMessage: This is the message to be displayed in case of error
		ObjToTest	: Object to be focused
	Output:
			
*/
function checkBoxesSpecificTask(isChecked,alertMessage,objToTest){
	if (!isChecked){ 
		displayAlertMessage(objToTest,alertMessage);
	} 
	else {
		//call a function do some specific task such as call show() function
	}
	return true;
}

/*
	This function check for all characters allowed to this field
	Input:
		charAllowed : This is the list of all those characters which are allowed
		objToTest	: This is the object that has to be checked
	Output:
		return true if no other character is present other than allowed characters else return false
		 
*/

function checkAllowedChar(charAllowed,objToTest){
	if (testIsValidObject(objToTest)) {
		var allValid = true;
		// to check all white spaces
		var flag = false;
		if (objToTest.value.charAt(0)==" "){
			flag=true;
		}
		for (i=1;i<objToTest.value.length;i++){
			ch = objToTest.value.charAt(i);
			if (ch==" " && flag){
				flag = true;
			}
			else
				flag = false;
		}
		if (flag && i==objToTest.value.length) return false;
		
		
		for (i = 0;  i < objToTest.value.length;  i++){
			ch = objToTest.value.charAt(i);
			for (j = 0;  j < charAllowed.length;  j++){
				if (ch == charAllowed.charAt(j)){
					break;
				}
				if (parseInt(j+1) == parseInt(charAllowed.length)){
					allValid = false;
					break;
				}
			}
		}
	}
	return allValid;
}
 /* if (!allValid)
  {
    displayAlertMessage(objToTest,alertMessage);
  }*/

/*
	This Function is to check the range of a variable
	Input:
		Value		: this varibale coantains the value to be matched with
		operator	: this varibale contains the operator '<','>','='
		objToTest	: This is the object that has to be checked
	Output:
			Return true if satisfy the condition else retrun false	 
*/
function validateLength(objToTest,value,operator){
	var flag = false;
	switch(operator){
		case '<':
				if (objToTest.value.length < value)
					flag = true;
					break;
		case '>':
				if (objToTest.value.length > value)
					flag = true;
					break;
		case '=':
				if (objToTest.value.length = value)
					flag = true;
					break;
	}
	return flag;
}

/*
	This Function is to check the range of a variable
	Input:
		Value		: this varibale coantains the value to be matched with
		operator	: this varibale contains the operator '<','>','='
		objToTest	: This is the object that has to be checked
	Output:
			Return true if satisfy the condition else retrun false	 
*/
function validateRange(objToTest,value,operator){
	var flag = false;
	switch(operator){
		case '<':
				if (objToTest.value < value)
					flag = true;
					break;
		case '>':
				if (objToTest.value > value)
					flag = true;
					break;
		case '=':
				if (objToTest.value = value)
					flag = true;
					break;
	}
	return flag;
}
/*
if (!flag){
	displayAlertMessage(objToTest,alertMessage);
}*/

/*
	This function validate the email address
	Input:
		objtoText		: Object to be checked
	Output:
		return true if email address is valid else return false
		
*/
function validateEmail(objToTest) {
	var at="@"
	var dot="."
	objToTest = objToTest.value;
		if(objToTest =="")
		return true;
	var lat=objToTest.indexOf(at)
	var lstr=objToTest.length
	var ldot=objToTest.indexOf(dot)
	if (objToTest.indexOf(at)==-1){
		return false
	}
	
	if (objToTest.indexOf(at)==-1 || objToTest.indexOf(at)==0 || objToTest.indexOf(at)==lstr-1){
		return false
	}
	
	if (objToTest.indexOf(dot)==-1 || objToTest.indexOf(dot)==0 || objToTest.indexOf(dot)==lstr-1 ||objToTest.indexOf("--")!=-1){
		return false
	}
	
	if (objToTest.indexOf(at,(lat+1))!=-1){
		return false
	}
	
	if (objToTest.substring(lat-1,lat)==dot || objToTest.substring(lat+1,lat+2)==dot){
		return false
	}
	
	if (objToTest.indexOf(dot,(lat+2))==-1){
		return false
	}
	return true	;
}
// to trim a string
function Trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

