

// validation for NULL value
function validate_required(field,alerttxt) 
{
	with (field) 
	{
		if (value==null||value=="")
		{
			
			alert(alerttxt);
			return false;
		}
		else 
		{
			return true;
		}
	}
}


// validation for user ID

function validate_userid(field,alerttxt)
{
	
	with (field) 
	{
		apos=value.length
		// checks for the length
		if (apos < 6 || apos >12 ) 
		{
			alert(alerttxt);
			return false;
		} 
		else
		{
			//checks for the starting letter in user iD
			
			var alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
			var i=0
			
			if (alph_valid.indexOf(value.charAt(i)) < 0 )	//value.charAt(i) returns 1 or -1
			{
				alert('Your ID should start only with characters ');
				 return false;
			}
			
			// checks for special character
			if(value.match(/^[.a-zA-Z0-9]+$/))
			{
				return true;
			}
			else
			{
				alert ("Your user ID has special characters\n These are not allowed.");
				return false;
			}	
		}  //End of else
	}      //End of with
}


function validate_password(field,alerttxt)
{
	with (field) 
	{
		apos=value.length
		// checks for the length
		if (apos < 6 || apos >12 ) 
		{
			alert(alerttxt);
			return false;
		} 
		else
		{
			return true;
		}
	}
}


// function to confirm the password
function validate_confirmpassword(field1,field2)
{
	with(field1)
	{
		with(field2)
		{
			if (field1.value == field2.value) 
			{
				return true;
			} 
			else
			{
				alert("Incorrect password in Re-Type");
				return false;
			}
		}
	}
	
}

// Declaring valid date character, minimum year and maximum year

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// To calculate no of days in feb
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

// To calculate  no of days in a month
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}


// validation for date
function isDate(field)
{
	with(field)
	{
		dtStr = field.value     //	Assigning the value to variable dtStr
		var dtCh= "/";			// Date seperator 
		var minYear=1900;
		var maxYear=2100;

		var daysInMonth = DaysArray(12)           // Gets the no of days in a month
		var pos1=dtStr.indexOf(dtCh)			  // Gets the position of first seperator 
		var pos2=dtStr.indexOf(dtCh,pos1+1)       // Gets the position of second seperator
		var strDay=dtStr.substring(0,pos1)        // Gets the day from a string
		var strMonth=dtStr.substring(pos1+1,pos2) // Gets the month from a string
		var strYear=dtStr.substring(pos2+1)       // Gets the year from a string
		strYr=strYear
	
		//checks first char in Day is '0'if so takes second char & puts in strDay
		if (strDay.charAt(0)=="0" && strDay.length>1) 
			strDay = strDay.substring(1)
		
		//checks first char in month is '0'if so takes second char & puts in strMonth
		if (strMonth.charAt(0)=="0" && strMonth.length>1) 
			strMonth=strMonth.substring(1)
	
	
		for (var i = 1; i <= 3; i++) 
		{
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)

		//	If seperator is at pos -1 it is not valid
		if (pos1==-1 || pos2==-1)
		{
			alert("The date format should be : dd/mm/yyyy")
			return false
		}

		 //	Checks for month length & value  should be b/w 1-12
		if (strMonth.length<1 || month<1 || month>12)
		{
			alert("Please enter a valid month")
			return false
		}

		//	Checks for Day length & value  should be b/w 1-31
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{
			alert("Please enter a valid day")
			return false
		}

		//	Checks for year length & value  should be b/w minYear - maxYear
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
		{
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
			return false
		}

	 // Checks for the seperator in string
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		{
			alert("Please enter a valid date")
			return false
		}
	return true
	
}

function ValidateForm()
{
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false)
	{
		dt.focus()
		return false
	}
	}
    return true

}

function validate_required(field,alerttxt) 
{
	with (field) 
	{
		if (value==null||value=='')
		{
			
			alert(alerttxt);
			return false;
		}
		else 
		{
			return true;
		}
	}
}

function validate_required_text(field,alertxt)
{
	with (field)
	{
		if(value.length<=0 || value.length>6)
		{
			alert(alertxt);
			return false;
		}
	}
}

function validate_required_security_answer(field,alertxt)
{
	with (field)
	{
		if(value.length <= 0)
		{
			alert(alertxt);
			return false;
		}
	}
}


// Validation of form 

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(user_id,"User ID should be filled out")==false)
		{
			user_id.focus();
			return false;
		}
		if (validate_userid(user_id,"Not a valid User ID!")==false) 
		{
			user_id.focus();
			return false;
		}
		if (validate_required(user_pass_1,"Password must be filled out!")==false)
		{
			user_pass_1.focus();
			return false;
		}
		if (validate_password(user_pass_1,"Not a valid password!")==false)
		{
			user_pass_1.focus();
			return false;
		}
		if (validate_required(user_pass_2,"Retype Password must be filled out!")==false)
		{
			user_pass_2.focus();
			return false;
		}
		 if (validate_confirmpassword(user_pass_1,user_pass_2)==false)
		{
			user_pass_2.focus();
			return false;
		}
		
		if(isDate(doc_dob)==false)
		{
			doc_dob.focus();
			return false;
		}
		

		if(validate_required_text(pincode_r,"Invalid PIN !")==false)
		{
			pincode_r.focus();
			return false;
		}

		if(validate_required_security_answer(secret_r,"Please enter a valid answer") == false)
		{
			secret_r.focus();
			return false;
		}
   	}
}



/*
function validate_form(thisform)
{
    with (thisform)
    {
        if(validate_required(doc_name,"Invalid Doctor's Name !")==false)
        {
            doc_name.focus();
            return false;
        }

        if(isDate(doc_dob)==false)
        {
            doc_dob.focus();
            return false;
        }
        

        if(IsNumeric(doc_yr_of_passing,"Invalid Year of Passing!")==false)
        {
            doc_yr_of_passing.focus();
            return false;
        }


        if(validate_required_text(pincode_r,"Invalid PIN for Residence!")==false)
        {
            pincode_r.focus();
            return false;
        }


        if(validate_required_text(pincode_c,"Invalid PIN for Clinic!")==false)
        {
            pincode_c.focus();
            return false;
        }


        if(validate_required_password(user_pass_1,"Invalid Password!")==false)
        {
            pincode_c.focus();
            return false;
        }


        if(validate_required_password(user_pass_2,"Invalid Password!")==false)
        {
            pincode_c.focus();
            return false;
        }

        if(validate_required_security_answer(secret_r,"Please enter a valid answer") == false)
        {
            secret_r.focus();
            return false;
        }
        
    }
}
*/


