function toggleField(theForm, theOption, theValue, theField)
{
	if (theOption.value == theValue) 
	{
		theField.style.background = "#FFFFFF";
		theField.disabled = false;
		theField.focus();
	}
	else
	{
		//if(theOption.name == 'Citizenship_Nominee')
		//	theField.selectedIndex = 0;
		//else
		//	theField.value = "";
			
		theField.style.background = "#EEEEEE";
		theField.disabled = true;
	}
}

function popWin(url, w, h)
{
	var ww = w;
	var hh = h;
	origLeft = (screen.width-ww)/2;
	origTop = (screen.height-hh)/2;
	var w = window.open("",
                       "map",
                       "resizable=no,status=0,scrollbars=1,menubar=0,directories=0,width=" + w + ",height=" + h);
   if (w)
   {
		w.location=url;
		w.moveTo(origLeft,origTop);
		w.focus();
   }
}

function fillDropDown(fList)
{
	var s = document.form.Documents_Box_Nominee;
	var dropLen = s.length;
	
	for(var i=0; i<dropLen; i++)
		s.options[i] = null;
		
	fArr = fList.split("&*&*");

	for(var i=0; i<fArr.length; i++)
		s.options[i] = new Option(fArr[i], fArr[i]);
		
	document.form.Documents_Nominee.value = fList;
}

function showLayer( ) {
	var s = document.form.Artform_Nominee;
	var dropLen = s.length;
	
	for(var i=0; i<dropLen; i++) {
		var sid = "scope_" + s.options[i].value;
		
		if(s.options[i].selected == true) {
			if (document.getElementById(sid))  document.getElementById(sid).style.display = '';
		}
		else  {
			if (document.getElementById(sid)) document.getElementById(sid).style.display = 'none';
		}
	}
	
	for (var i=0; i<document.form.Scope_Nominee.length; i++)
		document.form.Scope_Nominee[i].checked = false;

	//if(s.options[s.selectedIndex].value != 'Others' && s.options[s.selectedIndex].value != 'others')
	//{
	//	document.form.Scope_Nominee_Others.value = "";
	//}
}

function checkLen(textarea)
{
	maxLen = 100;
/*
	
	str = escape(textarea.value);
	str = str.replace('%0D%0A%0D%0A', ' ');
	str = str.replace('%0D%0A', ' ');
	str = unescape(str);
	var c = 0;
	for(i = 0; i < str.length; i++)
		if(str.charAt(i) == " ") c++;
	
	if (c >= maxLen) {
		alert("You have reached the 100 word limit for \"Why your nominee deserves the award\". Please summarize it.");	
		return false;
	}
	else
		return true;
*/
	if (CountWords(textarea, false, false) > maxLen) {
		alert("You have reached the 100 word limit for \"Why your nominee deserves the award\". Please summarize it.");	
		return false;
	}
	else
		return true;
}

function CountWords (this_field, show_word_count, show_char_count) {
	if (show_word_count == null) {
	show_word_count = true;
	}
	if (show_char_count == null) {
	show_char_count = false;
	}
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) {
	word_count = 0;
	}
	if (word_count == 1) {
	wordOrWords = " word";
	}
	else {
	wordOrWords = " words";
	}
	if (char_count == 1) {
	charOrChars = " character";
	} else {
	charOrChars = " characters";
	}
	if (show_word_count & show_char_count) {
	alert ("Word Count:\n" + "    " + word_count + wordOrWords + "\n" + "    " + char_count + charOrChars);
	}
	else {
	if (show_word_count) {
	alert ("Word Count:  " + word_count + wordOrWords);
	}
	else {
	if (show_char_count) {
	alert ("Character Count:  " + char_count + charOrChars);
	      }
	   }
	}
	return word_count;
}

