
function stripHead(s) {
  if (s == null || s == "") return s
  i=0
  while (s.charAt(i) == ' ') {
   i++
  }
  return s.substring(i, s.length)
}

function stripTail(s) {
  if (s == null || s == "") return s 
  i=s.length - 1
  while (s.charAt(i) == ' ') {
    i--
  }
  return s.substring(0, i+1)
}

function strip(s) {
  if (s == null || s == "") return s
  s2 = stripHead(s)
  s2 = stripTail(s)
  return s2
}

function isValidURL(url) {
  if (url == null  || url == "") {
    alert("URL is empty")
    return false
  }
  if (url.substring(0,7) != "http://" && url.substring(0,7) != "mailto:"
           && url.substring(0,6) != "ftp://") {
    alert("URL must start with 'http://', 'ftp://', or 'mailto:'")
    return false
  }
  return true
}

function isValidEmail(s) {
  s2 = strip(s)
  if (s2 == null || s2 == "") return false
  if (s.indexOf('@') <= 0) return false
  if (s.indexOf(' ') > 0) return false
  return true
}

function isBlank(s) {
  if (s == null || s == "") return true
  s2 = stripHead(s)
  if (s2 == null  || s2 == "") return true
  return false
}

function stripUrlType(url) {
  s = strip(url)
  if (s == null || s == "") return s
  if (s.substring(0,7) == "http://") {
    s = s.substring(7, s.length)
  } else if (s.substring(0,7) == "mailto:") {
    s = s.substring(7, s.length)
  } else if (s.substring(0,6) == "ftp://") {
    s = s.substring(6, s.length)
  }
  return s
}

function getSelectedValue(list) {
  if (list.selectedIndex < 0) {
    return null
  }
  return list.options[list.selectedIndex].value
}

function validateForm(theform) {

 // if (getSelectedValue(theform.l_category) == "Select One") {
  //  alert("Please select a category")
  //  return false
  // }
  // if (getSelectedValue(theform.l_subCategory) == "Select One") {
  //  alert("Please select a sub-category")
  //  return false
  // }
  if (isBlank(theform.l_submiterName.value)) {
      alert("Submitter Name cannot be empty")
      return false
  }

  if (!isValidEmail(theform.l_submiterEmail.value)) {
    alert("Submitter Email is invalid")
    return false
  }
  if (isBlank(theform.l_entryName.value)) {
    alert("Web Site Name cannot be empty")
    return false
  }

  if (isValidURL(theform.l_url.value) == false) {
     return false
  }

  if (isBlank(theform.l_description.value)) {
    alert("Description cannot be empty")
    return false
  }
  var desc_length = theform.l_description.value.length
  if (desc_length > 500) {
    alert("Description is too long (" + desc_length + " characters). Reduce it to 500 characters.")
      return false
  }
  if (isBlank(theform.captchaImage.value)) {
    alert("Security Answer cannot be empty")
    return false
  }
  return true
}

function deleteOptions(theList) {
 // Delete all options in the select control
  while (theList.length > 0) {
    theList.options[0]=null
  }
}

function replaceOptions(theList, optionArray) {

  // replace the options list of select control. Return true if 
  // theList is replaced or deleted
  // return false if no change

  if (optionArray == null || optionArray.length == 0) {
      if (theList.length == 0) return false
      deleteOptions(theList)
      return true
  }
  
  // check whether theList already has the same options as optionArray
   var needReplace
    if (theList.length != optionArray.length) {
      needReplace = true
    } else { 
      needReplace = false
      for (var n=0; n<theList.length; n++) {
        if (theList.options[n].text != optionArray[n]) {
          needReplace = true 
          break
        }
	   }
    }

  if (needReplace) {
      deleteOptions(theList)
      for (var m=0; m<optionArray.length; m++) {
        theList.options[m] = new Option(optionArray[m], optionArray[m]);
	  }
	  var selIndex = 0;
  	  for(var i=0;i<optionArray.length;i++){
	    if (theList.options[i].value == subCatValue){
		    selIndex = i*1;  		
			break;
	    }else{
		  continue;
        }
	  }
	  theList.selectedIndex = selIndex;
      return true
    }
    return false
  }
  

