//File for JavaScript validation created by Amit on 17th Dec. 2001.
/*Following functions added
  MM_preloadImages - It loads the images.
  MM_swapImgRestore - It restores the previous image on mouse out event.	
  MM_findObj - It finds the object.
  MM_swapImage - It changes the image on mouse over event.
  isWhiteSpace - It checks for empty variables.
  isDayValid - It checks for the valid day for corresponding month.
  isEmail - It checks for the email address.
  validatePhoneFax - It checks the format of the phone number and validate the country code.
*/

/*File modified by mitesh on 18th dec 2001
  Following functions added
  isNumeric - Checks for numeric value (-ve,0,+ve,decimal)
  isInteger - Checks for integer value (0,numbers without sign,no decimal)
  isDigit - Checks for charcter as digit (0 to 9)
*/

/*File modified by mitesh on 24th Dec 2001
  Following functions were added
  swapImageOnClick - swaps images on click of object
 
*/ 

/*File modified by mitesh on 26th Dec 2001
  Following functions were added
  common variables to identify the browser(isIE4,isIE5 etc..) 
 
*/ 

/*File modified by Siddharth on 17th July 2002
  Following functions were added
  isNotTel to check for Physician Telephone in Medical History Popup
 
*/ 

/*File modified by Abhijeet on 26th Aug. 2003
  validatePhoneFax function is modified.
  Reference: Joe's e-mail on 25th Aug. 2003 subjected "Emergency fix"
*/ 

/***************Use ful falgs to identify browser***************************/

var isNav4, isIE4, isIE6;

//identifies netsacpe or Internet explorer
if (parseInt(navigator.appVersion.charAt(0)) >= 4) 
{
  isNav4 = (navigator.appName == "Netscape") ? true : false;
  isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
}

//identifies ns4 or ns6
if (isNav4 && parseInt(navigator.appVersion.charAt(0)) >= 5) 
{
  isNav6 = true;
}
else
{
 isNav6 = false;
}

/***************Use ful falgs to identify browser***************************/


/**************** useful strings that are used to carry out the validation*****/

var decimalPointDelimiter = "."
var NegativeSign = "-";
var PositiveSign = "+";
var whitespace = " \t\n\r";
var defaultEmptyOK = false;

/**************** useful strings that are used to carry out the validation*****/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
   
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_swapImgRestoreApp() { //v3.0
  var i,x,a=document.MM_srApp; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
  //alert( " Amit  " + a[i+1]);
  {if (x.src.indexOf(a[i+1]) < 0) x.src=x.oSrc;}
}

function MM_swapImageApp() { //v3.0
 
 var i,j=0,x,a=MM_swapImageApp.arguments; document.MM_srApp=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_srApp[j++]=x; document.MM_srApp[j++]=a[i+1]; if(!x.oSrc) x.oSrc=x.src; if(x.src.indexOf(document.MM_srApp[j-1]) < 0) x.src=a[i+2];}
  //y = document.MM_srApp[j-1];
  //alert( x.src.indexOf(y) + "  " + document.MM_srApp[j-1]);
  //x.src=a[i+2];
}

function isWhiteSpace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}  

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/*This function will check wheather the value is numeric or not.
  Returns true if numeric else false
*/
function isNumeric(val)
{
	if (isNaN(val))
	{ 
		return false;
	}
	else
	{
		 return true;
	}
}

/*This function will check wheather the value is integer or not.
  Returns true if interger else false
*/

function isInteger (s)
{   var i;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
		
    // All characters are numbers.
    return true;
}

/*
Checks wheather character is digit or not
*/
function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}


// Determine if the supplied day is valid.
//
function isDayValid(monthStr, dayStr, yearStr)
{
	var theMonth = parseInt(monthStr);
	var theDay   = parseInt(dayStr);
	var theYear  = parseInt(yearStr);

//	theDay = parseInt(dayStr);
//		var daysInMonth = getDaysInMonth(theMonth+1, theYear);
	var daysInMonth = getDaysInMonth(theMonth, theYear);
	
	if (theDay < 1 || theDay > daysInMonth)
	{
		alert("Invalid day in date, day must be between 1 and "+daysInMonth + ".");
		return false;
	}
	else
	{
		return true;
	}
}
	
