/*
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Module Name   : validateForm.js
'
'Description   : This script has all the common javscript functions used for the Cyprea Project
'
'Author		   : Maharajan S, AISL
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'						MODIFICATION HISTORY
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Name								When					Description
'
'Maharajan S			20/11/2001	    Created
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
*/

var isnn, isie	

/*
Function Name  : isValidDate

Description    : This function checks whether the given date is valid

Parameters	   : aobjDayCbo - Day ComboBox Object
				 aobjMonCbo - Month ComboBox Object	
				 aobjYearCbo - Year ComboBox Object

Returns		   : Boolean 		
*/
function isValidDate(aobjDayCbo,aobjMonCbo,aobjYearCbo)
{
	var lstrErrMsg;
	lstrErrMsg = "Please specify a valid date"
	var  dayValue , monValue , yearValue ; 

	dayValue = aobjDayCbo.options[aobjDayCbo.selectedIndex].value ; 
	monValue = aobjMonCbo.options[aobjMonCbo.selectedIndex].value ; 
	yearValue = aobjYearCbo.options[aobjYearCbo.selectedIndex].value ; 

	if (dayValue == null || dayValue == "")
	{
		alert ( lstrErrMsg ) ; 
		aobjDayCbo.focus() ; 
		return false ; 
	}

	if (monValue == null || monValue == "")
	{
		alert ( lstrErrMsg ) ; 
		aobjMonCbo.focus() ; 
		return false ; 
	}
	
	if (yearValue == null || yearValue == "")
	{
		alert ( lstrErrMsg ) ; 
		aobjYearCbo.focus() ; 
		return false ; 
	}
	
	if ((monValue == 4 || monValue == 6 || monValue == 9 || monValue == 11) && (dayValue > 30))
	{
		alert(lstrErrMsg)
		aobjDayCbo.focus();
		return false;
	}

	if ( monValue == 2 )
	{
		if ( ( yearValue % 4 ) == 0 )
		{
			if (dayValue > 29 )
			{
				alert(lstrErrMsg) ; 
				aobjDayCbo.focus();
				return false;
			}
		}
		else
		{
			if (dayValue > 28 )
			{
				alert(lstrErrMsg) ; 
				aobjDayCbo.focus();
				return false;
			}
		}
	}
return true ;
}


/*
Function Name  : isValidEmailAddr

Description    : This function checks whether the given email address is a valid email address

Parameters	   : astrAddr - The Address to be checked

Returns		   : Boolean 		
*/

function isValidEmailAddr(astrAddr)
{
	var atPosition , dotPosition ;
	var atFPosition , dotFPosition ;
	var intAtDotDiff, afterDotPosition ; 
	var lobjStr;

	lobjStr     = new String(trim(astrAddr));			
	atFPosition  = lobjStr.indexOf("@") ; 
	atPosition  = lobjStr.lastIndexOf("@") ; 
	dotFPosition = lobjStr.indexOf(".") ; 
	dotPosition = lobjStr.lastIndexOf(".") ; 
	intAtDotDiff = dotPosition - atPosition ;
	afterDotPosition =  new String(lobjStr.substr(dotPosition + 1 ,2));

	if ((dotPosition == lobjStr.length-1) || (atPosition == lobjStr.length-1)  ||
		(atFPosition <= 0) || (dotFPosition == -1) ||
		(intAtDotDiff < 2) ||	(atFPosition != atPosition) ||
		(afterDotPosition.length < 2 ) || (atFPosition - dotFPosition) == 1)
	{
		alert ( "Please Enter Valid E-Mail Address. \n Email Address should be of the following format - \n someone@someserver.com" ) ; 
		return false ; 
	}
return true ;
}


/*
Function Name  : trim

Description    : This function trims the leading white spaces ina String
				 
Parameters	   : astrString - String to be trimmed

Returns		   : String - The trimmed String 		

*/
function trim(astrString)
{
	while(astrString.substring(0,1) == ' ')
		astrString = astrString.substring(1);
	return astrString;
}


