// marco.js
var days_of_month = new Array("31","28","31","30","31","30","31","31","30","31","30","31"); // Total number of days in each month
var month_names = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

// track which submit button clicked
var whichSubmit;

// Tour Dimensions - Scott 2010-02-01
var win = null;
function newmarcowindow(mypage,myname){
	
	var w = 750;
	var h = 600;
	var scroll = 'yes';
	
	if (myname == "Tour") {
		w = 380;
		h = 292;
		scroll = 'no';
	}
	
	if (myname == "Tourlg") {
		w = 500;
		h = 355;
		scroll = 'no';
	}
	
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',screenX='+LeftPosition+',screenY='+TopPosition+',scrollbars='+scroll+',resizable'
	win = window.open(mypage,myname,settings)
	if(win.window.focus){
		win.window.focus();
	}
}

function isValidSearchDateSpan (form) {
	
	// make sure that the departure date is later than the arrival date
	// build a temporary date string for the arrival date
	// date from the form is in the form of MMM dd, yyyy 
	// example: 2004 Jun 25

	// new Date needs format of Jun 25, 2004
	var tmpbegdatestr = form.curbegdate.value.substring(5,8) + ' ' + form.curbegdate.value.substring(9,11) + ',' + form.curbegdate.value.substring(0,4);
	var tmpbegdate = new Date(tmpbegdatestr);
	
	// new Date needs format of Jun 25, 2004
	var tmpenddatestr = form.curenddate.value.substring(5,8) + ' ' + form.curenddate.value.substring(9,11) + ',' + form.curenddate.value.substring(0,4);
	var tmpenddate = new Date(tmpenddatestr);
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("You must select a valid Arrival and Departure date span");
		form.action.value = "";
		form.curbegdate.focus();
	    return false;
	}
	else
		return true;
}

// rearrange the dipslay name
function rearrangeName (value) {
	// rearrange the curcontactname so it reads firstname, mi, lastname
	// example: Tester, Mark A. -> Mark A. Tester

	var name = value;
	
	// split the sentence into an array of words
	var a = value.split(/,/g); 

	// reverse the last and firstname and mi
	if (a && a.length > 1)
		name = a[1] + " " + a[0];
	
	return name;
}

// strips leading and tailing spaces
function stripSpaces(value) {
	var oldstr = value;
    var newstr;
    return (oldstr.replace(/^\W+/,'')).replace(/\W+$/,'');
}

// set the contact id from source form to destination form
function setContactID (contactid,contactname,destformid,dstformfieldfocus) {

	if (parent.document.getElementById(destformid) && contactid > 0) {

		parent.document.getElementById(destformid).curcontactid.value = contactid;
		parent.document.getElementById(destformid).curcontactname.value = contactname;

		// set name on revenue check, if it exists
		if (parent.document.getElementById(destformid).currevenuecheckname) {
			
			// if an individual owner, rearrange the display name
			if ((parent.document.getElementById(destformid).curownershiptypeid) && (parent.document.getElementById(destformid).curownershiptypeid.value == 1))
				parent.document.getElementById(destformid).currevenuecheckname.value = rearrangeName(contactname);
			else
				parent.document.getElementById(destformid).currevenuecheckname.value = contactname;
		}
		
		// set name on tax check, if it exists
		if (parent.document.getElementById(destformid).curtaxcheckname) {
			
			if ((parent.document.getElementById(destformid).curownershiptypeid) && (parent.document.getElementById(destformid).curownershiptypeid.value == 1))
				parent.document.getElementById(destformid).curtaxcheckname.value = rearrangeName(contactname);
			else
				parent.document.getElementById(destformid).curtaxcheckname.value = contactname;
		}
		
		// set business name, if it exists
		if (parent.document.getElementById(destformid).curbusinessname) {
			parent.document.getElementById(destformid).curbusinessname.value = contactname;
		}
		
		// set legal name, if it exists
		if (parent.document.getElementById(destformid).curlegalname) {
			parent.document.getElementById(destformid).curlegalname.value = contactname;
		}
		
		// set the focus now to the destination form to the specified field
		if (dstformfieldfocus)
			eval("document.getElementById('" + dstformfieldfocus + "').focus();");

	}
	else {
		alert("ERROR: unable to get and set the contact name and id");
	}

	return true;
	
}

// set the agent id from source form to destination form
function setAgentID (agentid,destformid,dstformfieldfocus) {

	if (parent.document.getElementById(destformid) && agentid > 0) {
		parent.document.getElementById(destformid).curagentid.value = agentid;
		
		// set the focus now to the destination form to the specified field
		if (dstformfieldfocus)
			eval("document.getElementById('" + dstformfieldfocus + "').focus();");

	}
	else {
		alert("ERROR: unable to get and set the agent id");
	}
	
	return true;
	
}

