function casteSelection (theForm) {

  if (window.document.confirmMatrimForm.Caste.options[window.document.confirmMatrimForm.Caste.selectedIndex].text != "OTHER")
  {
    window.document.confirmMatrimForm.CasteOther.value = '';
  }
  else {
    window.document.confirmMatrimForm.CasteOther.focus();
  }
}
function otherEnable (theSelectedElement, theFormElementOther) {

  theFormElementOther.disabled = false;
  if (theSelectedElement != "Other")
  {
  	theFormElementOther.value = '';
 	theFormElementOther.disabled = true;
  }
}

function otherEnableMultiple (theSelectedElement, theFormElementOther) {

  	theFormElementOther.disabled = false;

	var loop=0;
	var foundOther = 0;
    for (loop = 0; loop < theSelectedElement.options.length; loop++){
		if (theSelectedElement.options[loop].selected)
		{
			var loopText = theSelectedElement.options[loop].text.toUpperCase();
			if (loopText == "OTHER") {
				var foundOther = 1;
				break;
			}
		}
    }
	if (foundOther == 0) {
  		theFormElementOther.value = '';
  		theFormElementOther.disabled = true;
	}
}

function isOtherSelected (theSelectedElement, theFormElementOther) {

	var loop=0;
	var otherSelected = 0;
    for (loop = 0; loop < theSelectedElement.options.length; loop++){
		if (theSelectedElement.options[loop].selected)
		{
			var loopText = theSelectedElement.options[loop].text.toUpperCase();
			if (loopText == "OTHER") {
				var otherSelected = 1;
				break;
			}
		}
    }
	if (otherSelected == 0) {
		return (false);
	}
	return (true);
}

// If a user selects a specific brahmin caste and not the general brahmin caste, we select the
// general brahmin for him as well
function selectBrahmin (theForm) {

	var specBrahminSelected = 0;
	var brahminPos;
    var brahminSelected = 0;

	// Loop through the caste list to see if a specific brahmincaste is selected and the
	// general caste is not selected.  While at it determin the postion of the general brahmin caste
	// as we need it to select the item if needed
	for (var i = 0; i < theForm.PCaste.length; i++){

		if (theForm.PCaste.options[i].selected == true) {
			if (theForm.PCaste.options[i].text.match(/Brahmin-/)) { // Is a specific brahmin caste selected ?
				specBrahminSelected = 1;
				continue; // Continue on, as the specific brahmin caste will also match the next if statement
			}

			// The following if stament must be after the preceding if, as it is meant only for general barhmin caste
			if (theForm.PCaste.options[i].text.match(/Brahmin/)) { // Is a general brahmin caste selected ?
				brahminSelected = 1;
			}
		}

		// Record the position of the general brahmin caste, but not of specific ones
		if (theForm.PCaste.options[i].text.match(/Brahmin/) && !theForm.PCaste.options[i].text.match(/Brahmin-/)) {
			brahminPos = i;
		}
	}

	// Alert user
	if (specBrahminSelected && !brahminSelected) {
		alert ('You have selected a specific Brahmin caste.  Specific Brahmin caste have been introduced recently.  Therefore, some profiles posted previously under general Brahmin may come under your caste.  We will therefore automatically added general Brahmins to your search criteria. ');
		theForm.PCaste.options[brahminPos].selected = true;
	}
}