function dateDiff(astrStartDate,astrEndDate)
{
	var lArrStartDate;
	var lobjStartDate;
	var lobjFromDate;
	var lArrEndDate;
	var lobjEndDate;
	var lobjToDate;
	var llngDiff;
	var lintDayDiff;
		 
	lobjStartDate = new String(astrStartDate);
	lArrStartDate = lobjStartDate.split("/");
	lobjFromDate = new Date(lArrStartDate[2],lArrStartDate[1],lArrStartDate[0],00,00,00);
	
	lobjEndDate = new String(astrEndDate);
	lArrEndDate = lobjEndDate.split("/");
	lobjToDate = new Date(lArrEndDate[2],lArrEndDate[1],lArrEndDate[0],00,00,00);

	llngDiff = lobjToDate - lobjFromDate;
	lintDayDiff = llngDiff/86400000;
	return lintDayDiff;
}


/*
Function Name  : showMsg

Description    : This function shows the given contents in the status bar
				 
Parameters	   : astrMsg - Message text to be shown

Returns		   : None
*/
function showMsg(astrMsg)
{
	self.status = astrMsg;
}


/*
Function Name  : clearMsg

Description    : This function clears the contents in the status bar
				 
Parameters	   : None

Returns		   : None
*/
function clearMsg()
{
	window.defaultStatus = '';
}


/*
Functn Name : locateObject()

Description : This function returns the location of the given object in the browser
						: This function is called from the ToolTip functions
 				 
Parameters  : n - Object's Name
					  : d - Document object

Returns		  : x - Position of the object in the browser window
*/
function locateObject(n, d) {
  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=locateObject(n,d.layers[i].document); return x;
}


/*
Function Name  : hideTooltip()

Description    : This function hides the tool tip shown
			   
Parameters	   : object - Tooltip object's name - Usually the name of the <Div> tag

Returns		   : None
*/
function hideTooltip(object)
{
	if (document.all)
	{
		locateObject(object).style.visibility="hidden";
		locateObject(object).style.left = 1;
		locateObject(object).style.top = 1;
		return false
	}
	else if (document.layers)
	{
		locateObject(object).visibility="hide"
		locateObject(object).left = 1;
		locateObject(object).top = 1;
		return false
	}
	else
		return true
}


/*
Function Name  : showTooltip()

Description    : This function shows the  tool tip 
			   
Parameters	   : object - Tooltip object's name - Usually the name of the <Div> tag
		         e - Event
				 TipContent - Message to be shown on the Tooltip
				 backColor - Background color of the tool tip
				 borderColor - border color of the tool tip
				 textColor - Text of the tool tip text
				 displaytime - Number of milliseconds the Tooltip will be displayed
Returns		   : None
*/

function showTooltip(object,e, tipContent, backcolor, bordercolor, textcolor, displaytime)
{
	var tipTimer;
	window.clearTimeout(tipTimer)
	
	if (document.all)
		{
			locateObject(object).style.top=document.body.scrollTop+event.clientY+20;
			
			locateObject(object).innerHTML='<table style="font-family: Arial, Helvetica; font-size: 8px; border: '+bordercolor+'; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; background-color: '+backcolor+'" width="10" border="0" cellspacing="1" cellpadding="1"><tr><td nowrap><font style="font-family: Arial, Helvetica; font-size: 10px; color: '+textcolor+'">'+unescape(tipContent)+'</font></td></tr></table> '

			if ((e.x + locateObject(object).clientWidth) > (document.body.clientWidth + document.body.scrollLeft))
				{	
					locateObject(object).style.left = (document.body.clientWidth + document.body.scrollLeft) - locateObject(object).clientWidth-10;
				}
			else
				{
				locateObject(object).style.left=document.body.scrollLeft+event.clientX
				}

			locateObject(object).style.visibility="visible"
//			tipTimer=window.setTimeout("hideTooltip('"+object+"')", displaytime);
		tipTimer=window.setTimeout("hideTooltip('"+object+"')", 15000);
		}
	else if (document.layers)
		{
		locateObject(object).document.write('<table width="10" border="0" cellspacing="1" cellpadding="1"><tr bgcolor="'+bordercolor+'"><td><table width="10" border="0" cellspacing="0" cellpadding="2"><tr bgcolor="'+backcolor+'"><td nowrap><font style="font-family: Arial, Helvetica; font-size: 10px; color: '+textcolor+'">'+unescape(tipContent)+'</font></td></tr></table><td></tr></table>')
		locateObject(object).document.close()
		locateObject(object).top=e.y+20

		if ((e.x + locateObject(object).clip.width) > (window.pageXOffset + window.innerWidth))
			{
				locateObject(object).left = window.innerWidth - locateObject(object).clip.width-10;
			}
		else
			{
			locateObject(object).left=e.x;
			}
		locateObject(object).visibility="show"
		//tipTimer=window.setTimeout("hideTooltip('"+object+"')", displaytime);
		tipTimer=window.setTimeout("hideTooltip('"+object+"')", 15000);
	}
	else
		return true
}


