function getCheckedValue(r) {
	//input r is a Radio object; returns the value of the checked button 
	for (i=0;i<r.length;i++) {
		//following alert was for testing purposes...
		//alert(i+ "th value is " + r[i].value);
		if (r[i].checked) { return r[i].value; }
	}
	return "";
}

function getSelectedValue(s) {
   //input s is a Select object; returns value of selected option
   if(s.selectedIndex <0) { value=""; }
   else {  value = s.options[s.selectedIndex].value; }
   return value;
}  

function isChecked(r) {
	//input r is a Radio object; returns true if checked 
	for (i=0;i<r.length;i++) {
		if (r[i].checked) { return true; }
	}
	return false;
}

function isEmptyString(s) {
	//input (s) is string
	if (s.length == 0) { return true; }           //s.value is emptyString
	else { return false; }   //s.value is a string of 1 or more characters
}

function isEmptyText(t) {
	//input (t) is javascript text or textarea object
	if (t.value == "") { return true; }           //t.value is emptyString
	else { return false; }   //t.value is a string of 1 or more characters
}

function isSelected(s) {
	//input s is a Selection object;
	if ( isEmptyString(getSelectedValue(s)) ) { return false; }
	else { return true; }
}

function setSelectedValue(s,v) {
	//input s is a Select object; v is value to select
	for (i=0;i<s.length;i++) {
		if (v==s.options[i].value) {
			s.options[i].selected = true;
			return true;
		}
	}
	return false;
}

function validEmailAddress(t) {

    // Note: The next expression must be all on one line...
    //       allow no spaces, linefeeds, or carriage returns!
    var validEmailId = t.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

    if (validEmailId){
      return true
    } else {
      alert('Please enter a valid e-mail address.')
      t.focus()
      t.select()
      return false
    }
}


function truncateNumber(number,dec) {
  input = ""+number;  // make it a string!
  decPos = input.indexOf(".",0) +1 ;
  if (decPos != 0) { value = input.substring(0,decPos+dec); }
  else { value = input; }
  return value;
} 

function getSelectedText(s) {
   //input s is a Select object; returns value of selected option
   if(s.selectedIndex <0) { text=""; }
   else {  text = s.options[s.selectedIndex].text; }
   return text;
}  

function validateInputFields(form) {
  if (isEmptyText(form.name)) {
    emptyfield = form.name
	fldlabel = "Name"
  } else if (isEmptyText(form.street_line1)) {
	emptyfield = form.street_line1
	fldlabel = "Street Address1"
  } else if (isEmptyText(form.city)) {	    		
	emptyfield = form.city
	fldlabel = "City"		
  } else if (isEmptyText(form.zip)) {
	emptyfield = form.zip
	fldlabel = "Zip"
  } else if (isEmptyText(form.contact_name)) {
	emptyfield = form.contact_name
	fldlabel = "Contact Person Name"
  } else if (isEmptyText(form.contact_tel_num)) {
	emptyfield = form.contact_tel_num
	fldlabel = "Contact Tel Number"
  } else if (isEmptyText(form.email_id)) {
    emptyfield = form.email_id
	fldlabel = "Email Id"
  } else if (isEmptyText(form.capacity)) {
    emptyfield = form.capacity
	fldlabel = "Capacity"
  } else if (isEmptyText(form.facility_type)) {
    emptyfield = form.facility_type
	fldlabel = "Facility Type"				
  } else if (isEmptyText(form.rates)) {
    emptyfield = form.rates
	fldlabel = "Rate"
  } else if (isEmptyText(form.rental_cycle)) {
    emptyfield = form.rental_cycle
	fldlabel = "Rental Cycle"
  } else if (isEmptyText(form.location)) {
    emptyfield = form.location
	fldlabel = "Location"		
  } else if (isEmptyText(form.oneline_desc)) {
    emptyfield = form.oneline_desc
	fldlabel = "Oneline Description"
  } else if (isEmptyText(form.menu_type)) {
    emptyfield = form.menu_type
	fldlabel = "Menu"								
  } else if (isEmptyText(form.special_dining)) {
    emptyfield = form.special_dining
	fldlabel = "Special Dining"								
  } else {
    return true
  }		
  alert(fldlabel + " is a required field.")
  emptyfield.focus()
  return false
}

function validateWebsiteURL(frm) {
  //if the URL does not contain http:// then reject
  //t = document.forms[0].website_url.value
  t = frm.website_url.value
  if (isEmptyText(frm.website_url)) {
    return true
  } else {
    if ((t.indexOf("http://", 0) >= 0) || (t.indexOf("HTTP://", 0) >= 0)){
      return true
    } else {
      alert ("Your website URL is missing http://")
      frm.website_url.focus()
	  return false
    }
  }
} 

function validateNumber(t) {
  //input (t) is javascript text
  validChars = "0123456789";
  string = t.value;
  string_length = t.value.length;
  for(i=0; i<string_length; i++) {
    curChar = string.charAt(i);
    if (validChars.indexOf(curChar,0) == -1) {
      alert ("Capacity must be a number")
      t.focus()
      t.select()      
      return false; 
    }
  }
  return true;
}

function validatePwdLen(s) {
  if (s.value.length < 6) {
    alert ("password should be atleast 6 characters long.")
    s.focus()
    return false
  } else if (s.value.length > 8) {
    alert ("password cannot be more than 8 characters long.")
    s.focus()
    return false
  } else { 
    return true
  }
}      

function validateLoginLen(s) {
  if (s.value.length < 6) {
    alert ("Login id should be atleast 6 characters long.")
    s.focus()
    return false
  } else if (s.value.length > 8) {
    alert ("login id cannot be more than 8 characters long.")
    s.focus()
    return false    
  } else { 
    return true
  }
}      

function trim(form_fld) {
//removes the leading and trailing spaces and also any extra spaces in the string.
  value = form_fld.value;

  while (value.charAt(value.length-1) == " "){value = value.substring(0,value.length-1);} 
  while(value.substring(0,1) ==" "){value = value.substring(1,value.length);}

  form_fld.value = value;

  //Leave the next line only when you want to select the textbox after you have clicked the button.
  form_fld.select();
}
