var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;

if (document.getElementById) {
	isID = 1;
	isDHTML = 1;
} else {
	if (document.all) {
		isAll = 1;
		isDHTML = 1;
	} else {
		browserVersion = parseInt(navigator.appVersion);
		if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {
			isLayers = 1;
			isDHTML = 1;
		}
	}
}

function findDOM(objectID1,objectID2,withStyle) {

	if(objectID1 == '') {
		objectID1 = false;
	}
	if (withStyle == 1) {
		if ( objectID2 != null || objectID2 != '' ) {
			if (isID) {
				if ( document.getElementById(objectID2) ) {
					return (document.getElementById(objectID2).style) ;
				}
			} else {
				if (isAll) {
					return (document.all[objectID2].style);
				} else {
					if (isLayers) {
						if (objectID1) {
							return (document.layers[objectID1].layers[objectID2]);
						} else {
							return (document.layers[objectID2]);
						}
					}
				}
			}
		}
	} else {
		if (isID) {
			return (document.getElementById(objectID2)) ;
		} else {
			if (isAll) {
				return (document.all[objectID2]);
			} else {
				if (isLayers) {
					if (objectID1) {
						return (document.layers[objectID1].layers[objectID2]);
					} else {
						return (document.layers[objectID2]);
					}
				}
			}
		}
	}
}

function hideSpan(objectID) { 
	if (isAll || isID) {
		domStyle = findDOM('', objectID, 1);
		if ( domStyle ) {
			domStyle.display='none';
		}
	}
	return;
}

function showSpan(objectID) {
	if (isAll || isID) {
		domStyle = findDOM('', objectID, 1);
		if ( domStyle ) {
			domStyle.display = 'inline'; // was block
		}
	}
	return;
}

function disableForm(elementName) {
	objElement = document.vacationPlanning[elementName];
	if ( objElement ) {
		objElement.disabled = true;
		while ( objElement.parentNode && 
			objElement.nodeName.toLowerCase() != 'dl' ) {
			objElement = objElement.parentNode;
				
		}
		if ( objElement.nodeName.toLowerCase() == 'dl' ) {
			objElement.style.display = 'none';
				
		}
	}
}

function enableForm(elementName) {
	objElement = document.vacationPlanning[elementName];
	if ( objElement ) {
		objElement.disabled = false;
		while ( objElement.parentNode && 
			objElement.nodeName.toLowerCase() != 'dl' ) {
			objElement = objElement.parentNode;
		}
		if ( objElement.nodeName.toLowerCase() == 'dl' ) {
			try {
				objElement.style.display = 'block';
			} catch (e) {
				// ie doesn't support 'table-row'
				objElement.style.display = 'block';
			}
		}
	}
}

function show(elementId) {
  var el = document.getElementById(elementId);
  if (el) {
    el.style.display = "block";
  }
}

function hide(elementId) {
  var el = document.getElementById(elementId);
  if (el) {
    el.style.display = "none";
  }
}

function swapAddressFields() {
  var form = document.vacationPlanning;
  var $ = document.getElementById;
  
//disable the country field
  var $D = YAHOO.util.Dom;
  var $ = YAHOO.util.Dom.get;
  var countryDisabled = $("countryDisabled");
  var country = $("country");
  if (countryDisabled) {
    country.disabled = true;
    $D.removeClass(country, "required");
    country.name = "country-disabled";
    countryDisabled.name = "country";
  }
  
  if (country.value == 'US') {
    hide("canada-postal-address-container");
    show("us-postal-address-container");
    enableForm("state");
    enableForm("zipcode");
    
    disableForm("canadaProvince");
    disableForm("canadaPostalCode");
    
    // communication language
    showSpan("languageprefEnglish");
    showSpan("languageprefSpanish");
    hideSpan("languageprefFrench"); 
    
    // dvd language
    showSpan("dvdEnglishOrSpanish");
    hideSpan("dvdFrenchOrEnglish");
    
    if (form.state.value == 'PR') {
      enableForm("urbanization");
      disableForm("address2");
      show("puerto-rico-address-container");
    } else {
      hide("puerto-rico-address-container");
      disableForm("urbanization");
      enableForm("address2");
    }
  } else {
    disableForm("urbanization");
    if (country.value == 'CA') {
      show("canada-postal-address-container");
      hide("us-postal-address-container");
      enableForm("canadaProvince");
      enableForm("canadaPostalCode");
      
   // communication language
      hideSpan("languageprefEnglish");
      showSpan("languageprefFrench");
      hideSpan("languageprefSpanish");
      
      disableForm("state");
      disableForm("zipcode");
      disableForm("province");  //other non-canadian provinces (for future use)
      disableForm("postalCode");  //other non-canadian postalcodes (for future use)
      
   // dvd language     
      showSpan("dvdFrenchOrEnglish");
      hideSpan("dvdEnglishOrSpanish");
    } else {
      enableForm("state");
      enableForm("zipcode");
      
      disableForm("canadaProvince");
      disableForm("canadaPostalCode");
    }
  }  
}