function autoComplete (field, select, property, forcematch) {
	var found = false;
	
	if (field == '') {
          return;
     }

     var allWords = select.options;
     var posLow = 0;
     var posHigh = select.options.length;
     var foundIt = false;
     var checks = 0;
	 
	 // do binary search for value
     while (posLow <= posHigh) {
          posMid = Math.floor((posLow + posHigh) / 2);
		  
          checks++;
          if (select.options[posMid][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
               found = true;
               break;
          } else {
               if (field.value.toUpperCase() < select.options[posMid].text.toUpperCase()) {
                    posHigh = posMid - 1;
               } else {
                    posLow = posMid + 1;
               }
          }
     }
	
	if (found) { select.selectedIndex = posMid; }
	else { select.selectedIndex = -1; }
	
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
		}
		
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[posMid][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}

// Replaces text with by in string
function replace(string,text,by) {

    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function loadmarcowindow(url,target) {

   if (target != '') {
		if ( top.opener && ! top.opener.closed ) {
			
			target.window.location.href = url;
		}
		else
			alert("opener window closed");
	}
    else
        window.location.href = url;
		
}

function isFilled(elm) {
	if (!elm || elm.value == "" || elm.value == null) return false;
   	else return true;
}

function isChecked(form,elementname) {
	
	var returnvalue = false;
    for(i=0; i<form.elements.length; i++) {
         var elem = form.elements[i]
		  if((elem.type == "checkbox" || elem.type == "radio") && elem.name == elementname) {
			   if(elem.checked == true)
				   returnvalue = true;
		  }
    }
	
	return returnvalue;
}

// select all checkboxes for All selection
function setAllCheckboxes(form,element){

	for(i=0; i<form.elements.length; i++) {
		
		var elem = form.elements[i];
			
		// check all checkboxes
		if(elem.type == "checkbox" && elem.name == element.name && element.checked == true) {

			if(elem.checked == false) {
				elem.checked = true;
			}
		}
		// uncheck all checkboxes of All is unchecked
		else if(elem.type == "checkbox" && elem.name == element.name && element.checked == false) {

			if(elem.checked == true) {
				elem.checked = false;
			}
		}
    }
	
	return true;
	
}

function doAction(action,url) {
	var newurl = url + "&action=" + action;
    window.location.href = newurl;
    return;
}

function confirmAction(action,url,message) {
    if(confirm(message))
		doAction(action,url);
    else
		window.location.href = url;

    return;
}

function confirmMarcoAction(form,message) {
	if(confirm(message))
		form.submit();
	else
		return false;
		
    return true;
}

function confirmMarcoActionNoSubmit(message) {
	if(confirm(message))
		return true;
	else
		return false;
}

// do delete using a url
// the A tag's href attribute when outside a CFOUTPUT tag, needs to be 
// <a href="#" onclick="return confirmMarcoActionURL('url.cfm','This is a message');">
// else within CFOUTPUT: <a href="##" onclick="return confirmMarcoActionURL('url.cfm','This is a message');">
function confirmMarcoActionURL(url,message) {
	var u = url;
	
	if(confirm(message))
		window.location.href = u;
	else
		return false;
		
	return true;

}

function pick (option,form) {
  var f = form;
  if (option != "") {
    document.forms[f].submit();
  }
}

function checkSeasonForm(form) {
	
	// check current Season Begin Date
	if (isFilled(form.curseasonbegdate) == false) {
	    alert("You must provide a Season Begin Date in the format yyyy-mm-dd");
	    form.curseasonbegdate.focus();
	    return false;
    }
	
	// check current Season End Date
	if (isFilled(form.curseasonenddate) == false) {
	    alert("You must provide a Season End Date in the format yyyy-mm-dd");
	    form.curseasonenddate.focus();
	    return false;
    }
	
	// check current Default Min Nights
	if (form.curdefminnights.selectedIndex == 0) {
	    alert("You must select a Default Minimum Nights");
	    form.curdefminnights.focus();
	    return false;
    }

	return true;
	
}

function checkSeasonSetForm(form) {
	
	// check current Season Set
	if (isFilled(form.curseasonsetid) == false) {
	    alert("You must provide a Season Set ID");
	    form.curseasonsetid.focus();
	    return false;
    }
	
	// check current Season Code Beg
	if (isFilled(form.curseasonsetbegcode) == false) {
	   alert("You must provide a Season Code Begin");
	    form.curseasonsetbegcode.focus();
	    return false;
    }
	
	// check current Season Code End
	if (isFilled(form.curseasonsetendcode) == false) {
	    alert("You must provide a Season Code End");
	    form.curseasonsetendcode.focus();
	    return false;
    }
	
	// check current Season Set Description
	if (isFilled(form.curseasonsetdesc) == false) {
	    alert("You must provide a Season Set Description");
	    form.curseasonsetdesc.focus();
	    return false;
    }
	
	return true;
	
}

function checkRateForm(form) {
	
	// check current Season Code
	if (form.currateseasonid.selectedIndex == 0) {
	    alert("You must select a Season");
	    form.currateseasonid.focus();
	    return false;
    }
	
	// check current persons min
	if (isFilled(form.curpersonsmin) == false) {
	   alert("You must provide a Persons Minimum");
	    form.curpersonsmin.focus();
	    return false;
    }
	
	// check current persons max
	if (isFilled(form.curpersonsmax) == false) {
	    alert("You must provide a Persons Max");
	    form.curpersonsmax.focus();
	    return false;
    }
	
	// check current rate
	if (isFilled(form.currate) == false) {
	    alert("You must provide a Rate");
	    form.currate.focus();
	    return false;
    }
	
	// check min nights
	if (isFilled(form.currateminnights) == false) {
	    alert("You must provide the Minimum number of Nights");
	    form.currateminnights.focus();
	    return false;
    }

	return true;
	
}

function checkImageForm(form) {
	
	// check current sort order
	if (isFilled(form.cursortorder) == false) {
	   alert("You must provide a Sort Order");
	    form.cursortorder.focus();
	    return false;
    }
	
	return true;
	
}

function checkTourForm(form) {
	
	// check current description
	if (isFilled(form.curdescription) == false) {
	   alert("You must provide a Description");
	    form.curdescription.focus();
	    return false;
    }
	
	return true;
	
}

function checkUnitAbstractForm(url,form) {
	
	// check current abstract
	if (isFilled(form.curabstract) == false) {
	    alert("You must provide an abstract");
		form.save.value = "";
	    form.curabstract.focus();
	    return;
    }

	window.location.href = url;
	
}

function checkUnitForm(form) {
	
	// check current unit code
	if (form.action.value == 'insert' && 
		form.curunitid.value == 0 && 
		form.curunitconfigid.value == 0) {
		if (isFilled(form.curunitcode) == false) {
			alert("You must provide a unit code");
			form.curunitcode.focus();
			return false;
		}
	}

	// check current unit name
	if (isFilled(form.curunitname) == false) {
	    alert("You must provide a unit name");
	    form.curunitname.focus();
	    return false;
    }
		
	// for villas, check these fields
	if (form.curresortid.value == 0) {
		
		// check current island
		if (form.curislandseasonset.selectedIndex == 0) {
			alert("You must select an Island");
			form.curislandseasonset.focus();
			return false;
		}
	
		// check current occupancy max
		if (isFilled(form.curoccupancymax) == false) {
			alert("You must provide an occupancy maximum");
			form.curoccupancymax.focus();
			return false;
		}
		
		// check current bedrooms
		if (isFilled(form.curbedrooms) == false) {
			alert("You must provide the number of bedrooms");
			form.curbedrooms.focus();
			return false;
		}
		
		// check current baths
		if (isFilled(form.curbaths) == false) {
			alert("You must provide the number of bathrooms");
			form.curbaths.focus();
			return false;
		}
	
		// check current deposit
		if (form.curdepositid.selectedIndex == 0) {
			alert("You must select a deposit");
			form.curdepositid.focus();
			return false;
		}
	}
	else {
		// make sure we have an interval set for a resort unit config
		if (form.curunitconfigid && form.curunitid && form.curunitconfigid.value == form.curunitid.value) {
			
			if (form.curintsetid.selectedIndex == 0) {
				alert("You must select an Interval Set");
				form.curintsetid.focus();
				return false;
			}
		}
	}
	
	return true;
	
}

function checkSubmitCatalogOrderForm(url,form) {
	
	//if(confirm('Are you sure you want to submit a catalog order?'))
		//form.submit();
	//else
		//window.location.href = url;
	
}

function checkContactForm(form) {
	
	// see if we are an individual
	if (form.curisindividual.value == 1) {
		
		// check current first name
		if (isFilled(form.curfname) == false) {
			alert("You must provide a First Name");
			form.curfname.focus();
			return false;
		}
		
		// check current last name
		if (isFilled(form.curlname) == false) {
			alert("You must provide a Last Name");
			form.curlname.focus();
			return false;
		}
	
	}
	else {
		
		// check current business anme
		if (isFilled(form.curbusinessname) == false) {
			alert("You must provide a Business Name");
			form.curbusinessname.focus();
			return false;
		}
		
	}
	// check current display 
	if (isFilled(form.curdisplayname) == false) {
	    alert("You must provide a Display Name");
	    form.curdisplayname.focus();
	    return false;
   	}
			
	return true;
	
}

function checkNewContactForm(form) {
	
	// check current business name
	if ((isFilled(form.curisindividual) == true) && (form.curisindividual.value == 0)) {
		if (isFilled(form.curbusinessname) == false) {
			alert("You must provide a Business Name");
			form.curbusinessname.focus();
			return false;
		}
	}
	else {
		if (isFilled(form.curfname) == false) {
			alert("You must provide a First Name");
			form.curfname.focus();
			return false;
		}
		// check current last name
		if (isFilled(form.curlname) == false) {
			alert("You must provide a Last Name");
			form.curlname.focus();
			return false;
		}
	}
		
	return true;
	
}

function checkOwnerContactForm(form) {
	
	// check current contact name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
   	}
	
	return true;
	
}

function checkManagerProfilerForm(url,form) {
	
	// check current company name
	if (isFilled(form.curcompany) == false) {
	    alert("You must provide a Company Name");
		form.save.value = "";
	    form.curcompany.focus();
	    return;
   	}
	
	// check current contact
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
		form.save.value = "";
	    form.curcontactid.focus();
	    return;
   	}
	
	// check current address 1
	if (isFilled(form.curadd1) == false) {
	    alert("You must provide an Address 1");
		form.save.value = "";
	    form.curadd1.focus();
	    return;
   	}
	
	// check current city
	if (isFilled(form.curcity) == false) {
	    alert("You must provide a City");
		form.save.value = "";
	    form.curcity.focus();
	    return;
   	}
	
	// check current zip code
	if (isFilled(form.curzipcode) == false) {
	    alert("You must provide a Zip Code");
		form.save.value = "";
	    form.curzipcode.focus();
	    return;
   	}
	
	// check current country
	if (form.curcountryid.selectedIndex == 0) {
	    alert("You must select a Country");
		form.save.value = "";
	    form.curcountryid.focus();
	    return;
   	}
	
	// check current phone
	if (isFilled(form.curphone) == false) {
	    alert("You must provide a Phone");
		form.save.value = "";
	    form.curphone.focus();
	    return;
   	}
	
	window.location.href = url;
	
}

function checkResortUnitForm(url,form) {
	
	// check current unit name
	if (isFilled(form.curunitname) == false) {
	    alert("You must provide a unit name");
		form.save.value = "";
	    form.curunitname.focus();
	    return;
    }
			
	window.location.href = url;
	
}

function checkFeeForm(form) {
	
	// check current fee id
	if (form.currestranstypeid.selectedIndex == 0) {
	    alert("You must select a fee");
	    form.currestranstypeid.focus();
	    return false;
    }
			
	return true;
	
}

function checkEmailForm(form) {
	
	// check current email
	if (isFilled(form.curemail) == false) {
	    alert("You must provide an email address");
	    form.curemail.focus();
	    return false;
    }
			
	return true;
	
}

function checkPhoneForm(form) {
	
	// check current phone type
	if (form.curphonetypeid.selectedIndex == 0) {
	    alert("You must select a Phone Number Type");
	    form.curphonetypeid.focus();
	    return false;
    }
	
	// check current phone number
	if (isFilled(form.curphonenumber) == false) {
	    alert("You must provide a Phone Number");
	    form.curphonenumber.focus();
	    return false;
    }
	
	return true;
	
}

