var js_randid; var version=0;
var action_path = "smartsearch";

if(navigator.appVersion.indexOf("MSIE")!=-1) {
	temp = navigator.appVersion.split("MSIE");
	version = parseFloat(temp[1]);
}

if(((parseInt(version) < 5.5) && js_br=="I") || js_br=="N" || js_br=="O" || js_br=="S") {
	action_path = "dosearch";
}

function genNumbers() {
  var i, rannum;

  for(i = 1000000; i < 1000010; i++) {
    rannum = Math.random()*1000000;
    rannum = Math.round(rannum);
  }
  var rand_flag = "r"+rannum+"s";
  return rand_flag;
}

function IsEmpty(obj, obj_type)
{
	if (obj_type == "text")	
	{
		var objValue;
		objValue = obj.value.replace(/\s+$/,"");	
			if (objValue.length == 0) 
			{
			return true;
			}
			else {return false;}
	}
}

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 < 2001 )
                                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 removearry()
{
	for (var i=0;i<document.MatriForm.RELIGION.length;) {
		if (document.MatriForm.RELIGION.options[i]) {
			document.MatriForm.RELIGION.remove(i);
		}
		else {
			i++;
		};
	};	
}

function removearry_caste()
{
	for (var i=0;i<document.MatriForm.CASTE.length;) {
		if (document.MatriForm.CASTE.options[i]) {
			document.MatriForm.CASTE.remove(i);
		}
		else {
			i++;
		};
	};	
}

function removearry_all()
{
			for (var i=0;i<document.MatriForm.RELIGION1.length;) {
				if (document.MatriForm.RELIGION1.options[i]) {
					document.MatriForm.RELIGION1.remove(i);
				}
				else {
					i++;
				};
			};	

			for (var j=0;j<document.MatriForm.CASTE1.length;) {
				if (document.MatriForm.CASTE1.options[j]) {
					document.MatriForm.CASTE1.remove(j);
				}
				else {
					j++;
				};
			};	
}

	function childlivingst()
	{	
		if ( MatriForm.MARITAL_STATUS1.checked)
		{
			document.MatriForm.MARITAL_STATUS0.checked=false;		
		}	
		else if ( MatriForm.MARITAL_STATUS2.checked)
		{
			document.MatriForm.MARITAL_STATUS0.checked=false;					
		}
		else if ( MatriForm.MARITAL_STATUS3.checked)
		{
			document.MatriForm.MARITAL_STATUS0.checked=false;					
		}	
		else if ( MatriForm.MARITAL_STATUS4.checked)
		{
			document.MatriForm.MARITAL_STATUS0.checked=false;					
		}				
		else if ( MatriForm.MARITAL_STATUS0.checked)
		{
			document.MatriForm.MARITAL_STATUS1.checked=false;					
			document.MatriForm.MARITAL_STATUS2.checked=false;					
			document.MatriForm.MARITAL_STATUS3.checked=false;											
			document.MatriForm.MARITAL_STATUS4.checked=false;														
		}				
	}
	
	function childlivingstany()
	{	
		if ( MatriForm.MARITAL_STATUS0.checked)
		{
			document.MatriForm.MARITAL_STATUS1.checked=false;					
			document.MatriForm.MARITAL_STATUS2.checked=false;					
			document.MatriForm.MARITAL_STATUS3.checked=false;
			document.MatriForm.MARITAL_STATUS4.checked=false;														
		}				
	}
		
	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 validate2()
	{
		var MatriForm = this.document.MatriForm;
		var FINALAGE = MatriForm.ENDAGE.value - MatriForm.STAGE.value;		
		var stAge = 0, endAge = 0;
	 	if( MatriForm.STAGE.value == "" )
		{
		alert("Enter age range");
		MatriForm.STAGE.focus();
		return false;
		}
		else if( MatriForm.ENDAGE.value == "" )
		{
		alert("Enter age range");
		MatriForm.ENDAGE.focus();
		return false;
		}	
		
		if ( FINALAGE > 20 )
		{
			alert("The difference between a partner's \"From\" and \"To\" age should not exceed 20 years.");
			MatriForm.ENDAGE.focus( );
			return false;
		}
				
		if ( !MatriForm.GENDER[0].checked && !MatriForm.GENDER[1].checked)
		{
			alert( "Please select Gender" );
			MatriForm.GENDER[0].focus( );
			return false;
		}

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

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

		if (!MatriForm.MARITAL_STATUS0.checked && !MatriForm.MARITAL_STATUS1.checked && !MatriForm.MARITAL_STATUS2.checked && !MatriForm.MARITAL_STATUS3.checked && !MatriForm.MARITAL_STATUS4.checked)
			{
				alert ("Please select the type of person you are looking For");
				MatriForm.MARITAL_STATUS0.focus();
				return false;
			}
		
		if ( MatriForm.ENDHEIGHT.selectedIndex  < MatriForm.STHEIGHT.selectedIndex )
		{
			alert( "Invalid height range." );
			MatriForm.ENDHEIGHT.focus( );
			return false;
		}

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

		return true;
	}

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{	
	var selLength = theSel.length;
	if(selLength>0)
	{
		theSel.options[theIndex] = null;
	}
}