// Get number of days in the month.
//
function getDaysInMonth(monthInt,yearInt)  
{
    var days;
		
    if (monthInt==1 || monthInt==3 || monthInt==5 || monthInt==7 || 
	    monthInt==8 || monthInt==10 || monthInt==12)  
	{
		days=31;
	}
    else if (monthInt==4 || monthInt==6 || monthInt==9 || monthInt==11) 
	{
		days=30;
	}
    else if (monthInt==2)  
	{
        if (isLeapYear(yearInt)) 
		{
            days=29;
        }
        else 
		{
            days=28;
        }
    }
    return (days);
}

// Check if the supplied year is a leap year.
//
function isLeapYear (yearInt) 
{
	if (((yearInt % 4)==0) && ((yearInt % 100)!=0) || ((yearInt % 400)==0)) 
	{
		return (true);
	}
	else 
	{
		return (false);
	}
}

function noSpaceBetween(s)
{   var i;

	i = s.indexOf(" ");
	if ((i < (s.length)) && (i >= 0))
		return false;
	else
		return true;
}

function isEmail (s)
{       
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function validatePhoneFax(pPhCountryCode,pPhCityCode,pPhoneNo,pExtn,pPhoneFlag,pHiddenDD)
{
	var validPhoneFax = true;
	var strPhoneFax;
	strPhoneFax = "";
	if (pPhoneFlag == true) 
		strPhoneFax = "Phone";
	else
		strPhoneFax = "Fax";
		
		
	if (pPhCountryCode != "" )
	{
		if (isNaN(pPhCountryCode) == true)
		{
			alert( strPhoneFax + " country code entered must be numeric.");
			return false;
		}
	}
	else
	{
		if ((pPhCityCode != "") || (pPhoneNo != "") || (pExtn != "" ))
		{	
			alert("Please enter the " + strPhoneFax + " country code.");
			return false;
		}
	}
	
	if (pPhCityCode != "")
	{
		if (isNaN(pPhCityCode)== true) 
		{
			alert( strPhoneFax + " area code entered must be numeric.");
			return false;
		}
	}
	/*else
	{
		if ((pPhCountryCode != "") || (pPhoneNo != "") || (pExtn != "" ))
		{		
			alert("Please enter the " + strPhoneFax + " area code.");
			return false;
		}
	}*/
		
	if (pPhoneNo == "")
	{
		if ((pPhCountryCode != "") || (pPhCityCode != "") || (pExtn != "" ))
		{	
			alert("Please enter the " + strPhoneFax + " number.");
			return false;
		}	
	}
	/* Commented this code by Abhijeet on 26th Aug. 2003 
	if (pPhCountryCode != "" )
	{
		//added to check for numeric country code 
		if (!isValidPhoneCode(pPhCountryCode,pHiddenDD)) 
		{	alert(strPhoneFax + " country code entered is not valid.");
			//validPhoneFax = false
			return false;
		}
	}
	*/
	return true;	
}

function isValidPhoneCode(pPhoneCode,pHiddenDD)
{	
	
	var blnFlag = false;
	var strDropDown = "document." + pHiddenDD;   
	
	for(intCtr = 0;intCtr < (pHiddenDD.length);intCtr++)
	{
		if (pPhoneCode == (pHiddenDD.options[intCtr].text))
		{
			blnFlag = true;
			break;		
		}
	}
	
	if (blnFlag == true) 
		return true;
	else
		return false;	
		
}

/*This function will swap images on click of the object
*/
function swapImageOnClick(id,imgNor,imgClicked)
{
	
	
	var currentSrc = MM_findObj(id).src;
	
	var tempStorageImgClicked =  imgClicked;
	lastIndexOfBS = tempStorageImgClicked.lastIndexOf("/");
	if (lastIndexOfBS != -1)
	{
		tempStorageImgClicked = tempStorageImgClicked.substring(lastIndexOfBS+1,tempStorageImgClicked.length);
	}
	
		
	if (currentSrc.indexOf(tempStorageImgClicked) != -1)
	{
		//MM_findObj(id).src = imgNor;
	}
	else
	{
		//MM_findObj(id).src = imgClicked;
	}
	
}


// Added by Siddharth Priya on 17th July 2003 for checking Physician Telephone for Medical History Popup
function isNotTel(strValue)
{
	var str = new String();
	str = strValue;
	for(i=0;i<str.length;i++)
	{		
		var strChar = str.substring(i,i+1);
		if	((strChar == " ") || (!isNaN(strChar)) || (strChar == ".") || (strChar == "-") || (strChar == "(") || (strChar == ")"))
			continue;
		else
			return true;
	}		
	return false;
}


