function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status = msgStr;
  document.MM_returnValue = true;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	if (selObj.options[selObj.selectedIndex].value != "") {
		eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
		if (restore) { selObj.selectedIndex = 0; }
	}
}

function focused(obj) {

	var isTextField = obj.type;
	
	if(isTextField && (isTextField == "text" || isTextField == "password" || isTextField == "textarea")) {
		obj.className = 'text_field_highlite';
	}
	     
	     
	if (obj.value == obj.defaultValue) {
		obj.value = "";
	}
}

function blurred(obj) {

	var isTextField = obj.type;
	
	if(isTextField && (isTextField == "text" || isTextField == "password" || isTextField == "textarea")) {
		obj.className = 'text_field';
	}
	    
	if (obj.value == "") {
		obj.value = obj.defaultValue;
	}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
  if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
  obj.visibility=v; }
}

function newWindow(pageSource) {
	leWindow = window.open(pageSource, 'newWindow', 'toolbar=no,width=480,height=500,status=0,location=yes,scrollbars=0,left=15,top=20,resizable=0,menubar=0');
}

function newWindowSized(pageSource, thisWidth, thisHeight) {
	var windowDetails = "toolbar=no,width=" + thisWidth + ",height=" + thisHeight + ",status=no,location=no,scrollbars=yes,left=15,top=20,resizable=yes,menubar=no";
	leWindow = window.open(pageSource, 'newWindow', windowDetails);
}

function emailCheck (emailStr) { 
	var checkTLD=0; 
	var knownDomsPat=/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|cc|tv)$/; 
	var emailPat=/^(.+)@(.+)$/; 
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 
	var validChars="\[^\\s" + specialChars + "\]"; 
	var quotedUser="(\"[^\"]*\")"; 
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 
	var atom=validChars + '+'; 
	var word="(" + atom + "|" + quotedUser + ")"; 
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 
	var matchArray=emailStr.match(emailPat); 
	if (matchArray==null) { 
		alert("The Email Address Is Invalid"); 
		return false; 
	} 
	var user=matchArray[1]; 
	var domain=matchArray[2]; 
	for (i=0; i<user.length; i++) { 
		if (user.charCodeAt(i)>127) { 
			alert("The Username Contains Invalid Characters."); 
			return false; 
		} 
	} 
	for (i=0; i<domain.length; i++) { 
		if (domain.charCodeAt(i)>127) { 
			alert("Ths Domain Name Contains Invalid Characters."); 
			return false; 
		} 
	} 
	if (user.match(userPat)==null) { 
		alert("The Username Is Invalid."); 
		return false; 
	} 
	var IPArray=domain.match(ipDomainPat); 
	if (IPArray!=null) { 
	for (var i=1;i<=4;i++) { 
		if (IPArray>255) { 
			alert("The Destination IP Address Is Invalid."); 
			return false; 
		} 
	} 
	return true; 
	} 
	var atomPat=new RegExp("^" + atom + "$"); 
	var domArr=domain.split("."); 
	var len=domArr.length; 
	for (i=0;i<len;i++) { 
		if (domArr[i].search(atomPat)==-1) { 
			alert("The Domain Name Is Invalid."); 
			return false; 
		} 
	} 
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) { 
		alert("The Domain Name Extension Is Invalid"); 
		return false; 
	} 
	if (len<2) { 
		alert("The Address Is Missing A Hostname."); 
		return false; 
	}
	return true;
} 

function dollar_format(y) {
    max_length=12;                         //max_length= length of text input
    spacing="               ";
    x=Math.floor(y);
    xx=y-x;
    xx=xx+"00.000"                 //xx=the cents only (with zeroes).
    a=xx.indexOf(".");
    q=x+xx.substring(a,a+4);
    ql= (q.length<max_length)?(max_length-q.length):0;   
    r=(y)?spacing.substring(0,ql)+q:"";
    last_digit= r.substring(r.length-1, r.length+1);
    r = r.substring(0,r.length-1);
    if (parseInt(last_digit) >= 5) {r = parseFloat(r) + parseFloat(.01) }
    r = "" + r;
    a=r.indexOf(".");
    if (r.length < a+3) {r = r + "0";}
    rLeft = r.substring(0,a);
    rRight = r.substring(a,a+3);
    r=rLeft + rRight;
    r = "$" + trim(r);
    return (r);
}

