// Function to check whether the field is alphabetic

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Function to check whether the field is numeric
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


var whitespace = "\\t\\n\\r";
function isWhitespace (s)
{
  var i;
  if (isBlank(s)) return true;
  for (i = 0; i < s.length; i++)
  {
	alert("inside for");
	alert("s.length : " + s.length);
    var c = s.charAt(i);
    alert("c : " + c);
    if (whitespace.indexOf(c) == -1) return false;
    alert("whitespace.indexOf(c) : " + whitespace.indexOf(c));
  }
  return true;
}


// Function to check whether the field is blank

function isBlank(s)		
{
	for(var i = 0; i < s.length; i++) 	
	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) 
		return false;	
	}
	return true;
}	

//=======================================================================================

// Function to Check for valid email address

//========
//Updated email validation function
function checkEmail(theForm,item)
	{
		
		var emailPat=/^(.+)@(.+)$/ ;
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" ;
		var validChars="\[^\\s" + specialChars + "\]" ;
		var quotedUser="(\"[^\"]*\")" ;
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/ ;
		var atom=validChars + '+' ;
		var word="(" + atom + "|" + quotedUser + ")" ;
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$") ;
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$") ;
		var matchArray=theForm.match(emailPat) ;
			if (matchArray==null) {
  			alert("Email address seems incorrect (check @ and .'s)");
  			item.focus();
			return false;
			}
		var user=matchArray[1];
		var domain=matchArray[2];

			if (user.match(userPat)==null) {
		    alert("The username doesn't seem to be valid.");
		    item.focus();
			return false;
			}

			var IPArray=domain.match(ipDomainPat);
			if (IPArray!=null) {
			  for (var i=1;i<=4;i++) {
				    if (IPArray[i]>255) {
					alert("Destination IP address is invalid!");
					item.focus();
					return false;
					}
			 }
			return true;
			}
		var domainArray=domain.match(domainPat)
			if (domainArray==null) {
				alert("The domain name doesn't seem to be valid.");
				item.focus();
				return false;
				}

			var atomPat=new RegExp(atom,"g");
			var domArr=domain.match(atomPat);
			var len=domArr.length;
			if (domArr[domArr.length-1].length<2 || 
			    domArr[domArr.length-1].length>3) {
			    alert("The address must end in a three-letter domain, or two letter country.");
			    item.focus();
				return false;
			}

			if (len<2) {
			   var errStr="This address is missing a hostname!";
			   alert(errStr);
			   item.focus();
			   return false;
			}
			return true;

	}
//========
function checkEmail1(theForm,item)
{
	if (isBlank(theForm))
	{	
		alert("Email required. Thank you.");
		item.focus();
		return false;
	}	
	else
	{
  		count=0;
  		flagdot=0;
  		pos=0;
		str= theForm;
		l=str.length;
		for(i=0;i<l;i++)
		{
			p=str.charAt(i)
			if(p=="@")
			{
				count++;pos=i;
			}
			if (p==".")
			{
				flagdot=i;
			}
		}				
		if(count==1)
		{
			if (flagdot>0)
			{
				return true;
			}
			else 
			{
				alert("Please insert a correct email ID");
				item.focus();
				return false;
			}
		}
		else 
		{
			alert("Please insert a correct email ID");
			item.focus();
			return false;
		}
	}	
}

//=======================================================================================
 
//	Function to validate date