// get the month value for a 3 character representation of the current month
function getMonthValue(monthabbrev) {
	
	var month_value = -1;
	for(i = 0; i < 12; i++) {
		if (month_names[i] == monthabbrev) {
			month_value = i;
			break;
		}
	}
	return month_value;
	
}

function checkOwnerForm(form) {
	
	// check current contact
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
		if (document.getElementById('keywordsid'))
	    	document.getElementById('keywordsid').focus();
	    return false;
   	}
	
	// check commission percentage
	if (isFilled(form.curcommpercent) == false) {
	    alert("You must provide a Commission Percentage.");
		form.curcommpercent.focus();
	    return false;
   	}
	
	// see if we have a villa or a fractional
	if (form.curresortid) {

		// see if we have a resort id
		if (form.curresortid.value > 0) {
			
			// check current rms unit num
			if (isFilled(form.currmsunitnum) == false) {
				alert("You must provide a RMS Unit Number");
				form.currmsunitnum.focus();
				return false;
			}
			// interval week checks
			if (form.curisgenericunit && form.curisgenericunit.value == 0) {
				
				// check interval selector
				if (form.curownerinterval && form.curownerinterval.selectedIndex == 0) {
					alert("You must select an Owner Interval");
					form.curownerinterval.focus();
					return false;
				}
			}
			// generic unit checks
			else {

				// make sure we have a value for the first night date
				if (form.curfirstnight && form.curfirstnight.value != '') {

					// determine the month integer value for the month, ex: Feb => 2-1=1
					var month_value = getMonthValue(form.curfirstnight.value.substring(5,8));
							
					if (month_value >= 0)
						var tmpbegdate = new Date(form.curfirstnight.value.substring(13,17),
						  month_value,
						  form.curfirstnight.value.substring(9,11));
					else {
						alert("Unable to determine month of First Night");
						form.curfirstnight.focus();
						return false;
					}
				}
				else {
					alert("Please select a First Night");
					form.curfirstnight.focus();
					return false;
				}

				// make sure we have a last night date
				if (form.curlastnight && form.curlastnight.value != '') {
					
					// determine the month integer value for the month, ex: Feb => 2-1=1
					var month_value = getMonthValue(form.curlastnight.value.substring(5,8));
							
					if (month_value >= 0)
						var tmpenddate = new Date(form.curlastnight.value.substring(13,17),
						  month_value,
						  form.curlastnight.value.substring(9,11));
					else {
							alert("Unable to determine month of Last Night");
							form.curlastnight.focus();
							return false;
						}
				}
				else {
					alert("Please select a Last Night");
					form.curlastnight.focus();
					return false;
				}
				
				// if difference in milliseconds between begin date and end date is less than or equal to 0, 
				// throw an alert box
				if ((tmpenddate.getTime() - tmpbegdate.getTime()) < 0) {
					alert("The Begin date selected is overlapping your End date. Please check your dates.");
					return false;
				}
				else {
					
					var nightsinspan =
						(Date.UTC(y2k(tmpenddate.getYear()),tmpenddate.getMonth(),tmpenddate.getDate(),0,0,0)
						- Date.UTC(y2k(tmpbegdate.getYear()),tmpbegdate.getMonth(),tmpbegdate.getDate(),0,0,0))/1000/60/60/24;

					if (nightsinspan >= 7) {
						alert("The date span you selected can not contain more than 7 nights. Please adjust your dates.");
						return false;
					}
					else
						return true;
					
				}
			}
		}
	}
	
	return true;
	
}

function checkOwnerTradeOutForm(url,form) {
	
	// check current begin date
	if (form.curnewbegdate.selectedIndex == 0) {
	    alert("You must select an Begin Date");
		form.save.value = "";
	    form.curnewbegdate.focus();
	    return;
    }
	
	// check current end date
	if (form.curnewenddate.selectedIndex == 0) {
	    alert("You must select an End Date");
		form.save.value = "";
	    form.curnewenddate.focus();
	    return;
    }
	
	window.location.href = url;
	
}

function checkIslandForm(form) {
	
	// check current island name
	if (isFilled(form.curislandname) == false) {
	    alert("You must provide island name");
	    form.curislandname.focus();
	    return false;
    }
	
	// check current island abbreviation
	if (isFilled(form.curislandabbrev) == false) {
	    alert("You must provide island abbreviation");
	    form.curislandabbrev.focus();
	    return false;
    }
	
	// check current country id
	if (form.curcountryid.selectedIndex == 0) {
	    alert("You must select a country");
	    form.curcountryid.focus();
	    return false;
    }
	
	// check current default season set id
	if (form.curdefseasonsetid.selectedIndex == 0) {
	    alert("You must select a default season set id");
	    form.curdefseasonsetid.focus();
	    return false;
    }
			
	return true;
	
}

function checkAddressForm(form) {
	
	// check current address type
	if (form.curaddresstypeid.selectedIndex == 0) {
	    alert("You must select an Address Type");
	    form.curaddresstypeid.focus();
	    return false;
    }
	
	// check current add1
	if (isFilled(form.curadd1) == false) {
	    alert("You must provide an Address 1");
		if (form.curadd1)
	   		form.curadd1.focus();
	    return false;
    }
	
	// check current city
	if (isFilled(form.curcity) == false) {
	    alert("You must provide a City");
		if (form.curcity)
	    	form.curcity.focus();
	    return false;
    }
	
	// check current country
	if (form.curcountryid.selectedIndex == 0) {
	    alert("You must select a Country");
		if (form.curcountryid)
	 	   form.curcountryid.focus();
	    return false;
    }
	
	return true;
	
}

function checkResortForm(form) {
	
	// check current resort name
	if (isFilled(form.curresortname) == false) {
	    alert("You must provide a resort name");
	    form.curresortname.focus();
	    return false;
    }

	// check current default season set
	if (form.curdefseasonsetid.selectedIndex == 0) {
	    alert("You must select a default season set");
	    form.curdefseasonsetid.focus();
	    return false;
    }
			
	return true;
	
}

// 2004-04-21 - mark - start
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDateValid (day,month,year) {
	
	// checks if date passed is valid
	// will accept dates in following format:
	// isDate(dd,mm,ccyy), or
	// isDate(dd,mm) - which defaults to the current year, or
	// isDate(dd) - which defaults to the current month and year.
	// Note, if passed the month must be between 1 and 12, and the
	// year in ccyy format.
	
	if (day <= 0) {
		alert("day: " + day + " month: " + month + " year: " + year);
		return false;
	}
	
	var today = new Date();
	
	year = ((!year) ? y2k(today.getYear()):year);
	
	month = ((!month) ? today.getMonth():month-1);
	
	if (!day) return false;
	
	var test = new Date(year,month,day);
	
	if ( (y2k(test.getYear()) == year) &&
		 (month == test.getMonth()) &&
		 (day == test.getDate()) )
		return true;
	else
		return false;
}

function checkSearchForm(form) {
	
	// dropdown menu format (pick one): check current destination, island, locations - Tour Dimensions - Scott 2010-02-01
	if (form.cursearchislandids.selectedIndex == 0) {
	    alert("You must select at least one island destination");
	    return false;
    }
    
	// checkbox format (pick multiple locations): check current destination, island, locations - Tour Dimensions - Scott 2010-02-01
	// if (!form.cursearchislandids || isChecked(form,"cursearchislandids") == false) {
	//     alert("You must select at least one island destination");
	//     return false;
   //  }
    
	// make sure that the departure date is later than the arrival date
		
	// to create a new Javascript Date object, it needs the yyyy-mm-dd incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curbegdate.value.substring(0,4),
							  form.curbegdate.value.substring(5,7)-1,
							  form.curbegdate.value.substring(8,10));
	
	var tmpenddate = new Date(form.curenddate.value.substring(0,4),
							  form.curenddate.value.substring(5,7)-1,
							  form.curenddate.value.substring(8,10));
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
	else
		return true;
		
}

function checkCountryForm(form) {
	
	// check current Display Name
	if (isFilled(form.curdisplayname) == false) {
	    alert("You must provide a Display Name");
	    form.curdisplayname.focus();
	    return false;
    }
	
	// check current Country Name
	if (isFilled(form.curcountryname) == false) {
	    alert("You must provide a Country Name");
	    form.curcountryname.focus();
	    return false;
    }

	// check current ISO
	if (isFilled(form.curiso) == false) {
	    alert("You must provide a two character ISO abbreviation");
	    form.curiso.focus();
	    return false;
    }
	
	// check current Hotel Tax Percentage
	if (isFilled(form.curhoteltaxpercentage) == false) {
	    alert("You must provide a Hotel Tax Percentage");
	    form.curhoteltaxpercentage.focus();
	    return false;
    }

	return true;
	
}
// 2004-05-03 mark - end

