function ResetValues( )
{
	var GDMatriForm = this.document.forms[ 0 ];

	GDMatriForm.MARITAL_STATUS.selectedIndex = 0;
	GDMatriForm.STAGE.value = "18";
	GDMatriForm.ENDAGE.value = "70";
	GDMatriForm.STHEIGHT.selectedIndex = 0;
	GDMatriForm.ENDHEIGHT.selectedIndex = 47;
	GDMatriForm.RELIGION.selectedIndex = 0;
	GDMatriForm.EDUCATION.selectedIndex = 0;
	GDMatriForm.CITIZENSHIP.selectedIndex = 0;
	GDMatriForm.COUNTRY.selectedIndex = 0;
	GDMatriForm.CASTE.value = "";
	GDMatriForm.DAYS[0].checked = 1;
	GDMatriForm.ST_DAY.selectedIndex = 0;
	GDMatriForm.ST_MONTH.selectedIndex = 0;
	GDMatriForm.ST_YEAR.selectedIndex = 0;
	GDMatriForm.DATE_OPT[0].checked = 1;
	GDMatriForm.DISPLAY_FORMAT[0].checked = 1;
	GDMatriForm.PHOTO_OPT.checked = 0;
	GDMatriForm.HOROSCOPE_OPT.checked = 0;
	GDMatriForm.IGNORE_OPT.checked = 0;

	return true;
}

function InitArray()
{
	this.length = InitArray.arguments.length
       for (var i = 0; i < this.length; i++)
        this[i+1] = InitArray.arguments[i]
}

function ValidateDate( Mon, Day, Year )
{
       Months = new InitArray(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
       var monthInt = Mon.options[Mon.selectedIndex].value;
       var dayInt = Day.options[Day.selectedIndex].value;
       var yearInt = Year.options[Year.selectedIndex].value;


      if ( monthInt < 1 || monthInt > 12 || dayInt < 1 || dayInt > 31 || yearInt < 1997 )
                   return ( false );

      if ( dayInt > Months[ monthInt ]  )
         if ( monthInt != 2 )
            return false;
         else if ( dayInt > 29 )
               return false;
         else if ( yearInt % 400 == 0 )
              return true;
         else if ( yearInt % 100 == 0 )
              return false;
         else if ( yearInt % 4 != 0 )
              return false;

        return true;
}

function ValidateNo( NumStr, String ) 
{
	for( var Idx = 0; Idx < NumStr.length; Idx ++ )
	{
		 var Char = NumStr.charAt( Idx );
		 var Match = false;

		for( var Idx1 = 0; Idx1 < String.length; Idx1 ++) 
		{
			 if( Char == String.charAt( Idx1 ) ) 
				 Match = true;
		}

		if ( !Match ) 
			return false;
	}

	return true;
}

// Function to validate all the inputs
function validate(  )
{
	var GDMatriForm = this.document.forms[ 0 ];
	var stAge = 0, endAge = 0;
 	if( GDMatriForm.STAGE.value == "" )
	{
	alert("Enter age range");
	GDMatriForm.STAGE.focus();
	return false;
	}
	else if( GDMatriForm.ENDAGE.value == "" )
	{
	alert("Enter age range");
	GDMatriForm.ENDAGE.focus();
	return false;
	}	
	if ( !GDMatriForm.GENDER[0].checked && !GDMatriForm.GENDER[1].checked)
	{
		alert( "Please select Gender" );
		GDMatriForm.GENDER[0].focus( );
		return false;
	}

	// Check Age 
	if ( GDMatriForm.STAGE.value != "" && 
		!ValidateNo( GDMatriForm.STAGE.value, "0123456789" ) )
	{
		alert("Invalid Age " + GDMatriForm.STAGE.value);
		GDMatriForm.STAGE.focus( );
		return false;
	}
	else if ( GDMatriForm.STAGE.value != "" )
	{
		stAge = parseInt( GDMatriForm.STAGE.value );
		if ( stAge < 18 || stAge > 99 )
		{
			alert( "Invalid Age " +  GDMatriForm.STAGE.value + ".  Minimum age allowed is 18 and maximum age is 99" );
			GDMatriForm.STAGE.focus( );
			return false;
		}
	}
		
	if( GDMatriForm.ENDAGE.value != "" &&
		!ValidateNo( GDMatriForm.ENDAGE.value, "0123456789" ) )
	{
		alert("Invalid Age " + GDMatriForm.ENDAGE.value);
		GDMatriForm.ENDAGE.focus( );
		return false;
	}
	else if ( GDMatriForm.ENDAGE.value != "" )
	{
		endAge = parseInt( GDMatriForm.ENDAGE.value );
		if ( endAge < 18 || endAge > 99 )
		{
			alert( "Invalid Age " +  GDMatriForm.ENDAGE.value + ".  Minimum age allowed is 18 and maximum age is 99" );
			GDMatriForm.ENDAGE.focus( );
			return false;
		}

		if ( stAge != 0 && endAge < stAge )
		{
			alert( "Invalid age range. " + stAge + " to " + endAge );
			GDMatriForm.STAGE.focus( );
			return false;
		}
	}

	if ( GDMatriForm.ENDHEIGHT.selectedIndex  < GDMatriForm.STHEIGHT.selectedIndex )
	{
		alert( "Invalid height range." );
		GDMatriForm.ENDHEIGHT.focus( );
		return false;
	}

/*
	if ( GDMatriForm.DAYS[1].checked && !ValidateDate( GDMatriForm.ST_MONTH, GDMatriForm.ST_DAY, GDMatriForm.ST_YEAR ) )
	{
		alert( GDMatriForm.ST_MONTH.options[GDMatriForm.ST_MONTH.selectedIndex].text +
		 " " + GDMatriForm.ST_DAY.options[GDMatriForm.ST_DAY.selectedIndex].text + 
		 " " + GDMatriForm.ST_YEAR.value + " is not a valid date" );
		GDMatriForm.ST_MONTH.focus( );
		return false;
	}
*/
	return true;
}

function savesearch( )
{
	if ( validate( ) == false )
		return;

	this.document.forms[0].action = "/cgi-bin/savesearch.cgi";
	this.document.forms[0].submit( );
}
