/*
	- Majority of jquery code provided by HON for Hook Phase 2 form submission from "Search" page to HON servers for their own fulfillment process.
	- Extracting locale and logged in status generated by tea to pass on to HON who passes it on to another vendor.
	- Setting s_wdpro.tl omniture tracking value for site analytics team.
	- Added call back function to set src attribute for image buttons.
*/

WDPRO_LOADER.require("dom");

// Validates the form content before submitting to HON

var lang = document.getElementById("lang");
WDPRO_LOADER.require("dom");
WDPRO_LOADER.require("formUtils");
WDPRO_LOADER.require("connection");

// target specific pages since the actual search page and the landing page share this..
if( $('body').attr('id') == 'Give-A-Day-Get-A-Disney-Day' || $('body').attr('id') == 'Give-A-Day-Get-A-Disney-Day-Travel-Trade'  ) {
	WDPRO_LOADER.addCallback(
		function() {
		
		//Detect language	
			lang.value = lang.value.toLowerCase();
			index = lang.value.indexOf("_");
			lang.value = lang.value.slice(0, index);
			WDPRO_CONFIG.locale = lang.value;
			$D = YAHOO.util.Dom;
			$S = WDPRO.util.StringUtils;
	
			var frm = $D.get("sample-search-form");
			
			var highlight = function(el) {
				$D.setStyle(el, 'border-color', '#F00');
			}
	
			var clearHighlight = function(el) {
				$D.setStyle(el, 'border-color', '#000');
			}
	
			var tmpCfg = WDPRO.util.FormValidator.createConfigByClasses(frm, {postalCode:"postalCode",integer:"ints",notEmpty:"required",currency:"currency",date:"date"});
			
			tmpCfg.rules.push({inputs: frm.postalCode, rule: function(elementValue) {
					  var isFormatted = false;
					  elementValue = elementValue.split(' ').join('');
											 
						//var regExp = new RegExp(/^[A-Za-z0-9\s]{5,6}$/);
						//if(regExp.test(elementValue))
						//	isFormatted = true;
						if(elementValue.length > 0) {
							isFormatted = true;
							document.forms['sample-search-form'].onsubmit = function(){
								return true;
							}
						}
						 return isFormatted;						
						}
					}
				);
			
			YAHOO.util.Event.addListener($D.get("Submit"), "click", function() {
					var resp = WDPRO.util.FormValidator.validate(tmpCfg);
					var errs = resp.errors;
					
						for (var i=0; i<errs.length; i++) {
							var errEl = errs[i].getElement();	
							var errSpan = YAHOO.util.Dom.getPreviousSiblingBy(errEl, function(el) { return YAHOO.util.Dom.hasClass(el, "validationError");});
							
							if(errEl.id == "postalCode") {
								$D.setStyle("VolunteerProjectInterestLabel", "marginTop", "-239px");
								
								if(navigator.appName == "Microsoft Internet Explorer") {	
									$D.setStyle("VolunteerProjectInterestLabel", "marginTop", "-254px");
								}							
								$D.setStyle("zipCodeMessage", "marginTop", "145px");
							}						
							$D.setStyle(errSpan, "display", "block");								
						}
						
					
						var scs = resp.successes;
						for (var i=0; i<scs.length; i++) {
							var scsEl = scs[i].getElement();
												
							if(scsEl.id == "postalCode") {							
								$D.setStyle("VolunteerProjectInterestLabel", "marginTop", "-224px");
								$D.setStyle("zipCodeMessage", "marginTop", "130px");
								if(navigator.appName == "Microsoft Internet Explorer"){
									$D.setStyle("VolunteerProjectInterestLabel", "marginTop", "-240px");
								}								
							}							
							var errSpan = YAHOO.util.Dom.getPreviousSiblingBy(scsEl, function(el) { return YAHOO.util.Dom.hasClass(el, "validationError");});
							$D.setStyle(errSpan, "display", "none");
						}
						
					if(errs.length == 0){
						document.forms['sample-search-form'].onsubmit = function(){
							return true;
						}
					}		
					
				}
			);
		}
	);
}


	
// define variables
var $uriPath = '';
var honLinkId = '';

// get locale via uri
$uriPath = window.location.pathname;
$arrURIPath = $uriPath.split('/');
$uriLocale = $arrURIPath[2];