// 2004-05-17 mark - begin
function checkAgentForm(form) {
	
// check current Contact Name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
    }
	
	// check current Business Name
	if (isFilled(form.curbusinessname) == false) {
	    alert("You must select a Business Name");
	    form.curbusinessname.focus();
	    return false;
    }
	
	// check current Contact Name
	if (form.curagenttypeid.selectedIndex == 0) {
	    alert("You must select an Agent Type");
	    form.curagenttypeid.focus();
	    return false;
    }
	
	// check current Commission Percent
	if (isFilled(form.curcommissionpercent) == false) {
	    alert("You must provide a Commission Percent");
	    form.curcommissionpercentW.focus();
	    return false;
    }
	
	return true;
	
}
// 2004-05-17 mark - end

// 2004-05-20 mark - begin
function checkBusinessNameForm(url,form) {
	
	// check current Business Name
	if (isFilled(form.curbusinessname) == false) {
	    alert("You must provide a Business Name");
		form.save.value = "";
	    form.curbusinessname.focus();
	    return;
    }
	
	window.location.href = url;
	
}
// 2004-05-20 mark - end

// 2004-05-21 mark - begin
function checkManagerStaffForm(form) {
	
	// check current contact
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactid.focus();
	    return false;
   	}
	
	return true;
	
}
// 2004-05-21 mark - end

// 2004-05-24 mark - begin
function checkUnitVendorForm(form) {
	
	// check current vendor id
	if (form.curvendorid.selectedIndex == 0) {
	    alert("You must select a Vendor");
	    form.curvendorid.focus();
	    return false;
   	}
	
	return true;
	
}

function checkResVendorForm(form) {
	
	// check current vendor id
	if (form.curvendorid.selectedIndex == 0) {
	    alert("You must select a Vendor");
	    form.curvendorid.focus();
	    return false;
   	}
	
	return true;
	
}

function checkVendorForm(form) {
	
	// check current contact
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactid.focus();
	    return false;
   	}
	
	// check current biz name
	if (isFilled(form.curbusinessname) == false) {
	    alert("You must provide a Business Name");
	    form.curbusinessname.focus();
	    return false;
   	}
	
	// check current vendor type id
	if (form.curvendortypeid.selectedIndex == 0) {
	    alert("You must select a Vendor Type");
	    form.curvendortypeid.focus();
	    return false;
    }
	
	// check and make sure at least one vendor class check box was selected
	// if the checkboxes are present on the form
	if ((form.curispropertymanagement && isChecked(form,"curispropertymanagement") == false) &&
		(form.curisguestservice && isChecked(form,"curisguestservice") == false)) {
	    alert("You must select at least one Vendor Class");
	    return false;
    }
	
	return true;
	
}

function checkUnitContactForm(form) {
		
	// check current contact name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
   	}
	
	return true;
	
}

function checkAgentStaffForm(form) {
		
	// check current contact name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
   	}
	
	return true;
	
}

function checkVendorStaffForm(form) {
		
	// check current contact name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
   	}
	
	return true;
	
}

// check the catalog order form
function checkCatalogOrderForm(form) {
	
	// check current first name
	if (isFilled(form.curfname) == false) {
	    alert("You must provide a First Name");
	    form.curfname.focus();
	    return false;
   	}
	
	// check current last name
	if (isFilled(form.curlname) == false) {
	    alert("You must provide a Last Name");
	    form.curlname.focus();
	    return false;
   	}
	
	// check current email
	if (isFilled(form.curemail) == false) {
	    alert("You must provide an Email Address");
	    form.curemail.focus();
	    return false;
   	}
	
	// check current address 1
	if (isFilled(form.curadd1) == false) {
	    alert("You must provide an Address");
	    form.curadd1.focus();
	    return false;
   	}
	
	// check current city
	if (isFilled(form.curcity) == false) {
	    alert("You must provide a City");
	    form.curcity.focus();
	    return false;
   	}
	
	// check current zip code
	if (isFilled(form.curzipcode) == false) {
	    alert("You must provide a Zip Code");
	    form.curzipcode.focus();
	    return false;
   	}
	
	// check current country
	if (form.curcountry.selectedIndex == 0) {
	    alert("You must select a Country");
	    form.curcountry.focus();
	    return false;
   	}
	
	// for United States and Canada, a state selection is required
	if (form.curcountry.options[form.curcountry.selectedIndex].value == "UNITED STATES" || 
		form.curcountry.options[form.curcountry.selectedIndex].value == "CANADA") {

		if (form.curstate.selectedIndex == 0) {
			alert("You must select a State or Province");
			form.curstate.focus();
			return false;
		}
	}
	
	// check current home phone 1
	if (isFilled(form.curphonehome) == false) {
	    alert("You must provide a Phone number");
	    form.curphonehome.focus();
	    return false;
   	}
	
	return true;
}

function checkPartnerForm(form) {
	
	// check current contact
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactid.focus();
	    return false;
   	}
	
	// check current partenership percentage
	if (isFilled(form.curpartnerpercentage) == false) {
	    alert("You must provide a Percentage");
	    form.curpartnerpercentage.focus();
	    return false;
   	}
	
	return true;
	
}
// 2004-05-24 mark - end

// Performs a check on a given year to see if its a leap year
function isLeapYear(year) {                          
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true : false;
}

// Re-populate the departure days select list when departure month is changed
function checkDepartureDays(endday) {                              

	var today = new Date();                             
	var selected_month = document.getElementById('curendmonthselid').value;
	var selected_year = document.getElementById('curendyearselid').value;
	
	//Check the year... some browsers return a three digit equivalent
	if(today.getYear() < 1900) {                       
		this_year = (today.getYear() + 1900);
	}
	else {
		this_year = today.getYear();
	}
	
	// get the maximum number of days for the selected departure month
	if((isLeapYear(selected_year) == true) && (selected_month == 2)) {
		var max_days = 29;
	}
	else {
		var max_days = days_of_month[selected_month-1];
	}
	
	// re-build the departure day picker
	// re-build the arrival day picker
	// don't display any days in the past
	if (selected_year == this_year && selected_month == today.getMonth()+1)
		start_day = today.getDate();
	else
		start_day = 1; 
		
	var days = '<select class="picker" id="curenddayselid" name="curendday">';
	for(i=start_day ; i<=max_days ; i++) {
		if (i == endday)
			days += '<option value="' + i +'" selected>' + i + '</option>';
		else
			days += '<option value="' + i +'">' + i + '</option>';
	}
	days += '</select>'
	
	// set the DOM object for the departure day
	document.getElementById("curenddayid").innerHTML = days;

}

// Re-populate the departure months select list when departure year is changed
function checkDepartureMonths(endmonth,endday) {                            

	var today = new Date();
	
	//Check the year... some browsers return a three digit equivalent
	if(today.getYear() < 1900) {                       
		this_year = (today.getYear() + 1900);
	}
	else {
		this_year = today.getYear();
	}
	
	// re-build the departure month date picker
	// don't display any months in the past
	if (document.getElementById('curendyearselid').value == this_year)
		start_month = today.getMonth();
	else
		start_month = 0; 
	
	var mnth = '<select class="picker" id="curendmonthselid" name="curendmonth" onChange="checkDepartureDays();">';
	for(x=start_month ; x<12 ; x++) {
		if (x == endmonth)
			mnth += '<option value="' + (x+1) +'" selected>' + month_names[x] + '</option>';
		else
			mnth += '<option value="' + (x+1) +'">' + month_names[x] + '</option>';
	}
	mnth += '</select>';
	
	// set the DOM object for the departure month
	document.getElementById("curendmonthid").innerHTML = mnth;
	
	// re-build the departure days selector
	checkDepartureDays(endday);

}

// Re-populate the arrival days select list when arrival month is changed
function checkArrivalDays(begday) {                              

	var today = new Date(); 
	var selected_month = document.getElementById('curbegmonthselid').value;
	var selected_year = document.getElementById('curbegyearselid').value;
	
	//Check the year... some browsers return a three digit equivalent
	if(today.getYear() < 1900) {                       
		this_year = (today.getYear() + 1900);
	}
	else {
		this_year = today.getYear();
	}
	
	// get the maximum number of days for the selected arrival month
	if((isLeapYear(selected_year) == true) && (selected_month == 2)) {
		var max_days = 29;
	}
	else {
		var max_days = days_of_month[selected_month-1];
	}
	
	// re-build the arrival day picker
	// don't display any days in the past
	if (selected_year == this_year && selected_month == today.getMonth()+1)
		start_day = today.getDate();
	else
		start_day = 1; 
	
	var days = '<select class="picker" id="curbegdayselid" name="curbegday">';
	for(i=start_day ; i<=max_days ; i++) {
		if (i == begday)
			days += '<option value="' + i +'" selected>' + i + '</option>';
		else
			days += '<option value="' + i +'">' + i + '</option>';
	}
	days += '</select>';

	// set the DOM object for the arrival day
	if (document.getElementById("curbegdayid"))
		document.getElementById("curbegdayid").innerHTML = days;

}