function populateSubCategory(theform) {

 // var category = theform.l_category.value
  var category = theform.l_category.options[theform.l_category.selectedIndex].value
  theform.l_preCategory.value = category
  var subCategory = null
  if (category == "Select One") {
	  hideSubCategory();
     // subCategory = null
  } else if (category == "Practice Areas (36)") {
	  showSubCategory();
      subCategory = new Array("Select One","Administrative Law","Admiralty Law","Antitrust and Trade Regulation",
		  "Banking Law","Bankruptcy Law","Civil Rights Law","Commercial Law","Communications Law","Constitutional Law",
		  "Contracts Law","Corporation and Enterprise Law","Criminal Law","Cyberspace and Internet Law","Dispute Resolution and Arbitration",
		  "Education Law","Entertainment and Sports Law","Environmental Law","Ethics and Legal Responsibility","Family Law",
		  "Federal Benefits Law","Government Contract Law","Healthcare Law","Immigration Law","Indian and Tribal Law",
		  "Intellectual Property Law","Copyright Center","Trademark Center","Patent Center","Trade Secrets","International Law",
		  "International Trade Law","Judges and the Judiciary","Labor and Employment Law","Legal Theory","Litigation Resources",
		  "Personal Injury and Tort Law","Probate, Trusts and Estates","Real Estate and Property Law","Securities Law","Tax Law") 
 } else if (category == "State Resources") {
     showSubCategory();
     subCategory = new Array("Select One","General State Resources","Alabama","Alaska","Arizona","Arkansas","California",
		 "Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas",
		 "Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri",
		 "Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota",
		 "Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas",
		 "Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming","District of Columbia","America Samoa",
		 "Guam","N. Mariana Islands","Puerto Rico","Tribal Governments","US Virgin Islands")
	 
 } else if (category == "Judicial Courts and Decisions") {
	  showSubCategory();
      subCategory = new Array("Select One","Federal District Courts by Circuits")
 } else if (category == "Legal Professional Development") {
	  showSubCategory();
      subCategory = new Array("Select One","Career Center","State Bar Associations","Continuing Legal Education")
 } else if (category == "Internet Resources") {
 	  showSubCategory();
      subCategory = new Array("Select One","Internet Basics","Internet Software Resources")
 } else if (category == "Computers and Security") {
  	  showSubCategory();
      subCategory = new Array("Select One","Antivirus Center","Computer Software","Cryptography and Encryption","First Amendment and Internet Privacy")
 } else if (category == "Health and Leisure") {
	  showSubCategory();
      subCategory = new Array("Select One","Health (Includes 7 additional pages)","General Health","Child Health","Teen Health",
		"Womens Health","Mens Health","Senior Health","Mental Health","Medications","Stress Management","Family and Relationships",
		"Food and Wine","Communities and Relocation","Sports","Online Shopping")
 } else{
	 hideSubCategory();
 }
 browserName = navigator.appName
 if (replaceOptions(theform.l_subCategory, subCategory) && 
      browserName == "Netscape") {
    // List l_subCategory is modified, Netscape need refresh
    // IExploer must not refresh to avoid infinite loop
//    history.go(0)*/
  } 
}

function doReset(theform) {
  theform.reset()
  populateSubCategory(theform)
}
function showSubCategory(){ 
    document.getElementById('subCatDiv').style.display = 'block';
    document.getElementById('subCatSelectDiv').style.display = 'block';
}
function hideSubCategory(){ 
	document.getElementById('subCatDiv').style.display = 'none'; 
    document.getElementById('subCatSelectDiv').style.display = 'none';
} 
function submit(){
	if(validateForm(document.legalURL)){
		document.legalURL.submit();
	}
}
