////////////////////////////////////////
function getCookie(value){
  var cookiestring = document.cookie;
  var index = cookiestring.indexOf(value);
  alert ("GETting the " + value + " cookie");
  if (index == -1) return null; // no such cookie found
//  alert ("getCookie did not return null for " + value);
  index = cookiestring.indexOf("=", index) + 1;
  var endstr = cookiestring.indexOf(";", index);
  if (endstr == -1) endstr = cookiestring.length;
  return unescape(cookiestring.substring(index, endstr));

}

////////////////////////////////////////
function setCookie(name,value,days) {
  var cookiestring = document.cookie;
  var today = new Date();
  var expiry = new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
  document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
  alert ("Setting cookie: " + name + ", expiry: " + expiry );
  cookiestring = document.cookie; // update the cookie
}

///////////////////////////////
function confirmDelete(theForm)
{
    return confirm('Confirm: Permanently Delete this entry?');
}

///////////////////////////////
function validRequired(formField,fieldLabel)
{
  var result = true;
  
  if (formField.value == "")
  {
    alert('Please enter the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }

  return result;
}

///////////////////////////////
function validateForm(theForm)
{

  if (!validRequired(theForm.first_name,"First Name"))
    return false;

  if (!validRequired(theForm.last_name,"Last Name"))
    return false;

  if (!validRequired(theForm.companyname,"Company/Organization/School"))
    return false;

  return true;
}

///////////////////////////////
function validateVolForm(theForm)
{

  if (!validRequired(theForm.first_name,"First Name"))
    return false;

  if (!validRequired(theForm.last_name,"Last Name"))
    return false;

  if (!validRequired(theForm.province,"Province"))
    return false;

  if (!validRequired(theForm.email,"Email"))
    return false;

  //check email address
  if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email.value)){
	  alert("Invalid E-mail Address");
	  return false;
  }

  return true;
}

///////////////////////////////
function validateRegForm(theForm)
{

  if (!validRequired(theForm.name_of_organization,"Name of Organization"))
    return false;

  if (!validRequired(theForm.province,"Province"))
    return false;

  if (!validRequired(theForm.country,"Country"))
    return false;

  if (!validRequired(theForm.email,"Email"))
    return false;

  if (!validRequired(theForm.address1,"Street/Unit Number"))
    return false;

  if (!validRequired(theForm.address2,"Street"))
    return false;

  return true;
}

///////////////////////////////
function validateSchoolForm(theForm)
{

  if (!validRequired(theForm.school_name,"School Name"))
    return false;

  if (!validRequired(theForm.school_address1,"Street/Unit Number"))
    return false;

  if (!validRequired(theForm.school_address2,"Street Name"))
    return false;

  if (!validRequired(theForm.school_city,"School City"))
    return false;

  if (!validRequired(theForm.province,"School Province"))
    return false;

  if (!validRequired(theForm.school_country,"School Country"))
    return false;

  if (!validRequired(theForm.school_postal_code,"School Postal Code"))
    return false;

  if (!validRequired(theForm.contact_first_name,"Contact First Name"))
    return false;

  if (!validRequired(theForm.contact_last_name,"Contact Last Name"))
    return false;

  if (!validRequired(theForm.school_contact_phone,"Contact Phone"))
    return false;

  if (!validRequired(theForm.school_contact_fax,"Contact Fax"))
    return false;

  if (!validRequired(theForm.contact_person_position,"Contact Person Position"))
    return false;

  if (!validRequired(theForm.school_contact_email,"School Contact Email"))
    return false;

  if (!theForm.project_contact_authorization.checked)
  {
	if (!validRequired(theForm.first_name_authorize,"Authorizing First Name"))
      		return false;

	if (!validRequired(theForm.last_name_authorize,"Authorizing Last Name"))
      		return false;

	if (!validRequired(theForm.authorizing_person_position,"Authorizing Position"))
      		return false;

	if (!validRequired(theForm.authorizing_person_emailadd,"Authorizing Email"))
      		return false;

	if (!validRequired(theForm.authorizing_person_phone,"Authorizing Phone"))
      		return false;

  }

  if ((!theForm.count_me_in[0].checked) && (!theForm.count_me_in[1].checked)){
	alert('Please enter the "Payment within/out BC" field.');
    	return false;
  }

  return true;
}