// Re-populate the arrival months select list when arrival year is changed
function checkArrivalMonths(begmonth,begday) {                            
		
	var today = new Date();
	
	//Check the year... some browsers return a three digit equivalent
	if(today.getYear() < 1900) {                       
		this_year = (today.getYear() + 1900);
	}
	else {
		this_year = today.getYear();
	}
	
	// re-build the arrival month date picker                       
	// don't display any months in the past
	if (document.getElementById('curbegyearselid').value == this_year)
		start_month = today.getMonth();
	else
		start_month = 0; 
		
	var mnth = '<select class="picker" id="curbegmonthselid" name="curbegmonth" onChange="checkArrivalDays();">';
	for(x=start_month ; x<12 ; x++) {
		if (x == begmonth)
			mnth += '<option value="' + (x+1) +'" selected>' + month_names[x] + '</option>';
		else
			mnth += '<option value="' + (x+1) +'">' + month_names[x] + '</option>';
	}
	mnth += '</select>';
	
	// set the DOM object for the arrival date
	document.getElementById("curbegmonthid").innerHTML = mnth;
	
	// re-build the arrival days selector
	if (document.getElementById("curbegdayid"))
		checkArrivalDays(begday);

}

// initialize the date selectors
function initDateSelectors(begmonth,begday,endmonth,endday) {
	
	checkArrivalMonths(begmonth,begday);
	checkDepartureMonths(endmonth,endday);
}

// POP UP A PRE-FORMATTED EMAIL MESSAGE WINDOW
function popupMailto(to,cc,bcc,subject,body) {

    // BUILD MAIL MESSAGE COMPONENTS 
    var doc = "mailto:" + to + 
        "?cc=" + cc + 
        "&bcc=" + bcc + 
        "&subject=" + escape(subject) + 
        "&body=" + escape(body); 

    // POP UP EMAIL MESSAGE WINDOW
    window.location = doc; 
} 

function checkAvailabilityForm(form) {
	
	// make sure that the departure date is later than the arrival date
		
	// to create a new Javascript Date object, it needs the yyyy-mm-dd incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curbegyear.value,
							  form.curbegmonth.value,
							  form.curbegday.value);
	
	var tmpenddate = new Date(form.curendyear.value,
							  form.curendmonth.value,
							  form.curendday.value);
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
	else
		return true;
		
}

function checkResAvailabilityForm(form) {
	
	// make sure that the departure date is later than the arrival date
		
	// to create a new Javascript Date object, it needs the ddd, mmm dd, yyyy incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curarrivaldate.value.substring(13,17),
							  getMonthValue(form.curarrivaldate.value.substring(5,8)),
							  form.curarrivaldate.value.substring(9,11));
	
	var tmpenddate = new Date(form.curdeparturedate.value.substring(13,17),
							  getMonthValue(form.curdeparturedate.value.substring(5,8)),
							  form.curdeparturedate.value.substring(9,11));
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
	else
		return true;

	return true;		
}

// added 2004-11-18
function checkUnitApplianceForm(form) {
	
	// check current appliance type id
	if (form.curappliancetypeid.selectedIndex == 0) {
	    alert("You must select an Appliance Type");
	    form.curappliancetypeid.focus();
	    return false;
   	}
	
	// check current description
	if (isFilled(form.curdescription) == false) {
	    alert("You must provide a Description");
	    form.curdescription.focus();
	    return false;
   	}
	
	return true;
	
}

function checkExportMarcoContacts(form) {

	// make sure at least one check box was checked
	if (!form.groups || isChecked(form,"groups") == false) {
	    alert("You must select at least one group");
	    return false;
    }
	
	return true;

} 

function checkExportToCSVFile(form) {

	// make sure at least one check box was checked
	if (!form.groups || isChecked(form,"groups") == false) {
		alert("You must select at least one group.");
		return false;
	}
	
	return true;

} 

function checkExportDirectGuestsToCSVFile(form) {

	// make sure at least one check box was checked
	
	if (isChecked(form,"curshowdirectguests") == false && isChecked(form,"curshowresortguests") == false) {
		alert("You must select at least one type of direct guests.");
		return false;
	}
	
	return true;

} 

function checkVerisignPaymentForm(form) {

	// make sure we have a villa name
	if (form.VILLANAME.selectedIndex == 0) {
	    alert("You must select a Villa");
		form.VILLANAME.focus();
	    return false;
	}
	else {
		// also set USER1 (VillaName) form field to value of COMMMENT1
		form.USER1.value = form.VILLANAME.options[form.VILLANAME.selectedIndex].value;
		form.COMMENT1.value = form.VILLANAME.options[form.VILLANAME.selectedIndex].value;
    }
	
	// make sure we have an arrival year
	if (isFilled(document.getElementById('curbegyearselid')) == false) {
		alert("You must select an Arrival Year");
		form.curbegyearselid.focus();
	    return false;
	}
	
	// make sure we have an arrival date
	if (isFilled(document.getElementById('curbegmonthselid')) == false) {
		alert("You must select an Arrival Month");
		form.curbegmonthselid.focus();
	    return false;
	}
	
	// make sure we have an arrival date
	if (isFilled(document.getElementById('curbegdayselid')) == false) {
		alert("You must select an Arrival Day");
		form.curbegdayselid.focus();
	    return false;
	}

	// make sure we have an amount
	if (isFilled(form.AMOUNT) == false) {
	    alert("You must provide an Amount");
		form.AMOUNT.focus();
	    return false;
    }
	
	// create a date string from the three date selectors in the format of mmm dd, yyyy
	var datestr = month_names[document.getElementById('curbegmonthselid').value-1] + ' ' + document.getElementById('curbegdayselid').value + ', ' + document.getElementById('curbegyearselid').value;

	// also set USER2 (ArrivalDate) and COMMENT2 to the date str
	form.ARRIVALDATE.value = datestr;
	form.USER2.value = datestr;
	form.COMMENT2.value = datestr;
	
	// set CUSTID to a combination of the VillaName and the ArrivalDate
	form.CUSTID.value = form.USER1.value + ';' + form.USER2.value;
	
	// set USER3 to NOTES
	// make sure we have an amount
	if (isFilled(form.NOTES) != false) {
	    form.USER3.value = form.NOTES.value;
    }
	
	return true;

} 

// check Enterprise Staff form
function checkEnterpriseStaffForm(form) {
	
	// for save action, check the required fields
	if (! form.deletesubmit) {
		
		// check that we have a contact name
		if (isFilled(form.curcontactname) == false) {
			alert("You must select a Contact.");
			form.curcontactname.focus();
			return false;
		}
		
	}
	
	return true;
}

// check Password form
function checkPasswordForm(form) {
	
	// check that we have a password
	if (isFilled(form.curpassword) == false) {
	    alert("You must provide a Password.");
		form.curpassword.focus();
	    return false;
	}
	
	// check that we have a password confirm
	if (isFilled(form.curpassword2) == false) {
	    alert("You must provide a Confirm Password.");
		form.curpassword2.focus();
	    return false;
	}
	
	// check that the password and the confirm password are the same
	if (form.curpassword.value != form.curpassword2.value) {
	    alert("The Password and Confirm Password do not match.");
		form.curpassword.focus();
	    return false;
	}
	
}

// dynamically create an email address 
function dynBuildContactEmailAddress (contact,email,host) {
	document.write("<a href=" + "mail" + "to:" + email + "@" + host + ">" + contact + "</a>")
}

// check User form
function checkUserForm(form) {
	
	// for save action, check the required fields
	if (! form.deletesubmit) {
		
		// check that we have a contact name
		if (isFilled(form.curcontactname) == false) {
			alert("You must select a Contact.");
			form.curcontactname.focus();
			return false;
		}
		
		if (form.action.value == "insert") {
				
			// check that we have a password
			if (isFilled(form.curpassword) == false) {
				alert("You must provide a Password.");
				form.curpassword.focus();
				return false;
			}
			
			// check that we have a password confirm
			if (isFilled(form.curpassword2) == false) {
				alert("You must provide a Confirm Password.");
				form.curpassword2.focus();
				return false;
			}
			
			// check that the password and the confirm password are the same
			if (form.curpassword.value != form.curpassword2.value) {
				alert("The Password and Confirm Password do not match.");
				form.curpassword.focus();
				return false;
			}
	
		}
		
	}
	
	return true;
}