function Form_Validator(theForm)
{

  if (theForm.t.selectedIndex == 0)
  {
    alert("The first \"Nomination Type\" option is not a valid selection.  Please choose one of the other options.");
    theForm.t.focus();
    return (false);
  }

  if (theForm.Salutation_Nominator.selectedIndex == 0)
  {
    alert("The first \"Salutation (Nominator)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Salutation_Nominator.focus();
    return (false);
  }

  if (theForm.First_Name_Nominator.value == "")
  {
    alert("Please enter a value for the \"First Name (Nominator)\" field.");
    theForm.First_Name_Nominator.focus();
    return (false);
  }

  if (theForm.Last_Name_Nominator.value == "")
  {
    alert("Please enter a value for the \"Last Name (Nominator)\" field.");
    theForm.Last_Name_Nominator.focus();
    return (false);
  }

  if (theForm.NRIC_Nominator.value == "")
  {
    alert("Please enter a value for the \"NRIC (Nominator)\" field.");
    theForm.NRIC_Nominator.focus();
    return (false);
  }

  if (theForm.Address_Nominator.value == "")
  {
    alert("Please enter a value for the \"Address (Nominator)\" field.");
    theForm.Address_Nominator.focus();
    return (false);
  }

  if (theForm.Postal_Code_Nominator.value == "")
  {
    alert("Please enter a value for the \"Postal Code (Nominator)\" field.");
    theForm.Postal_Code_Nominator.focus();
    return (false);
  }

  if (theForm.Handphone_Nominator.value == "" && theForm.Office_Phone_Nominator.value == "" && theForm.Home_Phone_Nominator.value == "")
  {
    alert("Please provide at least one contact number for the \"Contact Numbers (Nominator)\" field.");
    theForm.Handphone_Nominator.focus();
    return (false);
  }

  if (theForm.Email_Nominator.value.length > 0 && theForm.Email_Nominator.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"Email (Nominator)\" field.");
    theForm.Email_Nominator.focus();
    return (false);
  }

  if (theForm.Occupation_Nominator.value == "")
  {
    alert("Please enter a value for the \"Occupation (Nominator)\" field.");
    theForm.Occupation_Nominator.focus();
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.Behalf_Arts_Org_Nominator.length;  i++)
  {
    if (theForm.Behalf_Arts_Org_Nominator[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Is this nomination made on behalf of an arts organization?\" options.");
    location.href= '#on_behalf';    
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.Behalf_Arts_Org_Nominator.length;  i++)
  {
    if (theForm.Behalf_Arts_Org_Nominator[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Gender\" options.");
    return (false);
  }

    if (theForm.Behalf_Arts_Org_Nominator[1].checked)
	  if (theForm.Behalf_Arts_Org_Name_Nominator.value == "")
	  {
	    alert("Please enter a value for the \"Name of Arts Organization (Nominator)\" field.");
	    theForm.Behalf_Arts_Org_Name_Nominator.focus();
	    return (false);
	  }

  if (theForm.Photo_Box_Nominee.value == "")
  {
    alert("Please upload a photo in JPG or JPEG format for the \"Photo of Nominee\" field.");
    location.href= '#Photo_of_Nominee';
    return (false);
  }

  if (theForm.Salutation_Nominee.selectedIndex == 0)
  {
    alert("The first \"Salutation (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Salutation_Nominee.focus();
    return (false);
  }

  if (theForm.First_Name_Nominee.value == "")
  {
    alert("Please enter a value for the \"First Name (Nominee)\" field.");
    theForm.First_Name_Nominee.focus();
    return (false);
  }

  if (theForm.Last_Name_Nominee.value == "")
  {
    alert("Please enter a value for the \"Last Name (Nominee)\" field.");
    theForm.Last_Name_Nominee.focus();
    return (false);
  }

  if (theForm.Address_Nominee.value == "")
  {
    alert("Please enter a value for the \"Address (Nominee)\" field.");
    theForm.Address_Nominee.focus();
    return (false);
  }

  if (theForm.Postal_Code_Nominee.value == "")
  {
    alert("Please enter a value for the \"Postal Code (Nominee)\" field.");
    theForm.Postal_Code_Nominee.focus();
    return (false);
  }

  if (theForm.Handphone_Nominee.value == "" && theForm.Office_Phone_Nominee.value == "" && theForm.Home_Phone_Nominee.value == "")
  {
    alert("Please provide at least one contact number for the \"Contact Numbers (Nominee)\" field.");
    theForm.Handphone_Nominee.focus();
    return (false);
  }

  if (theForm.Email_Nominee.value.length > 0 && theForm.Email_Nominee.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"Email (Nominee)\" field.");
    theForm.Email_Nominee.focus();
    return (false);
  }

  if (theForm.NRIC_Nominee.value == "")
  {
    alert("Please enter a value for the \"NRIC (Nominee)\" field.");
    theForm.NRIC_Nominee.focus();
    return (false);
  }

  if (theForm.Birth_Day_Nominee.selectedIndex == 0)
  {
    alert("The first \"Day of Birth (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Birth_Day_Nominee.focus();
    return (false);
  }

  if (theForm.Birth_Month_Nominee.selectedIndex == 0)
  {
    alert("The first \"Month of Birth (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Birth_Month_Nominee.focus();
    return (false);
  }

  if (theForm.Birth_Year_Nominee.selectedIndex == 0)
  {
    alert("The first \"Year of Birth (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Birth_Year_Nominee.focus();
    return (false);
  }

  if (theForm.Age_Nominee.value == "")
  {
    alert("Please enter a value for the \"Age (Nominee)\" field.");
    theForm.Age_Nominee.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.Age_Nominee.value;
  var allValid = true;
  var validGroups = 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 in the \"Age (Nominee)\" field.");
    theForm.Age_Nominee.focus();
    return (false);
  }

  if (theForm.Birth_Place_Nominee.value == "")
  {
    alert("Please enter a value for the \"Place of Birth (Nominee)\" field.");
    theForm.Birth_Place_Nominee.focus();
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.Citizenship_Nominee.length;  i++)
  {
    if (theForm.Citizenship_Nominee[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Citizenship (Nominee)\" options.");
    location.href= '#Citizenship';
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.Citizenship_Nominee.length;  i++)
  {
    if (theForm.Citizenship_Nominee[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Gender\" options.");
    return (false);
  }

    if (theForm.Citizenship_Nominee[1].checked)
	  if (theForm.PR_Year_Obtained_Nominee.selectedIndex == 0)
	  {
	    alert("The first \"Year Obtained of Citizenship (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
	    theForm.PR_Year_Obtained_Nominee.focus();
	    return (false);
	  }

  if (theForm.Artform_Nominee.selectedIndex == 0)
  {
    alert("The first \"Area of Achievement (Nominee)\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Artform_Nominee.focus();
    return (false);
  }

	if(theForm.Artform_Nominee.options[theForm.Artform_Nominee.selectedIndex].value == 'Others' || theForm.Artform_Nominee.options[theForm.Artform_Nominee.selectedIndex].value == 'others')
	{
		if(document.form.Scope_Nominee_Others.value == "") {
			alert("Please enter a value for the \"Scope (Others)\" options.");
			theForm.Scope_Nominee_Others.focus();
			return (false);
		}
	}
	else {
		var isScopeChecked = false;
		for (i = 0; i < document.form.Scope_Nominee.length; i++)
		{
			if (document.form.Scope_Nominee[i].checked)
				isScopeChecked = true;
		}
		if (!isScopeChecked)
		{
		alert("Please select one or more values of the \"Scope\" options.");
		location.href= '#Area_of_Achievement';
		return (false);
		}
	}

  var radioSelected = false;
  for (i = 0;  i < theForm.Nominated_Before_Nominee.length;  i++)
  {
    if (theForm.Nominated_Before_Nominee[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Have you been nominated before? (Nominee)\" options.");
    location.href= '#nominated_before';    
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.Nominated_Before_Nominee.length;  i++)
  {
    if (theForm.Nominated_Before_Nominee[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Gender\" options.");
    return (false);
  }

    if (theForm.Nominated_Before_Nominee[1].checked)
	  if (theForm.Nomination_Years_Nominee.value == "")
	  {
	    alert("Please enter a value for the \"Year(s) of Nomination (Nominee)\" field.");
	    theForm.Nomination_Years_Nominee.focus();
	    return (false);
	  }

  if (theForm.Why_Deserves_Nominee.value == "")
  {
    alert("Please enter a value for the \"Why your nominee deserves the award\" field.");
    theForm.Why_Deserves_Nominee.focus();
    return (false);
  }

  if (!checkLen(theForm.Why_Deserves_Nominee))
  {
    theForm.Why_Deserves_Nominee.focus();
    return (false);
  }

  if (theForm.Evaluation_Nominee.value == "")
  {
    alert("Please enter a value for the \"Descriptive evaluation of artistic achievements of the nominee\" field.");
    theForm.Evaluation_Nominee.focus();
    return (false);
  }

  if (theForm.Documents_Nominee.value == "")
  {
	alert("Please upload  nominee's curriculum vitae and/or supporting documents.");
	document.form.Documents_Box_Nominee.focus();
	return (false);
  }
    
  return (true);
}