function moveOptions(theSelFrom, theSelTo)
{
	
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;
	
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
	
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
	
	if(NS4) history.go(0);
}

function removearry_left(obj)
{
	for (var i=0;i<obj.length;) {
		if (obj.options[i]) {
			obj.remove(i);
		}
		else {
			i++;
		};
	};	

}

function single_save()
{
	if(IsEmpty(document.MatriForm.search_name,'text'))
	{
		alert("Please enter the search name for saving.");
		document.MatriForm.search_name.focus();
		return false;
	}
	else
	{
		validate();
	}
}

function multiple_save()
{
	if(IsEmpty(document.MatriForm.search_name,'text'))
	{
		alert("Please enter the search name for saving.");
		document.MatriForm.search_name.focus();
		return false;
	}
	else
	{
		document.MatriForm.but_save.value="1";
		validate3();
	}
}

function validate()
{
	var i;

	for(i=0;i<document.MatriForm.elements.length;i++)
	{
		if(document.MatriForm.elements[i].name=="LANGUAGE")
		{
			if (document.MatriForm.LANGUAGE.options[document.MatriForm.LANGUAGE.selectedIndex].value=="0")
			{alert ("Please select domain");document.MatriForm.LANGUAGE.focus();return false;}
		}
	}

	if (validate2()==true)
	{
		for(var i = 0;i < document.MatriForm.LANGUAGE.length;i++)
		{document.MatriForm.LANGUAGE.options[i].selected = true;}

		for(var i = 0;i < document.MatriForm.RELIGION1.length;i++)
		{document.MatriForm.RELIGION1.options[i].selected = true;}
		
		for(var i = 0;i < document.MatriForm.CASTE1.length;i++)
		{document.MatriForm.CASTE1.options[i].selected = true;}
		
		for(var i = 0;i < document.MatriForm.EDUCATION1.length;i++)
		{document.MatriForm.EDUCATION1.options[i].selected = true;}
	
		for(var i = 0;i < document.MatriForm.CITIZENSHIP1.length;i++)
		{document.MatriForm.CITIZENSHIP1.options[i].selected = true;}

		for(var i = 0;i < document.MatriForm.COUNTRY1.length;i++)
		{document.MatriForm.COUNTRY1.options[i].selected = true;}

		js_randid = genNumbers();
		document.MatriForm.randid.value = js_randid;
		document.MatriForm.bversion.value = js_br+"~"+version;
		
		var domain=document.MatriForm.jsdomain.value;
		document.MatriForm.action="http://"+domain+"/search/"+action_path+".php?t=R&randid="+js_randid;
		document.MatriForm.submit();
		return true;
	}
	else
	{return false;}
}

function validate3()
{
	var i;
	domain=document.MatriForm.jsdomain.value;
	
	for(i=0;i<document.MatriForm.elements.length;i++)
	{
		if(document.MatriForm.elements[i].name=="LANGUAGE")
		{
			if(document.MatriForm.LANGUAGE.options[document.MatriForm.LANGUAGE.selectedIndex].value==0) {
				alert("Please select Language.");
			}
		}
	}

	if (validate2()==true)
	{
		for(var i = 0;i < document.MatriForm.LANGUAGE.length;i++)
		{document.MatriForm.LANGUAGE.options[i].selected = true;}

		for(var i = 0;i < document.MatriForm.RELIGION1.length;i++)
		{document.MatriForm.RELIGION1.options[i].selected = true;}
		
		for(var i = 0;i < document.MatriForm.CASTE1.length;i++)
		{document.MatriForm.CASTE1.options[i].selected = true;}
		
		for(var i = 0;i < document.MatriForm.EDUCATION1.length;i++)
		{document.MatriForm.EDUCATION1.options[i].selected = true;}
	
		for(var i = 0;i < document.MatriForm.CITIZENSHIP1.length;i++)
		{document.MatriForm.CITIZENSHIP1.options[i].selected = true;}

		for(var i = 0;i < document.MatriForm.COUNTRY1.length;i++)
		{document.MatriForm.COUNTRY1.options[i].selected = true;}	
		
		js_randid = genNumbers();
		document.MatriForm.randid.value = js_randid;
		document.MatriForm.bversion.value = js_br+"~"+version;
		
		document.MatriForm.action="http://"+domain+"/search/"+action_path+".php?t=R&randid="+js_randid;
		document.MatriForm.submit();
		return true;
	}
	else
	{return false;}
}

function single_save()
{
	if(IsEmpty(document.MatriForm.search_name,'text'))
	{
		alert("Please enter the search name for saving.");
		document.MatriForm.search_name.focus();
		return false;
	}
	else
	{
		document.MatriForm.but_save.value="1";
		validate();
	}
}