// check Enterprise Computer form
function checkEnterpriseComputerForm(form) {
	
	// for save action, check the required fields
	if (! form.deletesubmit) {
		
		// check that we have a contact name
		if (isFilled(form.curcontactname) == false) {
			alert("You must select a Contact.");
			form.curcontactname.focus();
			return false;
		}
		
		// check that we have a computer name
		if (isFilled(form.curcomputername) == false) {
			alert("You must provide a Computer Name.");
			form.curcomputername.focus();
			return false;
		}
		
	}
	
	return true;
}

// check Enterprise Distribution List form
function checkEnterpriseDistListForm(form) {
	
	// for save action, check the required fields
	if (! form.deletesubmit) {
		
		// check that we have a contact name
		if (isFilled(form.curdistributionlist) == false) {
			alert("You must provide a Distribution List name.");
			form.curdistributionlist.focus();
			return false;
		}
		
	}
	
	return true;
}

// check Enterprise Distribution List form
function checkEnterpriseDistListMemberForm (form) {
	
	// for save action, check the required fields
	if (!form.deletesubmit) {
		
		// check that we have a contact name
		if (isFilled(form.curcontactname) == false) {
			alert("You must select a Contact.");
			form.curcontactname.focus();
			return false;
		}
		
	}
	
	form.submit.value = "submit";
	
	return true;
}

// check Marco Email form
function checkMarcoEmailForm (form) {
		
	// check that we have either at least one to email address or at least
	// one distribution list chosen
	if ((!form.curdistlistemails || isChecked(form,"curdistlistemails") == false) && isFilled(form.to) == false) {
	    alert("You must select at least one Distribution list or provide at least one To recipient.");
	    return false;
    }
	
	// check that we have a subject
	if (isFilled(form.subject) == false) {
		alert("You must provide a Subject.");
		form.subject.focus();
		return false;
	}
	
	// save the user-entered file name so we can log it
	if (isFilled(form.subject) == true && form.realattachmentfilepath)
		form.realattachmentfilepath.value = form.attachmentfilepath.value;
	
	return true;
}

// set current email string values
function setEmailFormStrings (dstformid,value) {
	
	var toemailstr = '';
	
	// build the 'to' email string
	if (toemailstr.length == 0)
		toemailstr = value;
	else 
		toemailstr = toemailstr + ';' + value;
	
	// set the destination form 'to' email string
	if (toemailstr != '')
		if (!parent.document.getElementById(dstformid).to || parent.document.getElementById(dstformid).to.value == '')
			parent.document.getElementById(dstformid).to.value = toemailstr;
		else
			parent.document.getElementById(dstformid).to.value = parent.document.getElementById(dstformid).to.value + ';' + toemailstr;
					
	return true;
	
}

// set the email form data
function setMarcoEmailFormData (email,dstformid) {

	if (parent.document.getElementById(dstformid) && email != '') {
		setEmailFormStrings (dstformid,email);
		return true;
	}
	else {
		alert("An Email Address is not available for this contact. Please add one to the contact's profile.");
		if (document.getElementById('keywordsid')) 
			gdocument.getElementById('keywordsid').focus();
		return false;
	}
	
	return true;
}

// check resort contact form
function checkResortContactForm(form) {
	
	// check current contact name
	if (isFilled(form.curcontactname) == false) {
	    alert("You must select a Contact");
	    form.curcontactname.focus();
	    return false;
   	}
	
	return true;
	
}

// check resort avail form
function checkResortAvailForm(form) {

	// make sure we have at least one interval week selected
	var checked = false;
	for(i=0; i<form.elements.length; i++) {
		
		var elem = form.elements[i];

		if(elem.type == "checkbox") {
			if(elem.checked == true) {
				checked = true;
			}
		}
    }
	
	if (!checked) {
		alert("You must select at least one Interval period");
		return false;
	}
	else
		return true;
}

// check res resort avail form
function checkResResortAvailForm(form) {

	// make sure we have at least one interval week selected
	var checked = false;
	for(i=0; i<form.elements.length; i++) {
		
		var elem = form.elements[i];

		if(elem.type == "checkbox") {
			if(elem.checked == true) {
				checked = true;
			}
		}
    }
	
	if (!checked) {
		alert("You must select at least one Interval period");
		return false;
	}
		
	// confirm save action
	if (form.save && isFilled(form.save) == true) {
		if(confirm("You are about to change ALL nights of this Reservation. Proceed by selecting OK")) {
			form.submit();
			return true;
		}
		else
			return false;
	}

	return true;
	
}

// check quote form
function checkQuoteForm(form) {
	
	// check that we have either at least one to email address or at least
	// one distribution list chosen
	if (isFilled(form.to) == false) {
	    alert("You must provide at least one To recipient.");
	    return false;
    }
	
	// check that we have a subject
	if (isFilled(form.subject) == false) {
		alert("You must provide a Subject.");
		form.subject.focus();
		return false;
	}
	
	if (isFilled(form.attachmentfilepath) == true) 
		form.realattachmentfilepath.value = form.attachmentfilepath.value;

	return true;
	
}

// called from resortavail.cfm
function checkResortAvailabilityForm(form) {
	
	// check current unit config id
	if (form.curunitconfigid.selectedIndex == 0) {
	    alert("You must select a Unit Configuration");
	    form.curunitconfigid.focus();
	    return false;
    }
	
	// if we are not searching all dates, validate date picker fields
	if (whichSubmit.value == "Search") {
		
		// check current begin date
		if (isFilled(form.curspanstartdate) == false) {
				alert("You must select an Arrival Date");
				form.curspanstartdate.focus();
				return false;
			}
		
		// check current span end date
		if (isFilled(form.curspanenddate) == false) {
				alert("You must select a Departure Date");
				form.curspanenddate.focus();
				return false;
			}
		
		// to create a new Javascript Date object, it needs the ddd, mmm dd, yyyy incoming
		// date to be of the format yyyy,mm,dd
		var tmpbegdate = new Date(form.curspanstartdate.value.substring(13,17),
									getMonthValue(form.curspanstartdate.value.substring(5,8)),
									form.curspanstartdate.value.substring(9,11));
		
		var tmpenddate = new Date(form.curspanenddate.value.substring(13,17),
									getMonthValue(form.curspanenddate.value.substring(5,8)),
									form.curspanenddate.value.substring(9,11));
		
		// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
		// throw an alert box
			if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
			alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
				return false;
		}
		else
			return true;
		
	}
	
	return true;
	
}


function checkResResortAvailabilityForm(form) {
	
	// check current begin date
	if (isFilled(form.curspanbegindate) == false) {
	    alert("You must select an Arrival Date");
	    form.curspanstartdate.focus();
	    return false;
    }
	
	// check current span end date
	if (isFilled(form.curspanenddate) == false) {
	    alert("You must select a Departure Date");
	    form.curspanenddate.focus();
	    return false;
    }
	
	// to create a new Javascript Date object, it needs the ddd, mmm dd, yyyy incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curspanbegindate.value.substring(13,17),
							  getMonthValue(form.curspanbegindate.value.substring(5,8)),
							  form.curspanbegindate.value.substring(9,11));
	
	var tmpenddate = new Date(form.curspanenddate.value.substring(13,17),
							  getMonthValue(form.curspanenddate.value.substring(5,8)),
							  form.curspanenddate.value.substring(9,11));
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
	else
		return true;
		
	return true;
	
}

// check authentication login
function checkLoginForm(form) {
	
	// check current email
	if (isFilled(form.username) == false) {
	    alert("You must provide a User Name");
	    form.username.focus();
	    return false;
    }
	
	// check current password
	if (isFilled(form.password) == false) {
	    alert("You must provide a Password");
	    form.password.focus();
	    return false;
    }
	
	return true;
	
}

// check quote contact form
function checkQuoteContactForm(form) {

	// check current contact id
	if (isFilled(form.curcontactid) == false || (form.curcontactid && form.curcontactid.value == 0)) {
	    alert("You must select a Contact");
	    document.getElementById('keywordsid').focus();
	    return false;
    }
	
	return true;
	
}

// check contact select form
function checkContactSelectForm(form) {
	
	// check current contact id
	if (form.curcontactid.selectedIndex == 0) {
	    alert("You must select a Contact");
	    form.curcontactid.focus();
	    return false;
    }
	
	// this is for IE to handle enter key to submit the form
	form.continuesubmit.value = "continue";

	return true;
	
}