function swapAddressFieldsOld() {
	if ( !document.vacationPlanning ) {
			return;
	}

	if ( document.vacationPlanning.country.value == 'US') {
		enableForm("state");
		enableForm("zipcode");

		disableForm("canadaProvince");
		disableForm("canadaPostalCode");
		disableForm("province");
		disableForm("postalCode");
		
		// communication language
		showSpan("languageprefEnglish");
		showSpan("languageprefSpanish");
		hideSpan("languageprefFrench");	
		
		// dvd language
		showSpan("dvdEnglishOrSpanish");
		hideSpan("dvdFrenchOrEnglish");
		
			// show urbanization if puerto rico
			if ( document.vacationPlanning.state.value == 'PR') {
				enableForm("urbanization");
				disableForm("address2");
			} else {
				disableForm("urbanization");
				enableForm("address2");
			}
		
	} else {
		// no urbanizatino for canada choice
		disableForm("urbanization");	
			
			if ( document.vacationPlanning.country.value == 'CA' ) {
				enableForm("canadaProvince");
				enableForm("canadaPostalCode");
	
				// communication language
				hideSpan("languageprefEnglish");
				showSpan("languageprefFrench");
				hideSpan("languageprefSpanish");
				
				disableForm("state");
				disableForm("zipcode");
				disableForm("province");	//other non-canadian provinces (for future use)
				disableForm("postalCode"); 	//other non-canadian postalcodes (for future use)
	
				// dvd language			
				showSpan("dvdFrenchOrEnglish");
				hideSpan("dvdEnglishOrSpanish");
			} else {
				enableForm("state");
				enableForm("zipcode");				
				// activate for non-canadian provinces (for future use)
				//enableForm("province");
				//enableForm("postalCode");
				
				disableForm("canadaProvince");
				disableForm("canadaPostalCode");
			}
		
	}
}



function ContinueToReg() {	
	var ContinueToRegHiddenForm = document.forms['vpkRegistrationPrePop'];
	ContinueToRegHiddenForm.submit();
}




function CloseOpenWindow() {
	if ( !document.getElementsByTagName) return false;
	var linksCO = document.getElementsByTagName("a");

	for ( var i=0; i < linksCO.length; i++ ) {
		if ( linksCO[i].className.match("closeOpenWin")) {
			linksCO[i].onclick = function() {
				window.close(this);
				window.open(this.href);
			}
		}
	}
}

function CloseWindow() {

	if ( !document.getElementsByTagName) return false;
	var linksC = document.getElementsByTagName("a");

	for ( var i=0; i < linksC.length; i++ ) {
		if ( linksC[i].className.match("closeWin")) {
			linksC[i].onclick = function() {
				window.close(this);
				return false;
			}
		}
	}
}
WDPRO_LOADER.require("event");
WDPRO_LOADER.require("dom");
//WDPRO_LOADER.require("formUtils");