/*
Function Name  : GoBack
Description    : This function takes the visitor to the previously visited page
Parameters	   : None
Returns		   : None
*/
function goBack()
{
	history.go(-1);
}


/*
Function Name  : selectAll()

Description    : This function selects all the checkboxes in  a form
			   
Parameters	   : aobjFrm - Form object

Returns		   : None
*/
function selectAll(aobjFrm)
{

	for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
		{
			if ((aobjFrm.elements[lintCount].type) == "checkbox")
			{
				aobjFrm.elements[lintCount].checked = true;
			}
		}
}


/*
Function Name  : deselectAll()

Description    : This function selects all the checkboxes in  a form
			   
Parameters	   : aobjFrm - Form object
			   
Returns		   : None
*/
function deselectAll(aobjFrm)
{

	for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
		{
			if ((aobjFrm.elements[lintCount].type) == "checkbox")
			{
				aobjFrm.elements[lintCount].checked = false;
			}
		}
}


/*
Function Name  : isCheckBoxSelected

Description    : This function checks whether atleast one check box is selected.
 				 
Parameters	   : aobjFrm - Form object

Returns		   : None
*/
function isCheckBoxSelected(aobjFrm)
{
	var blnIsChecked = false;
	var intIndex = 0 ;

	for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
	{
		if ((aobjFrm.elements[lintCount].type == "checkbox") && (intIndex == 0))
		{
			intIndex = lintCount ;
		}

		if ((aobjFrm.elements[lintCount].type == "checkbox") && (aobjFrm.elements[lintCount].checked))
		{
			blnIsChecked = true;
		}
	}

	if (!blnIsChecked)
	{
			alert("Atleast One checkbox Should be Selected to perform the Operation")
			aobjFrm.elements[intIndex].focus();
			return false;
	}

	return true;
}


/*
Function Name  : showCalender

Description    : This function shows the calendar control on the browser
    				 
Parameters	   : None
			   
Returns		   : None
*/
function showCalender()
{
	var DatePickerWindow = new Object();
	strOptions = 'toolbar=no,width=175,height=185,dependent=yes,titlebar=No,left=300,top=200,resizable=No';
	DatePickerWindow = window.open("./../Include/popcjs.htm", "Help",strOptions);}


function key(k)   
{
	if(isie) {
		if(event.keyCode==17 || event.keyCode==18 || event.keyCode==93) {
			alert("Sorry, you do not have permission to press this key.") 
			return false;
		 } 
	}

	if(isnn){
		alert("Sorry, you do not have permission to press this key.") 
		return false; }   
}


function right(e) //to trap right click button 
{
	if (isnn && (e.which == 3 || e.which == 2 ))
		return false;
	else if (isie && (event.button == 2 || event.button == 3)) 
	{
		alert("Sorry, you do not have permission to right click on this page.");
		return false;
	}
		return true;
}


/*
Function Name  : isLaterDate

Description    : This function checks whether the date specified is in future
    				 
Parameters	   : astrDay - Day Value in the date
				 astrMon - Month value in the Date
			     astrYear - Year Value in the Date

Returns		   : Boolean - True if the date is in future
				 False on failure
*/
function isLaterDate(astrDay,astrMon,astrYear)
{
		var intDayDiff;
		var lstrToday;
		var lstrExpDay;
		var lobjToday;

		lobjToday = new Date();
		lstrToday = lobjToday.getDate() + "/" + (lobjToday.getMonth()+1) + "/" + lobjToday.getYear();

		lobjExp = new Date(astrYear,astrMon,astrDay);
		lstrExpDay = lobjExp.getDate() + "/" + lobjExp.getMonth() + "/" + lobjExp.getYear();
		intDayDiff = dateDiff(lstrToday,lstrExpDay);
		if (intDayDiff < 0)
		{
			return false;
		}
		return true;
}