function showItem(this_item) {
	if (this_item.style.display == 'none') {
		this_item.style.display = "";
    } else {
		this_item.style.display = "none";
    }
}

function confirmDelete(formName, this_item)
{
	var confirmString = "Are you sure you delete this " + this_item + "?";
	var agree = confirm(confirmString);
	if (agree) {
		if (formName.deleteMode) {
			formName.mode.value = formName.deleteMode.value;
		} else {
			formName.mode.value = "delete";
		}
		return true;
	} else {
		return false;
	}
}

function changeMonth(mode) {

	var newValue;

	if (mode == "in") {
		if (document.check_avail.check_in_month.selectedIndex > document.check_avail.check_out_month.selectedIndex) {
			document.check_avail.check_out_month.selectedIndex = document.check_avail.check_in_month.selectedIndex;
		}
	} else {
		if (document.check_avail.check_in_month.selectedIndex == 11 && document.check_avail.check_out_month.selectedIndex == 0) {
			document.check_avail.check_out_year.selectedIndex = document.check_avail.check_in_year.selectedIndex + 1;
		} else {	
			if (document.check_avail.check_in_month.selectedIndex > document.check_avail.check_out_month.selectedIndex) {
				document.check_avail.check_in_month.selectedIndex = document.check_avail.check_out_month.selectedIndex;
			}
		}
	}
}

function changeMonthOld(mode) {

	var newValue;

	if (mode == "in") {
		if (document.check_avail.check_in_month.selectedIndex > document.check_avail.check_out_month.selectedIndex) {
			document.check_avail.check_out_month.selectedIndex = document.check_avail.check_in_month.selectedIndex;
		}
	} else {
		if (document.check_avail.check_in_month.selectedIndex > document.check_avail.check_out_month.selectedIndex) {
			document.check_avail.check_in_month.selectedIndex = document.check_avail.check_out_month.selectedIndex;
		}
	}
}

function changeDay(mode) {
	var newValue;
	if (document.check_avail.check_in_day.selectedIndex == 0) {
		document.check_avail.check_in_day.selectedIndex = 1;
	}
	if (document.check_avail.check_out_day.selectedIndex == 0) {
		document.check_avail.check_out_day.selectedIndex = 1;
	}
	
	if (mode == "in") {
		 if (document.check_avail.check_in_month.selectedIndex == 1 && document.check_avail.check_in_day.selectedIndex > 28) {
			if (document.check_avail.check_in_year.selectedIndex == 1) {
				document.check_avail.check_in_day.selectedIndex = 29;
			} else {
				document.check_avail.check_in_day.selectedIndex = 28;
			}
			document.check_avail.check_out_month.selectedIndex = 2;
			}
		if ((document.check_avail.check_in_month.selectedIndex == 8 || document.check_avail.check_in_month.selectedIndex == 3 || document.check_avail.check_in_month.selectedIndex == 10) && document.check_avail.check_in_day.selectedIndex > 30) {
			document.check_avail.check_in_day.selectedIndex = 30;}
		if (document.check_avail.check_in_day.selectedIndex >= document.check_avail.check_out_day.selectedIndex) {
			if (document.check_avail.check_in_month.selectedIndex == 1 && document.check_avail.check_in_day.selectedIndex == 28) {
				var inNum = 1;
			} else if (document.check_avail.check_in_month.selectedIndex == 1 && document.check_avail.check_in_day.selectedIndex > 28) {
				var inNum = 1;
				if (document.check_avail.check_in_year.selectedIndex == 1) {
					document.check_avail.check_in_day.selectedIndex = 29;
				} else {
					document.check_avail.check_in_day.selectedIndex = 28;
				}
			} else if ((document.check_avail.check_in_month.selectedIndex == 8 || document.check_avail.check_in_month.selectedIndex == 3 || document.check_avail.check_in_month.selectedIndex == 10) && document.check_avail.check_in_day.selectedIndex == 30) {
				var inNum = 1;
			} else if ((document.check_avail.check_in_month.selectedIndex == 8 || document.check_avail.check_in_month.selectedIndex == 3 || document.check_avail.check_in_month.selectedIndex == 10) && document.check_avail.check_in_day.selectedIndex > 30) {
				var inNum = 1;
				document.check_avail.check_in_day.selectedIndex = 30;
			} else {
				if (document.check_avail.check_in_day.selectedIndex == 31) {
					if (document.check_avail.check_in_month.selectedIndex == document.check_avail.check_out_month.selectedIndex) {
						if (document.check_avail.check_in_month.selectedIndex == 11) {
							document.check_avail.check_out_month.selectedIndex = 0;
						} else {
							document.check_avail.check_out_month.selectedIndex = parseInt(document.check_avail.check_out_month.selectedIndex) + 1;
						}
					}
					document.check_avail.check_out_day.selectedIndex = 1;
				} else {
					var inNum = parseInt(document.check_avail.check_in_day.selectedIndex) + 1;
				}
			}
			if (document.check_avail.check_in_month.selectedIndex == document.check_avail.check_out_month.selectedIndex) {
				document.check_avail.check_out_day.selectedIndex = inNum;
			}
		}
	} else if (mode == "out") {
		if (document.check_avail.check_out_month.selectedIndex == 1 && document.check_avail.check_out_day.selectedIndex > 28) {
			if (document.check_avail.check_out_year.selectedIndex == 1) {
				document.check_avail.check_out_day.selectedIndex = 29;
			} else {
				document.check_avail.check_out_day.selectedIndex = 28;
			}		
		}
		if ((document.check_avail.check_out_month.selectedIndex == 8 || document.check_avail.check_out_month.selectedIndex == 3 || document.check_avail.check_out_month.selectedIndex == 10) && document.check_avail.check_out_day.selectedIndex > 30) {
			document.check_avail.check_out_day.selectedIndex = 30;} 
		if (document.check_avail.check_out_day.selectedIndex <= document.check_avail.check_in_day.selectedIndex && document.check_avail.check_in_month.selectedIndex == document.check_avail.check_out_month.selectedIndex) {			
			var outNum = parseInt(document.check_avail.check_out_day.selectedIndex) - 1;
			document.check_avail.check_in_day.selectedIndex = outNum;
		}
	}
}

