// JavaScript Document

// Contact Us Form validation

function validateFormCU(form)
{
  if (isBlank(form.name.value)){
     alert("Please enter your name!");
     form.name.focus();
     return false;}
  if (isBlank(form.email.value)){
     alert("Please enter your email address!");
     form.email.focus();
     return false;}
  else { form.email.value = stripLeadingTrailingBlanks(form.email.value);
            if (!isEmail(form.email.value)) {
               alert("Please enter a valid email address: ?????@?????.???");
               form.email.focus();
               return false; }
  }
  if (isBlank(form.questions.value)){
     alert("What are you questions/concerns?");
     form.questions.focus();
     return false;}
  return true;
}

//Purchasing Forms Validations

function btnYes(divA, divB)
{
   document.getElementById(divA).style.display = "none";
   document.getElementById(divB).style.display = "block";
}
function btnNo(divA, divB,divC)
{
   document.getElementById(divA).style.display = "none";
   document.getElementById(divB).style.display = "none";
   document.getElementById(divC).style.display = "block";
}

function doc_click(svrname, extras, justid)
{
		if (confirm("Do you want to be added to the bidders list?"))
		{
			window.open("/finance.asp?nr=div&app=profile&state=confirm&url=" + extras, "_self"); 
		}
		else
		{
			if (confirm("You understand that you are accessing the document\nbut will not be registered as a bidder of record to\nreceive addenda or other correspondence."))
			{ 
				window.open("/departments/finance/purchasing/tenderView.asp?id=" + justid + "&type=tender" ,"TenderDocument","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=500");
			}
		}
}
	
function notification(justid)
{
	window.open("/departments/finance/purchasing/tenderView.asp?id=" + justid + "&type=tender" ,"fastftp","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=500");
}

function validateFormp(form)
  {
  if (isBlank(form.comp_name.value)){
      alert("Please enter your company name.");}
  else if (isBlank(form.street.value)){
           alert("Please enter your street.");} 
  else if (isBlank(form.city.value)){
           alert("Please enter your city.");}
  else if (isBlank(form.province.value)){
           alert("Please enter your province.");}
  else if (isBlank(form.country.value)){
           alert("Please enter your country.");}
  else if (isBlank(form.postal.value)){
           alert("Please enter your postal code.");} 
  else if (isBlank(form.contact.value)){
           alert("Please enter a contact name.");}
  else if (isBlank(form.phone.value)){
           alert("Please enter your telephone number.");}
  else if (isBlank(form.fax.value)){
           alert("Please enter your fax number.");}
  else if (isBlank(form.email.value)){
           alert("Please enter your email address.");}
  else if (!isEmail(form.email.value)){
           alert("Please enter a valid email address e.g. yourname@emaildomain.com");
           form.email.focus();}
  else {
	return true; // form valid - submit to ACTION URL
  }
  return false;}

function ValidateEmail(form)
{
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (emailFilter.test(form.email.value))
	{
		return true;
	}
	alert('Please enter a valid email');
	return false;
}
function ValidateFormR(form)
{
    if(isBlank(form.pwd.value)) 
    { 
		alert('You have not entered a Password');
        form.pwd.focus(); 
        return false; 
	}
	if ( form.pwd.value.length < 6 )
	{
	   alert('Please enter a password with at least 6 characters');
	   form.pwd.focus();
	   return false;
	}
   if(form.pwd.value != form.confirm_pwd.value) 
   { 
      alert('Your Password does not match your re-typed Password');
      form.confirm_pwd.focus(); 
      return false; 
   }
   if (isBlank(form.comp_name.value))
   {
      alert('Company name is required');
	  return false;
   }
   if (isBlank(form.contact.value))
   {
      alert('Contact person is required');
	  return false;
   }
   if (isBlank(form.city.value))
   {
      alert('City is required');
	  return false;
   }
   if (isBlank(form.street.value))
   {
      alert('Street is required');
	  return false;
   }
   if (isBlank(form.country.value))
   {
      alert('Country is required');
	  return false;
   }
   if (isBlank(form.postal.value))
   {
      alert('Postal code is required i');
	  return false;
   }
   if (isBlank(form.phone.value))
   {
      alert('Phone is required');
	  return false;
   }
   return ValidateEmail(form);
} 