$(document).ready( function() {
	var $D = WDPRO.util.Dom;
				
	// Uncomment to see SQL queries, timing information, etc.
	//hon_ds.searchParameters.debug = 1;

	// disabling button before dom is returned
	if( $('#Submit').attr("disabled", true) ) {
		$('#Submit').removeAttr("disabled");
	}
	
	// add focus to city/state zip textbox
	$('#postalCode').focus();
		
	hon_ds.setSearchParametersCallback = function() {		
		
		// do not execute if on gadgad landing page
		//if( $('body').attr('id') != 'Give-A-Day-Get-A-Disney-Day2' ) {
		if( $('#sample-search-form select[name=what]').val() != '' &&
				$('#sample-search-form input[name=where]').val() != '') {
			if( $('body').attr('id') != 'Give-A-Day-Get-A-Disney-Day' || $('body').attr('id') != 'Give-A-Day-Get-A-Disney-Day2' ) {
				hon_ds.searchParameters.cityState = $('#sample-search-form input[name=where]').val();
				hon_ds.searchParameters.impactArea = $('#sample-search-form select[name=what]').val();
			}
		} else {
				hon_ds.searchParameters.cityState = $('#sample-search-form input[name=whereHidden]').val();
				hon_ds.searchParameters.impactArea = $('#sample-search-form input[name=whatHidden]').val();
				
				$('#sample-search-form input[name=where]').val(hon_ds.searchParameters.cityState);
				$('#sample-search-form select[name=what]').val(hon_ds.searchParameters.impactArea);				
		}
			
		//}


	
		// hon_ds.searchParameters.cityState = '12345';	// for testing only (to remove)
		
			
	  	hon_ds.searchParameters.familyOnly = $('#sample-search-form input[name=family-only]:checked').val() == '1';
	  	hon_ds.searchParameters.locale = $uriLocale;

		// set logged in status and locale
		$bLoggedIn = $("input[id$=honLoggedIn]").val()
	  	hon_ds.searchParameters.passthroughData = '{ "loggedIn":' + $bLoggedIn + ',"locale":"' + $uriLocale + '" }';
	  	
	  	// analytics: set linkId
		//if( hon_ds.searchParameters.cityState != '') {
			//honLinkId = 'searchsubmit_link_' + hon_ds.searchParameters.impactArea + '_' +  hon_ds.searchParameters.cityState;
			// set prop9 to c9
			//s_wdpro.prop9 = honLinkId;
			// Set the custom link value for tracking: e = exit link; d = file download; o = generic custom link
			//s_wdpro.tl($("#Submit"), 'o', honLinkId );
			
		//}
	};	

	
	// set src attribute for image buttons in search results
	hon_ds.endLoadCustomCallback = function(data, selector) {
		var buttonsSubmit = $D.getElementsByClassName("submitButton","input",$(selector).get(0));
		var buttonsExpress = $D.getElementsByClassName("expressButton","input",$(selector).get(0));
		var buttonsDetails = $D.getElementsByClassName("detailsButton","input",$(selector).get(0));
		var buttonsRegister = $D.getElementsByClassName("registerButton","input",$(selector).get(0));
	
		for (var i=0;i < buttonsSubmit.length; i++) {
			buttonsSubmit[i].src = SUBMIT_BUTTON_URL;
		}		
		for (var i=0;i < buttonsExpress.length; i++) {
			buttonsExpress[i].src = EXPRESS_BUTTON_URL;
		}
		for (var i=0;i < buttonsDetails.length; i++) {
			buttonsDetails[i].src = DETAILS_BUTTON_URL;
		}
		for (var i=0;i < buttonsRegister.length; i++) {
			buttonsRegister[i].src = REGISTER_BUTTON_URL;
		}
	}
	
	if( $('body').attr('id') != 'Give-A-Day-Get-A-Disney-Day' || $('body').attr('id') != 'Give-A-Day-Get-A-Disney-Day2') {
		if( $('#sample-search-form input[name=whereHidden]').val() != '' &&
				$('#sample-search-form input[name=whatHidden]').val() != '') {
			hon_ds.search();
		}
	}
	
	// clear element values when clicking "start a new search" link at bottom of search module
	hon_ds.resetSearchCallback = function() {
	  $('#sample-search-form select option[value=]').attr('selected',true);
	  $('#sample-search-form input[name=where]').val('');
	  $('#sample-search-form input[name=family-only]').attr('checked',false);
	  
	};

	// open new window for external urls
	/*
		hon_ds.externalURLClickedCallback = function( url, new_window ) {
	  //alert( "Would open site '"+url+"' (new_window="+new_window+")" );		 
	  window.open(url);
	  return false;
	}
	*/
	
	hon_ds.loadURL = function( url ) {
		//get current url and get locale from it
		var referringProtocol = window.location.protocol;
		var referringHost = window.location.host;
		var referringPathName = window.location.pathname;
		var urlInterstitialComponent = referringPathName.split( "/" );
		var urlInterstitialLocale = urlInterstitialComponent[2];		
		var urlInterstitial = referringProtocol + '//' + referringHost + '/disneyparks/'+ urlInterstitialLocale + '/WhatWillYouCelebrate/index?name=Give-A-Day-Get-A-Disney-Day-Search-Interstitial';
		var redirectURL = '';
		
		if( url != '' || url != null ) {
			redirectURL = escape(url);
		}
		
		newInterstitial = window.open ( urlInterstitial + '&redirectURL=' + redirectURL, "honSearch", "scrollbars=1, menubar=1, location=yes, status=1, toolbar=1, resizable=1, width=1024, height=768" );
		newInterstitial.focus();
		return false;
	}

	// show the submit button if there are no js issues
	$('#sample-search-form input[type="submit"]').show();
} ); 

function submit_form() {
	hon_ds.search();						
}