function changeYear(mode) {
	var newValue;
	
	if (mode == "in") {
		if (document.check_avail.check_in_year.selectedIndex > document.check_avail.check_out_year.selectedIndex) {
			document.check_avail.check_out_year.selectedIndex = document.check_avail.check_in_year.selectedIndex;
		}
	} else if (mode == "out") {
		if (document.check_avail.check_in_year.selectedIndex > document.check_avail.check_out_year.selectedIndex) {
			document.check_avail.check_in_year.selectedIndex = document.check_avail.check_out_year.selectedIndex;
		}
	}
}

function check_dates() {
	var today     = new Date();
	var thisMonth = today.getMonth() + 1;
	thisMonth = numberZeroFormat(thisMonth);
	var thisDay   = today.getDate();
	thisDay = numberZeroFormat(thisDay);
	var thisYear  = today.getFullYear();
	var dateToday = thisYear + "" + thisMonth + "" + thisDay;
	var dateStart = document.check_avail.check_in_year.value +  document.check_avail.check_in_month.value + document.check_avail.check_in_day.value;
	var dateStop  = document.check_avail.check_out_year.value +  document.check_avail.check_out_month.value + document.check_avail.check_out_day.value;

	if (parseInt(dateStop) < parseInt(dateStart)) {
		alert("Your check out date cannot be before your check in date");
		return false;
	} else if (parseInt(dateStart) == parseInt(dateStop)) { 
		alert("Your check in date cannot be the same as your check out date");
		return false;
	} else if (parseInt(dateStart) < parseInt(dateToday) || parseInt(dateStop) < parseInt(dateToday)) {
		alert("You cannot check availibility for dates in the past");
		return false;
	} else { 
		return true;
	}
}