function matrimScreenCheck (theForm, update)
{

  if (theForm.PostedBy.options[theForm.PostedBy.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Posted By\".");
    theForm.PostedBy.focus();
    return (false);
  }

  if (theForm.Language.options[theForm.Language.selectedIndex].text == "Pick One")
  {
    alert("Need to specify the \"Mother Tongue\".");
    theForm.Language.focus();
    return (false);
  }
  if (! theForm.Age.value || theForm.Age.value.match(/\D/))
  {
    alert("Need to enter valid number in the \"Age\" field.");
    theForm.Age.focus();
    return (false);
  }

  if (theForm.Age.value < 18 || theForm.Age.value > 55)
  {
    alert("We are sorry, but Age need to be between 18 and 55.  If you are not above 18 or below 55 you cannot register");
    theForm.Age.focus();
    return (false);
  }
  if (theForm.BirthDay.selectedIndex > 0 || theForm.BirthMonth.selectedIndex > 0 || theForm.BirthYear.selectedIndex > 0) {
    if (! isValidDate (theForm.BirthDay.options[theForm.BirthDay.selectedIndex].value, theForm.BirthMonth.options[theForm.BirthMonth.selectedIndex].value - 1, theForm.BirthYear.options[theForm.BirthYear.selectedIndex].value)) {
      alert("It appears that birth date provided by you is not a valid one.");
      theForm.BirthDay.focus();
      return (false);
	}
  }
  if (theForm.Height.options[theForm.Height.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Height\".");
    theForm.Height.focus();
    return (false);
  }
  if (theForm.Complexion.options[theForm.Complexion.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Complexion\".");
    theForm.Complexion.focus();
    return (false);
  }
  if (theForm.Build.options[theForm.Build.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Build\".");
    theForm.Build.focus();
    return (false);
  }
  if (theForm.PhysicalStatus.options[theForm.PhysicalStatus.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"PhysicalStatus\".");
    theForm.PhysicalStatus.focus();
    return (false);
  }

  if (theForm.Religion.options[theForm.Religion.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Religion\".");
    theForm.Religion.focus();
    return (false);
  }

  if (theForm.Caste.options[theForm.Caste.selectedIndex].text == "Pick One")
  {
    alert("You need to select a \"Caste\". If your exact caste is not on the list, select the one that best matches your caste. You may specify the exact caste in the Sub Caste field.  You may also contact help desk, if you need your caste to be included in the main caste list");
    theForm.Caste.focus();
    return (false);
  }
  if (theForm.CasteMatch.options[theForm.CasteMatch.selectedIndex].text == "Pick One" || theForm.CasteMatch.options[theForm.CasteMatch.selectedIndex].text == "--")
  {
    alert("Need to pick the \"Caste Match\". Specify, if you need a strict caste match, or a preferred match or no bar");
    theForm.CasteMatch.focus();
    return (false);
  }
  if (theForm.HoroscopeMatch.options[theForm.HoroscopeMatch.selectedIndex].text == "Pick One"  || theForm.HoroscopeMatch.options[theForm.HoroscopeMatch.selectedIndex].text == "--")
  {
    alert("Need to pick the \"Horoscope Match\". Specify, if you need a strict Horoscope match, or a preferred match or match not necessary");
    theForm.HoroscopeMatch.focus();
    return (false);
  }
/*
  if (theForm.Caste.options[theForm.Caste.selectedIndex].text == "OTHER" && !theForm.CasteOther.value)
  {
    alert("Please specify your caste in the box for other castes.");
    theForm.CasteOther.focus();
    return (false);
  }

  if (theForm.Caste.options[theForm.Caste.selectedIndex].text != "OTHER" && theForm.CasteOther.value)
  {
    alert("You have specified both \"Caste & Caste (Other)\". Please specify only one of the two.");
    theForm.CasteOther.focus();
    return (false);
  }
*/

/*
  var checkOK = "0123456789-";
  var checkStr = theForm.Age.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters (no spaces) in the \"Age\" field.");
    theForm.Age.focus();
    return (false);
  }
*/


  if (theForm.CurRes.options[theForm.CurRes.selectedIndex].text == "Pick One")
  {
    alert("Need to pick the \"Current Residence\".");
    theForm.CurRes.focus();
    return (false);
  }

  if (update == "N") {
    if (theForm.MaritalStatus.selectedIndex == 2 || theForm.MaritalStatus.selectedIndex == 4 || theForm.MaritalStatus.selectedIndex == 6)
    {
      if (!theForm.NoOfChildren.value || theForm.NoOfChildren.value.match(/\D/)) {
        alert("Please specify number of children.");
        theForm.NoOfChildren.focus();
        return (false);
      }
    }
  }
/*
  if (theForm.Language.options[theForm.Language.selectedIndex].text == "Other" && !theForm.LangOther.value)
  {
    alert("Need to specify the \"Language (Other)\".");
    theForm.LangOther.focus();
    return (false);
  }
  if (theForm.Language.options[theForm.Language.selectedIndex].text != "Other" && theForm.LangOther.value)
  {
    alert("You have specified both \"Language & Language (Other)\". Please specify only one of the two.");
    theForm.LangOther.focus();
    return (false);
  }
*/

// Education Check
var hasEducation = 0;
var i;
for (i = 0;  i < theForm.Education.length;  i++)
{
 	if (theForm.Education.options[i].selected) {
		hasEducation++;
		break;
	}
}
if (!hasEducation)
{
	alert("Please select at least 1 \"Education\".  If your specific education is not on the list, please select the one that is closest to your degree.  You may provide the your specific degree in the Specific Education field.");
	theForm.Education.focus();
	return (false);
}
// End of Education check

// FieldOfStudy Check
var hasFieldOfStudy = 0;
var i;
for (i = 0;  i < theForm.FieldOfStudy.length;  i++)
{
 	if (theForm.FieldOfStudy.options[i].selected) {
		hasFieldOfStudy++;
		break;
	}
}
if (!hasFieldOfStudy)
{
	alert("Please select at least 1 \"Field Of Education\".  If your field of education is not on the list, please select the one that is closest to your field of education.  Once you complete registration, you may contact Help Desk to include your field of education in the list.");
	theForm.FieldOfStudy.focus();
	return (false);
}
// End of FieldOfStudy check

if (theForm.Profession.options[theForm.Profession.selectedIndex].text == "Pick One")
{
    alert("Need to pick the \"Profession\".  If your profession is not listed, select the one that is closest to your profession and enter your exact profession in the Specific Profession field.  If you wish to have your profession listed in main category, please contact help desk for consideration");
    theForm.Profession.focus();
    return (false);
}
if (theForm.Income.options[theForm.Income.selectedIndex].text == "Pick One")
{
    alert("Please select a Yearly Income Range. This helps others finding someone like you.  If you have no income or you are not working, you may select Not Applicable.");
    theForm.Income.focus();
    return (false);
}

if (theForm.SpecificIncome.value.length > 0) {
	if (theForm.Income.options[theForm.Income.selectedIndex].text == "Pick One") {
		alert("Since you have provided a specific income, you also need to pick Yearly Income Level applicable to you.");
    	theForm.Income.focus();
    	return (false);
	}
	//if ( (theForm.SpecificIncome.value.search(/^[,]/)) || (theForm.SpecificIncome.value.search(/^[,\d]/)) ) {
	if (theForm.SpecificIncome.value.match(/[^,\d]/)) {
		alert("For specific income, please use only digits and commas.  For example, 500000 or 5,00,000 is correct.\n\n 5 lacs or 5 lakhs or 500000/- would be wrong.\n\nIf you think you entered the figure correctly and still getting this warning, check for extra spaces.");
    	theForm.SpecificIncome.focus();
    	return (false);
	}
}

/*
if (theForm.Profession.options[theForm.Profession.selectedIndex].text == "Other" && !theForm.ProfOther.value)
{
    alert("Need to specify the \"Profession (Other)\".");
    theForm.ProfOther.focus();
    return (false);
}

if (theForm.Profession.options[theForm.Profession.selectedIndex].text != "Other" && theForm.ProfOther.value)
{
    alert("You have specified both \"Profession & Profession (Other)\". Please specify only one of the two.");
    theForm.ProfOther.focus();
    return (false);
}
*/

/*
if (theForm.profile.value.length == 0) {
    var profMsg = "Please provide a few words about the bride/groom (at least 40 characters). A good description will get the attention of others who are looking for a profile like yours.";
    alert(profMsg);
    theForm.profile.focus();
    return (false);
}
if (theForm.profile.value.length < 40) {
    var profMsg = "Your description about the bride/groom is too short.  Please provide at least 40 characters. You currently have " + theForm.profile.value.length + " characters";
    alert(profMsg);
    theForm.profile.focus();
    return (false);
}
*/
if (theForm.profile.value.length > 600) {
    var profMsg = "Please limit the description about the bride/groom to a maximum of 600 characters. You currently have " + theForm.profile.value.length + " characters";
    alert(profMsg);
    theForm.profile.focus();
    return (false);
}
/*
if (theForm.FamilyBackground.value.length == 0) {
    var profMsg = "Please provide a few words about the family (at least 40 characters). A good description will get the attention of others who are looking for a profile like yours.";
    alert(profMsg);
    theForm.FamilyBackground.focus();
    return (false);
}
if (theForm.FamilyBackground.value.length < 40) {
    var profMsg = "Your description about the family is too short.  Please provide at least 40 characters. You currently have " + theForm.FamilyBackground.value.length + " characters";
    alert(profMsg);
    theForm.FamilyBackground.focus();
    return (false);
}
*/
if (theForm.FamilyBackground.value.length > 600) {
    var profMsg = "Please limit the description about the family to a maximum of 600 characters. You currently have " + theForm.FamilyBackground.value.length + " characters";
    alert(profMsg);
    theForm.FamilyBackground.focus();
    return (false);
}
/*
if (theForm.Advertisement.value.length == 0) {
    var profMsg = "Please provide heading for your profile in a few words (at least 40 characters). A good heading will get the attention of others who are looking for a profile like yours.";
    alert(profMsg);
    theForm.Advertisement.focus();
    return (false);
}
if (theForm.Advertisement.value.length < 40) {
    var profMsg = "The heading for your profile is too short.  Please provide at least 40 characters. You currently have " + theForm.Advertisement.value.length + " characters";
    alert(profMsg);
    theForm.Advertisement.focus();
    return (false);
}
*/
if (theForm.Advertisement.value.length > 150) {
    var profMsg = "Please limit the heading for your profile to a maximum of 150 characters. You currently have " + theForm.Advertisement.value.length + " characters";
    alert(profMsg);
    theForm.Advertisement.focus();
    return (false);
}

if (theForm.PMinAge.value)
{
	if (theForm.PMinAge.value.match(/\D/) || theForm.PMinAge.value < 18 || theForm.PMinAge.value > 55) {
		alert("Please enter valid \"Minimum Age\" between 18 and 55 years.");
    	theForm.PMinAge.focus();
    	return (false);
	}
}

/*
var checkOK = "0123456789-";
var checkStr = theForm.PMinAge.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
            if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
        allNum += ch;
    }
	if (!allValid)
    {
        alert("Please enter only digit characters (no spaces) in the \"Minimum Age\" field.");
        theForm.PMinAge.focus();
        return (false);
    }

	if (theForm.PMinAge.value < 18) {
        alert("\"\Minimum Age\" field cannot be less than 18.");
        theForm.PMinAge.focus();
        return (false);
    }
}
*/

if ( theForm.PMaxAge.value)
{
	if (theForm.PMaxAge.value.match(/\D/) || theForm.PMaxAge.value < 18 || theForm.PMaxAge.value > 55) {
		alert("Please enter valid \"Maximum Age\" between 18 and 55 years.");
    	theForm.PMaxAge.focus();
    	return (false);
	}
/*
	if (theForm.PMaxAge.value.length > 2) {
		alert("Please enter at most 2 characters in the \"Maximum Age\" field.");
    	theForm.PMaxAge.focus();
    	return (false);
	}

	var checkOK = "0123456789-";
    var checkStr = theForm.PMaxAge.value;
    var allValid = true;
    var decPoints = 0;
    var allNum = "";
    for (i = 0;  i < checkStr.length;  i++)
    {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
            if (ch == checkOK.charAt(j))
            break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
        allNum += ch;
    }
	if (!allValid)
    {
        alert("Please enter only digit characters (no spaces) in the \"Maximum Age\" field.");
        theForm.PMaxAge.focus();
        return (false);
    }
*/
}

if (theForm.PMinAge.value && theForm.PMaxAge.value) {
	if (theForm.PMinAge.value > theForm.PMaxAge.value) {
		alert("\"\Minimum Age\" cannot be greater than the \"Maximum Age\".");
       	theForm.PMinAge.focus();
       	return (false);
	}
}

/*
// Preferred Caste - Other Check
if (theForm.PCaste.selectedIndex >= 0 ) {
	if (isOtherSelected(theForm.PCaste, theForm.PCasteOther) && !theForm.PCasteOther.value) {
  		alert("Need to specify the \"Pref. Caste (Other)\".");
		theForm.PCasteOther.focus();
 		return (false);
	}
}

if (theForm.PCaste.selectedIndex >= 0 ) {
	if (!isOtherSelected(theForm.PCaste, theForm.PCasteOther) && theForm.PCasteOther.value) {
		alert("You have specified both \"Pref. Caste & Pref. Caste (Other)\". Please specify only one of the two.");
		theForm.PCasteOther.focus();
 		return (false);
	}
}
*/
if (theForm.PMinHt.options[theForm.PMinHt.selectedIndex].value != "" &&
   theForm.PMaxHt.options[theForm.PMaxHt.selectedIndex].value != "")
{
	if (theForm.PMaxHt.options[theForm.PMaxHt.selectedIndex].value <
        theForm.PMinHt.options[theForm.PMinHt.selectedIndex].value) {
    	alert("Maximum preferred height cannot be less that Minimum preferred height");
    	theForm.PMinHt.focus();
    	return (false);
	}
}

/*
// Preferred Language - Other Check
if (theForm.PLangPref.selectedIndex >= 0 ) {
	if (isOtherSelected(theForm.PLangPref, theForm.PLangPrefOther) && !theForm.PLangPrefOther.value) {
  		alert("Need to specify the \"Pref. Language (Other)\".");
		theForm.PLangPrefOther.focus();
 		return (false);
	}
}

if (theForm.PLangPref.selectedIndex >= 0 ) {
	if (!isOtherSelected(theForm.PLangPref, theForm.PLangPrefOther) && theForm.PLangPrefOther.value) {
		alert("You have specified both \"Pref. Language & Pref. Language (Other)\". Please specify only one of the two.");
		theForm.PLangPrefOther.focus();
 		return (false);
	}
}

// Preferred Profession - Other Check
//if (theForm.PPrefProf.selectedIndex >= 0 ) {
//	if (isOtherSelected(theForm.PPrefProf, theForm.PPrefProfOther) && !theForm.PPrefProfOther.value) {
 // 		alert("Need to specify the \"Pref. Profession (Other)\".");
//		theForm.PPrefProfOther.focus();
 //		return (false);
//	}
//}

//if (theForm.PPrefProf.selectedIndex >= 0 ) {
//	if (!isOtherSelected(theForm.PPrefProf, theForm.PLangPrefOther) && theForm.PPrefProfOther.value) {
//		alert("You have specified both \"Pref. Profession & Pref. Profession (Other)\". Please specify only one of the two.");
//		theForm.PPrefProfOther.focus();
 //		return (false);
//	}
//}
*/

  if (isStrEmpty(theForm.ContactPhone) && isStrEmpty(theForm.ContactEmail))
  {
    alert("Since you have not provided contact phone, you need to provide contact e-mail. If you do not have an e-mail id or do not use e-mail, please provide a phone number");
    theForm.ContactEmail.focus();
    return (false);
  }

  if (! isStrEmpty(theForm.ContactEmail)) {
    if ( isEmail(theForm.ContactEmail.value) == false ) {
       alert ("Email id does not appear to in valid format. Please verify");
       return false;
    }
  }

  if (update == "N") {
    if (!theForm.PosterName.value || theForm.PosterName.value == "your name")
    {
      alert("Please enter your name so that we can contact you for verification.");
      theForm.PosterName.focus();
      return (false);
    }

    if (!theForm.VerificationPhone.value || theForm.VerificationPhone.value == "phone number")
    {
      alert("Please enter valid verification phone number with area code.");
      theForm.VerificationPhone.focus();
      return (false);
    }

    if (!theForm.VerPhoneName.value || theForm.VerPhoneName.value == "phone owner name")
    {
      alert("Please enter verification phone owner's name.");
      theForm.VerPhoneName.focus();
      return (false);
    }

  }
  return (true);
}