WDPRO_LOADER.addCallback(function() {
  $D = YAHOO.util.Dom;
  $E = YAHOO.util.Event;  
  $ = $D.get;
  
  var value = $E.addListener("postal-address-opt-in", "click", function() {
    if (this.checked == true) {
      $D.replaceClass("postal-address-opt-out", "visible", "hidden");
    } else {     
      $D.replaceClass("postal-address-opt-out", "hidden", "visible");
    }
  });
});

WDPRO_LOADER.addCallback(
	function() {
	        YAHOO.util.Event.onContentReady("doc", swapAddressFields);
      		
      		/* FORM VALIDATOR TO BE FIXED LATER...
		WDPRO_CONFIG.locale = "en_US";
		$D = YAHOO.util.Dom;
		$S = WDPRO.util.StringUtils;

		var frm = $D.get("vacationPlanning");

		var tmpCfg = WDPRO.util.FormValidator.createConfigByClasses(frm, {notEmpty:"required"});
		//tmpCfg.rules.push({inputs: frm.postalCode, rule: $S.isPostalCode});


		YAHOO.util.Event.addListener($D.get("continueButton"), "click", function() {
				var resp = WDPRO.util.FormValidator.validate(tmpCfg);
				var errs = resp.errors;
				alert(errs.length);
				for (var i=0; i<errs.length; i++) {
					var errEl = errs[i].getElement();
					highlight(errEl);
					var errSpan = YAHOO.util.Dom.getPreviousSiblingBy(errEl, function(el) { return YAHOO.util.Dom.hasClass(el, "validationError");});
					YAHOO.util.Dom.setStyle(errSpan, "visibility", "visible");

				}
				var scs = resp.successes;
				for (var i=0; i<scs.length; i++) {
					var scsEl = scs[i].getElement();
					$D.setStyle(scs[i].getElement(), 'border-color', '#0F0');

					var errSpan = YAHOO.util.Dom.getPreviousSiblingBy(scsEl, function(el) { return YAHOO.util.Dom.hasClass(el, "validationError");});
					YAHOO.util.Dom.setStyle(errSpan, "visibility", "hidden");
				}
				return false
			}
		);
		*/

	}
);
WDPRO_LOADER.addCallback(CloseOpenWindow);
WDPRO_LOADER.addCallback(CloseWindow);

WDPRO_LOADER.addCallback(function(){
	$D = YAHOO.util.Dom;
	$E = YAHOO.util.Event;  
	$E.on("contStep2", "click", function(){
		//CMRDVA_form1btn:contactok_y
		var link = "CMRDVA_form1btn:contactok_";
		
		var checkOptIn = $D.get("postal-address-opt-in");
		if (checkOptIn) {
			var checked = checkOptIn.checked ? "y" : "n";
			link += checked;
		}
		s_wdpro.trackClick(this, link);
	});
	$E.on("contStep3", "click", function(){
		//CMRDVA_form2btn:days_01:flexible_y:wkids_n
		var link = "CMRDVA_form2btn:days_";
		var daysCmb = $D.get("wdwlengthofstay");
		if (isNaN(parseInt(daysCmb.value))) {
			link += "un";
		} else {
			var days = parseInt(daysCmb.value);
			if (days < 10) {
				link = link + "0" + days;
			} else {
				link = link + days;
			}
		}
		var flexiDates = document.forms['vacationPlanning'].flexibledates;
		var flexSelection = "";
		if (flexiDates[0].checked == true) {
			flexSelection = "y";
		} else if(flexiDates[1].checked == true) {
			flexSelection = "n";
		} else {
			flexSelection = "u";
		}
		link += ":flexible_" + flexSelection;
		var withKids = document.forms['vacationPlanning'].travelwithchildren;
		var withKidsSelection = "";
		if (withKids[0].checked == true) {
			withKidsSelection = "y";
		} else if(withKids[1].checked == true) {
			withKidsSelection = "n";
		} else {
			withKidsSelection = "u";
		}
		link += ":wkids_" + withKidsSelection;
		
		s_wdpro.trackClick(this, link);
	});
});