function validateForm(formName) {
	var thisForm = formName.elements; 
	for(var i=0; i<thisForm.length; i++) {
	   with(thisForm[i]) { 
	     var validateMode  = thisForm[i].VALIDATE;
		 
	     if(!validateMode) continue; 
		 var thisValue = trim(value);

		 if (validateMode == "email") {
		 	if (!emailCheck(thisValue)) {
				thisForm[i].select();
			 	thisForm[i].focus(); 
				return false;
			}
		 }
		 else if (validateMode == "cc") {
		 	if (!validCCForm(thisValue) || thisValue == "" || thisValue == NULL) {
				thisForm[i].select();
			 	thisForm[i].focus(); 
				return false;
			}
		 }
		 else if (validateMode == "select") {
		 	if (thisForm[i].selectedIndex == 0 || thisValue == "") {
				alert("Please select " +  thisForm[i].VALIDATENAME + " before submitting this form."); 
				thisForm[i].focus();
				return false;
			}
		 }
		 else if (validateMode == "requiredDefault") {
		 	if (thisValue == "") {
			 alert("Please enter " +  thisForm[i].VALIDATENAME + " before submitting this form.");                  
			 thisForm[i].select();
			 thisForm[i].focus();
			 return false;
			}
		 }
		 else if (validateMode == "notZero") {
		 	if (thisValue == "" || thisValue == 0 || thisValue == 0.00) {
			 alert("One of your GDS rates is set to either zero or blank.");                  
			 return false;
			}
		 }
		 else if (validateMode == "notZeroActive") {
		 	var thisElement = thisForm[i].name;	
			var iLen        = thisElement.length;			
			var first       = thisElement.indexOf("_");
			var code        = thisElement.substring(0, first);	
			var last        = thisElement.lastIndexOf("_") + 1;	
			var num         = thisElement.substring(last, iLen);	
			var validChars  = "0123456789";		
			var newNum      = "";
			
			for (forLoop = 0; forLoop < num.length; forLoop++) { 
				thisChar = num.charAt(forLoop); 
				if (validChars.indexOf(thisChar) != -1) {
					newNum = newNum + thisChar + "";
				}
			}
			
			var thisActive = "active" + code + newNum;
			var obj = MM_findObj(thisActive);
			if (obj == null) {
				var thisActive = code + "_active_" + newNum;
				var obj = MM_findObj(thisActive);			
			}
		 	if (obj != null && obj.checked) {
			 	if (thisValue == "" || thisValue == 0 || thisValue == 0.00) {
				 alert("One of your GDS rates is set to either zero or blank.");                  
				 return false;
				}
			}
		 }
		 else if (validateMode == "wholesalerActiveDates") {
			var thisElement = thisForm[i].name;	
			var iLen        = thisElement.length;
			var findActive  = thisElement.indexOf("_");
			var findActive  = findActive + 1;
			var newString   = thisElement.substring(findActive, iLen);		
			var first       = newString.indexOf("_");
			var thisId      = newString.substring(0, first);	
			var last        = newString.lastIndexOf("_") + 1;	
			var thisNum     = newString.substring(last, iLen);		
			var obj         = MM_findObj(thisElement);

			var dateToday = todaysDate();
				
			if (obj.checked) {
				var newStartDay   = "date_start_" + thisId + "_" + thisNum + "_date";
				var newStartMonth = "date_start_" + thisId + "_" + thisNum + "_month";
				var newStartYear  = "date_start_" + thisId + "_" + thisNum + "_year";
				var newStopDay    = "date_stop_" + thisId + "_" + thisNum + "_date";
				var newStopMonth  = "date_stop_" + thisId + "_" + thisNum + "_month";
				var newStopYear   = "date_stop_" + thisId + "_" + thisNum + "_year";
				var startDayObj   = MM_findObj(newStartDay);
				var startMonthObj = MM_findObj(newStartMonth);
				var startYearObj  = MM_findObj(newStartYear);
				var stopDayObj    = MM_findObj(newStopDay);
				var stopMonthObj  = MM_findObj(newStopMonth);
				var stopYearObj   = MM_findObj(newStopYear);
				var dateStart     = startYearObj.value + "" + numberZeroFormat(startMonthObj.value) + "" + numberZeroFormat(startDayObj.value);
				var dateStop      = stopYearObj.value + "" + numberZeroFormat(stopMonthObj.value) + "" + numberZeroFormat(stopDayObj.value);
				
				if (parseInt(dateStop) < parseInt(dateStart) || parseInt(dateStop) < parseInt(dateToday)) {
					alert("A stop date cannot be prior to today or prior to its respective start date.");
					stopMonthObj.focus();					
					return false;
				}
					
				// check start dates
				if (startMonthObj.value == 2) {
					if (startYearObj.value == 2008) {
						if (startDayObj.value > 29) {
							alert("There are date errors in your form.");
							startDayObj.focus();
							return false;
						}
					} else {
						if (startDayObj.value > 28) {
							alert("There are date errors in your form.");
							startDayObj.focus();
							return false;
						}
					}
				} else if (startMonthObj.value == 4 || startMonthObj.value == 6 || startMonthObj.value == 9 || startMonthObj.value == 11) {
					if (startDayObj.value > 30) {
						alert("There are date errors in your form.");
						startDayObj.focus();
						return false;
					}
				}
				
				// check stop dates
				if (stopMonthObj.value == 2) {
					if (stopYearObj.value == 2008) {
						if (stopDayObj.value > 29) {
							alert("There are date errors in your form.");
							stopDayObj.focus();
							return false;
						}
					} else {
						if (stopDayObj.value > 28) {
							alert("There are date errors in your form.");
							stopDayObj.focus();
							return false;
						}
					}
				} else if (stopMonthObj.value == 4 || stopMonthObj.value == 6 || stopMonthObj.value == 9 || stopMonthObj.value == 11) {
					if (stopDayObj.value > 30) {
						alert("There are date errors in your form.");
						stopDayObj.focus();
						return false;
					}
				}				
			}
		 }
		 else if (thisValue == "" || thisValue == thisForm[i].defaultValue){
			 alert("Please enter " +  thisForm[i].VALIDATENAME + " before submitting this form.");                  
			 thisForm[i].select();
			 thisForm[i].focus(); 
			 return false;
	      }
	   }
	}
	return true;
}