//check wheather To date is greater than from Date
function isValidFromToDate(astrDay,astrMon,astrYear,aendDay,aendMon,aendYear)
{
	var intDayDiff ; 
	var lobjFromDate ; 
	var lobjToDate ; 
	var lstrFromDate ; 
	var lstrToDate ; 
						
	lobjFromDate = new Date( astrYear , astrMon , astrDay) ; 
	lstrFromDate = lobjFromDate.getDate() + "/" + lobjFromDate.getMonth() + "/" + 			
						 lobjFromDate.getYear();

	lobjToDate = new Date ( aendYear , aendMon , aendDay ) ; 
	lstrToDate = lobjToDate.getDate() + "/" + lobjToDate.getMonth() + "/" + lobjToDate.getYear();
	
	intDayDiff = dateDiff(lstrFromDate,lstrToDate) ; 
	if ( intDayDiff < 0 )
	{
		return false ; 
	}		
return true ; 
}


function isNumber(astrTxt)
{
	var getNumber; 
	var notNumeric; 
	getNumber = astrTxt ; 
	
	notNumeric= isNaN(getNumber) ; 
	if ( notNumeric )
	{
		alert ( "Please Enter Numeric Value" ) ; 
		return false ; 
	}
	else
	{
		if ( getNumber < 0 )
		{
			alert ( "Please Enter a Value Greater than or Equal to Zero" ) ; 
			return false ; 
		}
	}

	if ( getNumber != Math.ceil(getNumber) )
	{
		alert ( "Please enter the number in full.\n Do not enter values in decimal." ) ; 
		return false ; 
	}	

return true ; 
}


function isNumberAndZero(astrTxt)
{
	var getNumber; 
	var notNumeric; 
	getNumber = astrTxt ; 
	notNumeric= isNaN(getNumber) ; 
	if ( notNumeric )
	{
		alert ( "Please Enter Numeric Value" ) ; 
		return false ; 
	}
	else
	{
		if ( getNumber <= 0 )
		{
			alert ( "Please Enter a Value Greater than Zero" ) ; 
			return false ; 
		}
	}
	if ( getNumber != Math.ceil(getNumber) )
	{
		alert ( "Please enter the number in full.\n Do not enter values in decimal." ) ; 
		return false ; 
	}	
return true ; 
}

	
function isNumbers(aobjFrm)
{
		var isTrue ; 

			for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
			{
				switch (aobjFrm.elements[lintCount].type)
				{
					case "text" :

						if ( aobjFrm.elements[lintCount].value != "" )
						{
							isTrue = isNaN(aobjFrm.elements[lintCount].value) ; 
								if (!isTrue)
								{
									if (aobjFrm.elements[lintCount].value <= 0)
									{
										alert("Enter a value greater than zero") ; 
										aobjFrm.elements[lintCount].value = "" ; 
										aobjFrm.elements[lintCount].focus() ; 
										return false ; 
									}
								}
								else
								{
										alert("Enter a numeric value") ; 
										aobjFrm.elements[lintCount].value = "" ; 
										aobjFrm.elements[lintCount].focus() ; 
										return false ; 
								}
						}
				}
			}
	return true ; 
}


function validateForm(aobjFrm)
{
	var blnStatus = true; 
	var lintCount ; 
	var strMsg = "You have not filled a required field.\n Please note that fields marked with * are required.\n Please fill those fields and try again."

	for ( lintCount = 0 ; lintCount < aobjFrm.elements.length - 1 ; lintCount ++ )
	{
		switch (aobjFrm.elements[lintCount].type)
		{
			case "file":
			case "password":
			case "text":
			case "textarea":
			{ 
				if ( String((aobjFrm.elements[lintCount].name).substr(0,3)) == "man" ) 
				{
					 if(trim(aobjFrm.elements[lintCount].value) == null  || trim(aobjFrm.elements[lintCount].value) == "")
						 {
						alert(strMsg)						
						aobjFrm.elements[lintCount].value = "" ; 
						aobjFrm.elements[lintCount].focus();
						return false;
							 }
				}
				break;
			}

			case "checkbox":
			case "radio":
			{
				if ( String((aobjFrm.elements[lintCount].name).substr(0,3)) == "man" ) 
				{
					 if(aobjFrm.elements[lintCount].checked == false)
						{
						alert(strMsg)							
						aobjFrm.elements(lintCount).focus();
						return false;
					}
				}
				break;
			}

			case "select-one":
			{
				if ( String((aobjFrm.elements[lintCount].name).substr(0,3)) == "man" ) 
				{
					if(aobjFrm.elements[lintCount].selectedIndex == 0)
					{
						alert(strMsg)							
						aobjFrm.elements[lintCount].focus();
						return false;
					}
				}
				break;
			}

			case "select-multiple":
			{
				if ( String((aobjFrm.elements[lintCount].name).substr(0,3)) == "man" ) 
				{
					var isSelected = false ; 

					for ( i = 0 ; i < aobjFrm.elements[lintCount].length - 1 ; i ++ )
					{
						if( aobjFrm.elements[lintCount].options[i].selected == true )
						{
							isSelected = true ; 
							break ; 
						}
					}

					if (!isSelected)
					{
						alert(strMsg)							
						aobjFrm.elements[lintCount].focus() ; 
						return false ; 
					}
					return isSelected ; 
				}
				break;
			}

		} // End of Switch
	} // End of For

	
	for ( lintCount = 0 ; lintCount < aobjFrm.elements.length - 1 ; lintCount ++ )
	{
		if ( (String(aobjFrm.elements[lintCount].value).indexOf("'")) != -1 )
		{
			alert ( "Enter Value witbout using single Quotes" ) ; 
			aobjFrm.elements[lintCount].focus() ; 
			return false ; 
		}
	}

return blnStatus ; 
}