function enableOC(thisform)
{
if (thisform.province.selectedIndex == 1){
   thisform.outside.disabled = false;
   thisform.outside.value = "Please specify"
   thisform.outside.focus()
   thisform.outside.select();
   }
else {thisform.outside.disabled = true;
      thisform.outside.value = "";
     }
}
function ValidateFormL(eForm) {
	if ("" == eForm.email.value) {
		window.alert("Please enter your email.");
		eForm.email.focus();
		return false;
	} else if("" == eForm.pwd.value) {
		window.alert("Please enter your password.");
		eForm.pwd.focus();
		eForm.pwd.select();
		return false;
	}//if
	return true;
}

function buttoncheck(form) {
 if (form.pwd.disabled == true) {
   form.pwd.disabled = false;
   form.confirm_pwd.disabled = false;
  } else {
   form.pwd.disabled = true;
   form.confirm_pwd.disabled = true;
  }
}
function ValidateFormProfile(form)
{
    if(isBlank(form.pwd.value)) 
    { 
		alert('You have not entered a Password');
        form.pwd.focus(); 
        return false; 
	}
	if ( form.pwd.value.length < 6 )
	{
	   alert('Please enter a password with at least 6 characters');
	   form.pwd.focus();
	   return false;
	}
   if(form.pwd.value != form.confirm_pwd.value) 
   { 
      alert('Your Password does not match your re-typed Password');
      form.confirm_pwd.focus(); 
      return false; 
   }
   return true;
}

function validateFormWTW(form)
  {
  // validate Name field
  // begin by stripping leading/trailing blanks
  if (getCheckedRadioButton(form.q1munic) == -1)
    {
    alert("Please choose the your municipality.");
    return false;
    }

  if (getCheckedRadioButton(form.q2activity) == -1)
    {
    alert("Please choose your current level of ativity.");
    return false;
    }
  else if ((form.q2activity.value == "0") || 
           (form.q2activity.value == "3") || 
           (form.q2activity.value == "4") )
	{
	alert("Based on your current phisical activity level, Walk This Way is not suited for you. However, other physical activity resources are available.");
	return false;
	}

  if (getCheckedRadioButton(form.q3regwalk) == -1)
    {
    alert("Please answer question 3.");
    return false;
    }

  if (getCheckedRadioButton(form.q4walktr) == -1)
    {
    alert("Please answer question 4.");
    return false;
    }

  if (isBlank(form.q5howoft) == -1)
    {
    alert("Please answer question 5.");
    return false;
    }
  else if (!isIntegerInRange(form.q5howoft.value, 0, 120))
    {
    alert("Please enter a reasonable positive integer \"How often...\" value!");
    form.q5howoft.value = "";
    form.q5howoft.focus();
    return false;
    }

  if (isBlank(form.q6howlong) == -1)
    {
    alert("Please answer question 6.");
    return false;
    }
  else if (!isIntegerInRange(form.q6howlong.value, 0, 120))
    {
    alert("Please enter a reasonable positive integer \"How long...\" value!");
    form.q6howlong.value = "";
    form.q6howlong.focus();
    return false;
    }

  if (getCheckedRadioButton(form.q7agegrp) == -1)
    {
    alert("Please select your age group.");
    return false;
    }

  if (getCheckedRadioButton(form.q8gender) == -1)
    {
    alert("Please select your gender.");
    return false;
    }

  if (getCheckedRadioButton(form.source) == -1)
    {
    alert("Please select where you completed the screenig tool.");
    return false;
    }
  return true;  // form valid - submit to ACTION URL
  }