function checkVeritySearchForm(form) {
	
	// check current keywords
	if (isFilled(form.keywords) == false) {
	    alert("You must provide at least one Keyword.");
	    form.keywords.focus();
	    return false;
    }
	else {
		var keyword = document.getElementById('keywordsid').value;
		// check for reserved words - and, or, not
		if (keyword.toLowerCase() == 'and' || keyword.toLowerCase() == 'or' || keyword.toLowerCase() == 'not') {
			alert("'and', 'not', and 'or' are reserved words. Please try another keyword.");
			form.keywords.focus();
	   		return false;
		}
	}
	
	return true;
	
}

function checkEnterKeyEvent (e,form) {
	
	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
    	form.search.click();
        return false;
    }
    else 
		return true;
				  
}

function checkEntNoteForm(form) {
	
	// check current note title
	if (isFilled(form.curtitle) == false) {
	    alert("You must provide a Title");
	    form.curtitle.focus();
	    return false;
    }

	// for a status of open, make sure we have at least one assignee
	if (form.curnotestatusid) {
		// for an open note, you just have at least one assigned contact id
		if (form.curnotestatusid.options[form.curnotestatusid.selectedIndex].value > 1 && (!form.curassignedcontactids || isChecked(form,'curassignedcontactids') == false)) {
			alert("You must select at least one Assigned Contact");
			return false;
		}
	}
	
	// NOTE: this does not work because active edit
	// creates this textarea as a hidden DOM object
	// and I can't figure out how to get a handle on its value
	// to check to see if it has a value or not...
	
	// check current note		
	//if (isFilled(form.curnote) == false) {
	    //alert("You must provide a Note");
	    //form.curnote.focus();
	    //return false;
    //}
	
	return true;
	
}

function checkPhotographerForm(form) {
	
	// check current Photographer display name
	if (isFilled(form.curlabel) == false) {
	    alert("You must provide a Photographer Display Name");
	    form.curlabel.focus();
	    return false;
    }
	
	return true;
	
}

// this function will clear any form field
// value is optional
function clearFormField(form,fieldid,value) {

	var val = '';
	// if a default value is passed, set the form field's value
	if (value)
		val = value;
		
	
	if (form && fieldid) {
		if(confirm('Are you sure you want to clear this form field?')) {
			eval("document.getElementById('" + fieldid + "').value = val");
			eval("document.getElementById('" + fieldid + "').focus()");
			return true;
		}
		else
			return false;
	}
	else {
		alert("Unable to determine form or form element id.");
	    return false;
	}
	
	return true;

}

// this function will toggle a DIV section to be visible or hidden
function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

// this function will make a hidden DIV section visible
function displayLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "block";
	}
}

// this function will swap one layer for another (hide Layer1 and display Layer1)
function swapLayers(Layer1,Layer2) {
	if (document.getElementById) {
		// this is the way the standards work
		var styleLayer1 = document.getElementById(Layer1).style;
		var styleLayer2 = document.getElementById(Layer2).style;
		styleLayer1.display = "none";
		styleLayer2.display = "block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var styleLayer1 = document.all[Layer1].style;
		var styleLayer2 = document.all[Layer2].style;
		styleLayer1.display = "none";
		styleLayer2.display = "block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var styleLayer1 = document.layers[Layer1].style;
		var styleLayer2 = document.layers[Layer2].style;
		styleLayer1.display = "none";
		styleLayer2.display = "block";
	}
}

// return integer representation of the three letter month abbreviation
function getMonthValue(monthabbrev) {
	
	var month_value = -1;
	for(i = 0; i < 12; i++) {
		if (month_names[i] == monthabbrev) {
			month_value = i;
			break;
		}
	}
	return month_value;
	
}

// check reservation form
function checkResForm(form) {

	// check number of adults
	if (form.curadults.selectedIndex == 0) {
		alert("You must select a non-zero value for Adults");
		form.curadults.focus();
		return false;
	}
	
	// check guest country
	if (form.curcountryid.selectedIndex == 0) {
		alert("You must select a Guest Country");
		form.curcountryid.focus();
		return false;
	}
	
	// check guest state
	if (form.curstateid && form.curstateid.selectedIndex == 0) {
		alert("You must select a Guest State");
		form.curstateid.focus();
		return false;
	}
	
	return true;
	
}

// check reservation night form
function checkResNightForm(form) {

	// this flags to submit the form when no errors or all alerts have been confirmed
	var submitform = false;
	
	if (form.curresnightguests.selectedIndex == 0) {
	    alert("You must provide a non-zero number of Guests");
	    form.curresnightguests.focus();
	    return false;
    }
	
	if (isFilled(form.curresnightrate) == false) {
		alert("You must provide a Nightly Rate");
		form.curresnightrate.focus();
		return false;
	}
	
	// provide an alert when set all is checked
	if (form.cursetall && isChecked(form,"cursetall") == true) {
		if(confirm("You are about to change ALL nights of this Reservation. Proceed by selecting OK"))
			submitform = true;
		else {
			form.cursetall.focus();
			return false;
		}
	}
	
	// provide an alert when user defined rate is selected
	if (form.curusemarcorate) {
		
		for(var i = 0; i < form.curusemarcorate.length; i++) {
			if(form.curusemarcorate[i].checked && form.curusemarcorate[i].value == 0) {
				if(confirm("You are about to use a user-defined Nightly Rate. Proceed by selecting OK")) {
					submitform = true;
				}
				else 
					return false;
			}
		}
	}
	
	if (submitform)
		form.submit();
		
	return true;
	
}

// check reservation availability form
function checkResAvailForm(form) {
	
	// make sure that the departure date is later than the arrival date
		
	// to create a new Javascript Date object, it needs the yyyy-mm-dd incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curbegyear.value,
							  form.curbegmonth.value,
							  form.curbegday.value);
	
	var tmpenddate = new Date(form.curendyear.value,
							  form.curendmonth.value,
							  form.curendday.value);
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
		
	return true;
	
}


// check reservation villa form
function checkResVillaForm(form) {

	// make sure that the departure date is later than the arrival date
		
	// to create a new Javascript Date object, it needs the yyyy-mm-dd incoming
	// date to be of the format yyyy,mm,dd
	var tmpbegdate = new Date(form.curbegyear.value,
							  form.curbegmonth.value,
							  form.curbegday.value);
	
	var tmpenddate = new Date(form.curendyear.value,
							  form.curendmonth.value,
							  form.curendday.value);
	
	// if difference in milliseconds between arrival date and departure date is less than or equal to 0, 
	// throw an alert box
   	if ((tmpenddate.getTime() - tmpbegdate.getTime()) <= 0) {
		alert("The Departure date selected is overlapping your Arrival date. Please check your travel dates.");
	    return false;
	}
	
	if (!form.curunitid || isChecked(form,"curunitid") == false) {
	    alert("You must select a Villa");
	    return false;
    }
	
	if(confirm("You are about to change the Villa and Arrival and Departure dates for this reservation. Proceed by selecting OK"))
		form.submit();
	else 
		return false;
		
}

// dynamically calculate the reservation charge amount for flat amounts
function calcFlatChargeAmount(form) {

	if (!form.currestranstypeid || form.currestranstypeid.selectedIndex == 0) {
		alert("You must select a Charge type");
		form.currestranstypeid.focus();
	    return false;
	}
	
	// calculate the charge amount
	var fieldvaluestr = form.currestranstypeid.options[form.currestranstypeid.selectedIndex].value;
	var fieldvalues = fieldvaluestr.split(/_/g)

	var nightlyamount = fieldvalues[1];

	// make sure we have a value for the first night date
	if (form.curfirstnight && form.curfirstnight.value != '') {

		// determine the month integer value for the month, ex: Feb => 2-1=1
		var month_value = getMonthValue(form.curfirstnight.value.substring(5,8));
				
		if (month_value >= 0)
			var tmpbegdate = new Date(form.curfirstnight.value.substring(13,17),
			  month_value,
			  form.curfirstnight.value.substring(9,11));
		else {
			alert("Unable to determine month of First Night");
			form.curfirstnight.focus();
			return false;
		}
	}
	else {
		alert("Please select a First Night");
		form.curfirstnight.focus();
		return false;
	}

	// make sure we have a last night date
	if (form.curlastnight && form.curlastnight.value != '') {
		
		// determine the month integer value for the month, ex: Feb => 2-1=1
		var month_value = getMonthValue(form.curlastnight.value.substring(5,8));
				
		if (month_value >= 0)
			var tmpenddate = new Date(form.curlastnight.value.substring(13,17),
			  month_value,
			  form.curlastnight.value.substring(9,11));
		else {
				alert("Unable to determine month of Last Night");
				form.curlastnight.focus();
				return false;
			}
	}

	// if difference in milliseconds between begin date and end date is less than or equal to 0, 
	// throw an alert box
	if ((tmpenddate.getTime() - tmpbegdate.getTime()) < 0) {
		alert("The Begin date selected is overlapping your End date. Please check your dates.");
		return false;
	}
	else {
		
		var nightsinspan =
			(Date.UTC(y2k(tmpenddate.getYear()),tmpenddate.getMonth(),tmpenddate.getDate(),0,0,0)
			- Date.UTC(y2k(tmpbegdate.getYear()),tmpbegdate.getMonth(),tmpbegdate.getDate(),0,0,0))/1000/60/60/24;

		var totalamount = (nightsinspan+1)*nightlyamount;
		
		if (totalamount > 0) {
			document.getElementById("chargeamountid").value = formatCurrency(totalamount);
			return true;
		}
		else {
			alert("Unable to determine amount of charge");
			return false;
		}
	}
	
	return true;
	
}