function checkMail(aobjFrm)
{
	var TEXTBOX = "text" ; 
	var a ; 
	for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
	{
		    switch(aobjFrm.elements[lintCount].type)
			{
				case "text":
				{
					if ( String((aobjFrm.elements[lintCount].name).substr(0,3)) == "man" ) 
					{
						//alert(String((aobjFrm.elements[lintCount].name).substr((String(aobjFrm.elements[lintCount].name).length-4),4))) ; 
						if ( (String((aobjFrm.elements[lintCount].name).substr((String(aobjFrm.elements[lintCount].name).length-4),4))) == "mail" )
						{
							blnStatus = isValidEmailAddr(aobjFrm.elements[lintCount].value) ; 
							if(!blnStatus)
							{
								aobjFrm.elements[lintCount].focus() ; 
								return false ; 
							}
						}
					}
				}
			}
	}
return true ; 
}


function checkTextArea(aobjFrm)
{
	var TEXTAREA = "textarea" ; 
	var lintCount ; 
	
	for(lintCount = 0;lintCount < aobjFrm.elements.length-1; lintCount++)
	{
		    switch (aobjFrm.elements[lintCount].type)
			{
				case "textarea":
				{
					if ( (String(aobjFrm.elements[lintCount].value).length) > ((String((aobjFrm.elements[lintCount].name).substr((String(aobjFrm.elements[lintCount].name).indexOf("area") + 4),(String(aobjFrm.elements[lintCount].name).length)-(String(aobjFrm.elements[lintCount].name).indexOf("area") + 4))))) )
					{ 
						alert ( "Enter less than " + ((String((aobjFrm.elements[lintCount].name).substr((String(aobjFrm.elements[lintCount].name).indexOf("area") + 4),(String(aobjFrm.elements[lintCount].name).length)-(String(aobjFrm.elements[lintCount].name).indexOf("area") + 4))))) + " characters " ) ; 
						aobjFrm.elements[lintCount].focus() ; 
						return false ; 
					}
			    }
		   }	
	}
return true ; 
}


function isDot(astrValue)
{
	if( String(astrValue).indexOf(".") != -1 )
	{
		alert ( "Please enter the number in full.\n Do not enter values in decimal." ) ; 
		return false ; 
	}
return true ; 
}


function showHelp(astrType)
{
			var strOptions;
     		strOptions = 'toolbar=no,width=600,height=500,dependent=yes,titlebar=no,left=100,top=20,resizable=yes,screenX=500,screenY=200,titlebar=no';


			if (astrType=="guest")
			{
					window.open('./../Help/Guest/Index.html','CypreaAssistant',strOptions);
					return;
			}
			if (astrType=="ro")
			{
					window.open('./../help/Rowner/Index.html','CypreaAssistant',strOptions);
					return;
			}
			if (astrType=="to")
			{
					window.open('./../help/Tourop/Index.html','CypreaAssistant',strOptions);
					return;
			}			
}


function showOriginalPicture(astrFileName)
{
	var objImg;
	var strOptions;
	var FileName;

	objImg = new Image();
	objImg.src = astrFileName;
	FileName = "./../Common/ShowOriginalPicture.asp?ImageName=" + astrFileName;
	strOptions = 'toolbar=no,width='+objImg.width+',height='+objImg.height+',dependent=yes,titlebar=No,left=20,top=20,resizable=No';
	OriginalImageWindow = window.open(FileName,"OriginalImage",strOptions);
}