function validateFormWTWPI(form)
  {
  if (isBlank(form.name.value))
    {
    alert("Please enter your name.");
    form.name.focus();
    return false;
    }

  if (isBlank(form.address.value))
    {
    alert("Please enter your address.");
    form.address.focus();
    return false;
    }

  if (isBlank(form.city.value))
    {
    alert("Please enter the city.");
    form.city.focus();
    return false;
    }

  // validate Zip field
  if (isBlank(form.postal.value))
    {
    alert("Please enter your postal code");
    form.postal.focus();
    return false;
    }

  // validate Email field
  // begin by stripping leading/trailing blanks
  if (!isBlank(form.email.value)){
     form.email.value = stripLeadingTrailingBlanks(form.email.value);
     if (!isEmail(form.email.value))
        {
        alert("Please enter a valid email address: ?????@?????.???");
        form.email.focus();
        return false;
     }}

  if (getCheckedRadioButton(form.contact) == -1)
    {
    alert("Please answer if we may contact you or not.");
    return false;
    }

  return true;  // form valid - submit to ACTION URL
  }
  
  function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}


// Checks for a valid date
function chkdate(objName) {
	var strDate = objName.value;
	var dd, mm, yyyy;
	var booNotFound = true;
	var strSeparatorArray = new Array('-',' ','/','.');
	if (strDate==null || strDate=="") { return false; }
	if (strDate.length < 1) { return true; }
	for (var itr = 0; itr < strSeparatorArray.length; itr++) {
		if (strDate.indexOf(strSeparatorArray[itr]) != -1) {
			var strDateArray = strDate.split(strSeparatorArray[itr]);
			if (strDateArray.length != 3) { return false; }
			dd   = parseInt(strDateArray[1],10);
			mm   = parseInt(strDateArray[0],10);
			yyyy = parseInt(strDateArray[2],10);
			if ((isNaN(dd) || isNaN(mm) || isNaN(yyyy))) { return false; }
			booNotFound = false;
			break;
		}
	}
	if (booNotFound) { return false; }

	if (yyyy < 100) { yyyy += 2000; }
	
	switch(mm) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:

		return (dd >= 1 && dd <= 31);
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		return (dd >= 1 && dd <= 30);
		break;
	case 2:
		return (LeapYear(yyyy))?(dd >= 1 && dd <= 29):(dd >= 1 && dd <= 28);
		break;
	default:
		return false;
	}
}

// Checks to make sure date is not in the future
function compareDate(objValue) {
    var strSeparatorArray = new Array('-',' ','/','.');
	for (var itr = 0; itr < strSeparatorArray.length; itr++) {
	    var strDateArray = objValue.split(strSeparatorArray[itr]);
		if (strDateArray.length == 3) {
		    dd   = parseInt(strDateArray[1],10);
		    mm   = parseInt(strDateArray[0],10);
		    yyyy = parseInt(strDateArray[2],10);
			break;
		}
	}
    var theDate = new Date();
	var today = new Date();
	theDate.setMonth(mm - 1);
	theDate.setDate(dd);
	theDate.setFullYear(yyyy);
	if (theDate.getFullYear() < today.getFullYear()) {
	    return true;
	}
	else if (theDate.getMonth() < today.getMonth() && theDate.getFullYear() == today.getFullYear()) {
	    return true;
	}
	else if (theDate.getDate() <= today.getDate() && theDate.getMonth() == today.getMonth() && theDate.getFullYear() == today.getFullYear()) {
	    return true;
	}
	return false;
}

// Adds trim() to a String object's methods
String.prototype.trim = trimString;

// Replaces white space with an empty string
function trimString() {
    return this.replace(/^\s+/, "").replace(/\s+$/, "");
}


//LTC Feedback Form
function validateFormLTC(form)
{
  if (isBlank(form.name.value)){
     alert("Please enter your name!");
     form.name.focus();
     return false;}
  if (!isBlank(form.email.value)){
      if (isEmail(form.email.value)==false){
     alert("Please enter a valid email address");
     form.email.focus();
     return false;} }
  if (!isBlank(form.phone.value)){
      if (isUSPhoneNumber(form.phone.value)==false){
     alert("Please enter a valid 10 digit phone number");
     form.phone.focus();
     return false;} }
  if (isBlank(form.questions.value)){
     alert("What are you questions/concerns/feedback?");
     form.questions.focus();
     return false;}
  return true;
}