function formatCurrency(num) {
	
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);

	cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
		cents = "0" + cents;

	return (((sign)?'':'-') + num + '.' + cents);
	
}

// dynamically calculate the reservation charge amount for percentage amounts
function calcPercentageChargeAmount(form) {

	if (!form.currestranstypeid || form.currestranstypeid.selectedIndex == 0) {
		alert("You must select a Charge type");
		form.currestranstypeid.focus();
	    return false;
	}
	
	// calculate the charge amount
	var fieldvaluestr = form.currestranstypeid.options[form.currestranstypeid.selectedIndex].value;
	var fieldvalues = fieldvaluestr.split(/_/g)

	var totaladjgrossrent = fieldvalues[1];

	if (!form.curpercentage) {
		alert("You must select a Percentage");
		form.curpercentage.focus();
	    return false;
	}
	
	// if the charge type has a percentage defined, use to to calc the amount
	var totalamount = 0;
	if (form.curpercentage && form.curpercentage.selectedIndex > 0)
		totalamount = form.curpercentage.options[form.curpercentage.selectedIndex].value * fieldvalues[1] / 100.0;

	if (totalamount > 0) {
		document.getElementById("chargeamountid").value = formatCurrency(totalamount);
		return true;
	}
	else {
		alert("Unable to determine amount of charge");
		return false;
	}

	return true;
	
}

function checkResChargeForm(form) {
	
	// check res charge type
	if (form.currestranstypeid.selectedIndex == 0) {
	    alert("You must select a Charge Type.");
	    form.currestranstypeid.focus();
	    return false;
    }
	
	// check charge amount
	if (isFilled(form.curamount) == false || form.curamount.value <= 0) {
		alert("You must provide a Amount in US Dollars greater than 0.00");
		form.curamount.focus();
		return false;
	}
	
	return true;
}

function checkResAirInfoForm(form) {
	
	// check airport escort pull down
	if (form.curairportescortvendorid.selectedIndex == 0) {
	    alert("You must select an Airport Escort");
	    form.curairportescortvendorid.focus();
	    return false;
    }
	
	// check traveling party last name
	if (isFilled(form.curguestpartylastname) == false) {
		alert("You must provide the Traveling Party's Last Name");
		form.curguestpartylastname.focus();
		return false;
	}
	
	// check arrival air vendor
	if (form.curarrivalairvendorid.selectedIndex == 0) {
	    alert("You must select an Arrival Airline");
	    form.curarrivalairvendorid.focus();
	    return false;
    }
	
	// check arrival flight number
	if (isFilled(form.curarrivalflightnum) == false) {
		alert("You must provide an Arrival Flight Number");
		form.curarrivalflightnum.focus();
		return false;
	}
	
	// check departure air vendor
	if (form.curdepartureairvendorid.selectedIndex == 0) {
	    alert("You must select an Departure Airline");
	    form.curdepartureairvendorid.focus();
	    return false;
    }
	
	// check departure flight number
	if (isFilled(form.curdepartureflightnum) == false) {
		alert("You must provide an Departure Flight Number");
		form.curdepartureflightnum.focus();
		return false;
	}
	
	// check total number of traveling guests
	if (form.curtotalguests.selectedIndex == 0) {
	    alert("You must select the number of Guests to be Transported");
	    form.curtotalguests.focus();
	    return false;
    }
	
	// if cars rented, we need the number of cars to be rented
	if (form.curcarrentalvendorid && form.curcarrentalvendorid.selectedIndex > 0) {
		if (form.curtotalcarsrented.selectedIndex == 0) {
			alert("You must select the number of Cars to be Rented");
			form.curtotalguests.focus();
			return false;
		}
	}
	
	return true;
	
}

function confirmBookIt(form,message) {
	if(confirm(message))
		return true;
	else
		return false;
}

// Replaces text with by in string
function replace(string,text,by) {

    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function setSelectContactPickerIFrame (form,layer1,layer2,keywords,searchtype,pickergroup,destformid,pickertype,destformfieldfocus) {
	
	if (layer1 && layer2)
		swapLayers(layer1,layer2);
		
	window.frames[form].location.href = 'iframe_selectcontactform.cfm?search=search&keywords=' + keywords + '&searchtype=' + searchtype + '&curpickergroup=' + pickergroup + '&destformid=' + destformid + '&curdestformfieldfocus=' + destformfieldfocus + '&curpickertype=' + pickertype;
	
}

// when profiling a unit, when an island is selected, pre-select the
// season set
function setSeasonSetID() {

	if (document.getElementById('curislandseasonset') && document.getElementById('curislandseasonset').selectedIndex != 0) {

		var items = document.getElementById('curislandseasonset').value.split(":");
		var islandid = items[0];
		var seasonsetid = items[1];
		
		for (i=0; i < document.getElementById('formseasonsetid').options.length; i++) {
			if (document.getElementById('formseasonsetid').options[i].index == seasonsetid) {
				document.getElementById('formseasonsetid').selectedIndex = document.getElementById('formseasonsetid').options[i].index; 
			}
		}

    }
}

// on rateform, when a season is selected on insert mode, set min nights
function setSeasonMinNights() {
	if (document.getElementById('currateseasonid')) {
		var items = document.getElementById('currateseasonid').value.split(":");
		var seasonid = items[0];
		var minnights = items[1];
		document.getElementById('currateminnightsid').value = minnights; 
	}
	
}

// this function dynamically creates a state selector form widget
// depending on which country was selected
function displayStateSelector(countryselector,stateid,statenames,stateids) {
	
	var countryid = countryselector.options[countryselector.selectedIndex].value; // selected country id
	
	if (statenames[countryid] && statenames[countryid].length > 0) {
		
		var states = '<select class="picker" id="curstateid_id" name="curstateid">';
		states += '<option value="0">-- select a state --</option>';
		for(i=0 ; i<=statenames[countryid].length-1 ; i++) {
			if (stateid == stateids[countryid][i])
				states += '<option value="' + stateids[countryid][i] +'" selected>' + statenames[countryid][i] + '</option>';
			else
				states += '<option value="' + stateids[countryid][i] +'">' + statenames[countryid][i] + '</option>';
		}
		states += '</select>'
		document.getElementById("stateselector_id").style.visibility='visible';
		document.getElementById("stateselectorlabel_id").style.visibility='visible';
	}
	else {
		var states = '<select class="picker" id="curstateid_id" name="curstateid"></select>';
		document.getElementById("stateselector_id").style.visibility='hidden';
		document.getElementById("stateselectorlabel_id").style.visibility='hidden';
	}
	
	// set the DOM object for the departure day
	document.getElementById("stateselector_id").innerHTML = states;
	
}

// 2008-09-14 mark - begin
function checkUnitForm(form) {
	
	// check unit code field
	if (isFilled(form.curunitcode) == false) {
		alert("You must provide a Unit Code");
		form.curunitcode.focus();
		return false;
	}
	else {
		// check unit code field for illegal characters
		if (form.curunitcode.value.match(/["'\s/\\:*\?<>\|]/)) { 
			alert("The Unit Code cannot contain a space, single or double quote, or /\\:*?<>|");
			form.curunitcode.focus();
			return false;
		}
	}
	
	return true;
	
}
// 2008-09-14 mark - end

// Swap Div - Scott 2010-03-01

function showonlyone(thechosenone) {
      var newboxes = document.getElementsByTagName("div");
            for(var x=0; x<newboxes.length; x++) {
                  name = newboxes[x].getAttribute("name");
                  if (name == 'newboxes') {
                        if (newboxes[x].id == thechosenone) {
                        newboxes[x].style.display = 'block';
                  }
                  else {
                        newboxes[x].style.display = 'none';
                  }
            }
      }
}