function validatePassReset() {
	if ((document.passForm.new_pass1.value == document.passForm.new_pass2.value) &&
	document.passForm.new_pass1.value != null && document.passForm.new_pass1.value != "" &&
	document.passForm.new_pass2.value != null && document.passForm.new_pass2.value != "") {
		return true;
	} else {
		alert("Your passwords do not match each other.");
		document.passForm.new_pass1.focus();
		document.passForm.new_pass1.select();
		return false;
	}
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

function checkoutValidate() {
	if (!validCCForm(document.resForm.credit_number)) {
		document.resForm.credit_number.focus();
		return false;
	} else if (document.resForm.credit_expiration.value == "") {
		alert("Please enter your credit card expiration date.");
		document.resForm.credit_expiration.select();
		document.resForm.credit_expiration.focus();
		return false;
	} else if (!document.resForm.stayed_before[0].checked && !document.resForm.stayed_before[1].checked) {
		alert("Please indicate if you have stayed with us before.");
		return false;
	} else if (!validateForm(resForm)) {
		return false;
	} else {
		return true;
	}
}

function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
} // end of the 'setSelectOptions()' function

// number format as 01
function numberZeroFormat(num) {
	var newNum = "" + num;
	
	if(newNum.length < 2) {
		newNum = "0" + newNum;
		return newNum;
	} else {	
		return newNum;
	}
}

// today's date as YYYYMMDD
function todaysDate() {
	var today      = new Date();
	var todayMonth = today.getMonth() + 1;
	var todayMonth = numberZeroFormat(todayMonth);		
	var todayDay   = today.getDate();
	var todayDay   = numberZeroFormat(todayDay);
	var todayYear  = today.getFullYear();	
	var dateToday  = todayYear + "" + todayMonth + "" + todayDay;
	
	return dateToday;
}

function wholesalerValidate() {
	if (!document.resForm.stayed_before[0].checked && !document.resForm.stayed_before[1].checked) {
		alert("Please indicate if you have stayed with us before.");
		return false;
	} else if (!validateForm(resForm)) {
		return false;
	} else {
		return true;
	}
}

function eventDateCheck() {
	var dateStart = document.eventForm.start_year.value + document.eventForm.start_month.value + document.eventForm.start_date.value;
	var dateStop  = document.eventForm.end_year.value + document.eventForm.end_month.value + document.eventForm.end_date.value;

	if (parseInt(dateStop) < parseInt(dateStart)) {
		alert("Your event stop date cannot be before your event start date");
		return false;
	} else if (!validateForm(eventForm)) {
		return false;
	} else { 
		return true;
	}
}
function javascriptWrite(item) {
	document.write(item);
}