//LTC Survey
function validateFormLTCSurvey(form)
{
  if (getCheckedRadioButton(form.home)== -1)
  {
	alert("Please select which home you work out of.");
	return false;
  }
  return true;	
}


//Works - New Residents
function validateFormNRes(form)
{
  if (isBlank(form.firstName.value)){
     alert("Please enter your first name!");
     form.firstName.focus();
     return false;}
  if (isBlank(form.lastName.value)){
     alert("Please enter your last name!");
     form.lastName.focus();
     return false;}
  if (isBlank(form.phoneNumber.value)){
     alert("Please enter your phone number!");
     form.phoneNumber.focus();
     return false;}
  if (isBlank(form.emailAddress.value)){
     alert("Please enter your email address!");
     form.phoneNumber.focus();
     return false;}
  if (isBlank(form.address.value)){
     alert("Please enter your address!");
     form.address.focus();
     return false;}
  if (isBlank(form.majorIntersection.value)){
     alert("Please enter a major intersection!");
     form.address.focus();
     return false;}
  if (isBlank(form.city.value)){
     alert("Please enter your city!");
     form.city.focus();
     return false;}
  if (isBlank(form.postalCode.value)){
     alert("Please enter your postal code!");
     form.postalCode.focus();
     return false;}
  if (isBlank(form.newOrResale.value)){
     alert("Please specify whether this is a new house or a re-sale!");
     form.newOrResale.focus();
     return false;}
	 
  return true;
}



//Waste Management Program Updates
function validateFormWMPU(form)
{
  if (isBlank(form.firstName.value)){
     alert("Please enter your first name!");
     form.firstName.focus();
     return false;}
  if (isBlank(form.lastName.value)){
     alert("Please enter your last name!");
     form.lastName.focus();
     return false;}
  if (isBlank(form.emailAddress.value)){
     alert("Please enter your email address!");
     form.emailAddress.focus();
     return false;}
  if (isBlank(form.streetAddress.value)){
     alert("Please enter your street address!");
     form.streetAddress.focus();
     return false;}
  if (isBlank(form.city.value)){
     alert("Please enter your city!");
     form.city.focus();
     return false;}
  if (isBlank(form.postalCode.value)){
     alert("Please enter your postal code!");
     form.postalCode.focus();
     return false;}
  	 
  return true;
}


//Waste Clear Bag Study Contact Form
function validateFormWasteContact(form)
{
  if (isBlank(form.firstName.value)){
     alert("Please enter your first name!");
     form.firstName.focus();
     return false;}
  if (isBlank(form.lastName.value)){
     alert("Please enter your last name!");
     form.lastName.focus();
     return false;}
  if (isBlank(form.emailAddress.value)){
     alert("Please enter your email address!");
     form.emailAddress.focus();
     return false;}
  if (isBlank(form.message.value)){
     alert("Please enter your message to us!");
     form.message.focus();
     return false;}
  if (form.message.textLength > 4000){
     alert("Your message is too big! Please make it smaller (4000 characters maximum)");
     form.message.focus();
     return false;}
  if (isBlank(form.streetAddress.value)){
     alert("Please enter your street address!");
     form.streetAddress.focus();
     return false;}
  if (isBlank(form.city.value)){
     alert("Please enter your city!");
     form.city.focus();
     return false;}
  if (isBlank(form.postalCode.value)){
     alert("Please enter your postal code!");
     form.postalCode.focus();
     return false;}
  	 
  return true;
}


//WQR
function openWin(URL) {
	var popWin = window.open(URL, "ParamDef", "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,dependent,width=360,height=300,left=150,top=150");
}//openWin


function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