function validateDate(sendDate,item)
{
	var mon = sendDate.substring(0,2);
	if ((sendDate.substring(2,3) != "/") || (sendDate.substring(5,6) != "/"))
	{	
		alert("Please enter date in correct format. e.g. 04/11/2001");
		return false;	
	}
	if ((sendDate.substring(0,2) > 12) || (isNaN(sendDate.substring(0,2))))
	{
		alert("Please enter correct month");
		return false;
	}
	else if ((sendDate.substring(3,5) > 31) || (isNaN(sendDate.substring(3,5))))
	{
		alert("Enter correct day");
		return false;
	}
	else if (sendDate.length < 10)
	{
		alert("Please enter correct Year");
		return false;
	}
	else if (sendDate.substr(6,4) < 1900)
	{
		alert("Year cannot be less than 1900");
		return false;
	}
	else if (((mon == 04) || (mon == 06) || (mon == 09) || (mon == 11)) && ((sendDate.substring(3,5)) > 30))
	{
		alert("Please check the month/day");
		return false;
	}
	else if ((mon == 02) && (sendDate.substring(3,5) > 28))
	{	
		var year = sendDate.substring(6,10);
		var yy = (((year % 4 == 0) && (!(year % 100 == 0) || (year % 400 == 0) ) ) ? 29 : 28);
		if (yy != sendDate.substring(3,5))
		{
			alert("Enter correct number of days for February");
			return false;
		}
	}
	return true;
}
 
//======================================================================================= 

//	Function to trim blank spaces

function trimSpaces(sString)
{
	sTrimmedString = "";
	if (sString != "")
	{
		var iStart = 0;
		var iEnd = sString.length - 1;
		var sWhitespace = " \t\f\n\r\v";

		while (sWhitespace.indexOf(sString.charAt(iStart)) != -1)
		{
			iStart++;
			if (iStart > iEnd)
				break;
		}

		// If the string not just whitespace
		if (iStart <= iEnd)
		{
			while (sWhitespace.indexOf(sString.charAt(iEnd)) != -1)
				iEnd--;
			sTrimmedString = sString.substring(iStart,++iEnd);
		}
	}
	return sTrimmedString;
}


//Replace Code

//function replace(originalString,searchText,replaceText)
//{
//    var strLength = originalString.length;
//    var txtLength = searchText.length;
//		if ((strLength == 0) || (txtLength == 0))
//      {
//            return originalString;
//      }
//      var i = originalString.indexOf(searchText);
//      if ((!i) && (searchText != originalString.substring(0,txtLength)))
//      {
//            return originalString;
//      }
//      if (i == -1)
//      {
//            return originalString;
//      }
//      var newstr = originalString.substring(0,i) + replaceText;
//      if (i+txtLength < strLength)
//      {
//            newstr +=
//replace(originalString.substring(i+txtLength,strLength),searchText,replaceText);
//
//
//      }
//      return newstr;
//}



function Continue(theForm)
{
	var xmlHttp;
	// Check for Login ID
		if (isBlank(theForm.LoginID.value))
		{
			alert("Login ID should not be Empty");
			theForm.LoginID.focus();
			return false;
		}

	// Check for Security Question
		if (theForm.SecurityQuestion.selectedIndex == 0)
		{
			alert("Please Select Security Question");
			theForm.SecurityQuestion.focus();
			return false;
		}

	// Check for Answer
		if (isBlank(theForm.ans.value))
		{
			alert("Answer should not be Empty");
			theForm.ans.focus();
			return false;
		}
	document.getElementById("status").style.display = "block";
	document.getElementById("status").innerHTML = "<b>Done</b>";
	document.getElementById("forgotpassword").style.display = "none";
	document.getElementById("changepassword").style.display = "block";

	/*xmlHttp = GetXmlHttpObject();

var OPass=encodeURIComponent(document.getElementById("CurrPassword").value);
var NPass=encodeURIComponent(document.getElementById("NewPassword").value);
var CPass=encodeURIComponent(document.getElementById("ConfPassword").value);
var HProf=encodeURIComponent(document.getElementById("hdnprofileid").value);
var parameters="CurrPassword="+OPass+"&NewPassword="+NPass+"&ConfPassword="+CPass+"&Profile_id="+HProf;
	xmlHttp.open("POST", "changepassword.asp", true);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(parameters);


if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
		alert(xmlHttp.responseText);
		document.getElementById("thewindowcontent").innerHTML = xmlHttp.responseText;
	}
}
*/
}
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}