var postalCodePanel;
var postalValidationServiceUrl;

// PostalCode Panel panel subclass
PostalCodePanel = function(el, userConfig) {
	if (arguments.length > 0) {
		PostalCodePanel.superclass.constructor.call(this, el, userConfig);
	}
}

// Inherit from YAHOO.widget.Panel
YAHOO.extend(PostalCodePanel, YAHOO.widget.Panel);

// Initialize the PostalCodePanel
PostalCodePanel.prototype.init = function(el, userConfig) {
	PostalCodePanel.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(PostalCodePanel);
	
	this.beforeRenderEvent.subscribe(function() {
			if (! this.footer) {
				this.setFooter("");
			}
		},
		this, true
	);

	this.renderEvent.subscribe(function() {
		var me = this;
		var headerHeight = me.header.offsetHeight;
		me.bodyContentDiv = YAHOO.util.Dom.getElementsByClassName("yui-content", "div", me.body)[0];
		var contentDivId = YAHOO.util.Dom.generateId(me.bodyContentDiv, "postalcodepanel");
		me.bodyContentDiv.setAttribute("id", contentDivId);
	}, this, true);
	
	if (userConfig) {
		this.cfg.applyConfig(userConfig, true);
	}
	
	this.dragEvent.subscribe(function(type, args) {
		if (args[0] == "startDrag") {
			this.cfg.setProperty("zIndex", 100);
		}
	}, this, true);
	
	this.initEvent.fire(PostalCodePanel);
}

// Set up the default values for the properties
PostalCodePanel.prototype.initDefaultConfig = function() {
	PostalCodePanel.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("getpostalcodeserviceurl", {value: ""});
}

PostalCodePanel.prototype.validateZipCode = function() {
	var pcInput = YAHOO.util.Dom.get("postalcode_pc");
	var cnInput = YAHOO.util.Dom.get("postalcode_cn");
	var pc = pcInput.value;
	if (pc != "" || cnInput.value == "ZZ") {
		var params = new SOAPClientParameters();
		params.add("zipCode", pc);
		params.add("countryCode", cnInput.value);
		if (this.packageRecordId && this.packageRecordId.length > 0) {
			params.add('packageRecordId', this.packageRecordId);
		}
		SOAPClient.invoke(postalValidationServiceUrl, "isZipCodeValid", params, true, this.validateZipCode_callback, this);
	} else {
		postalCodePanel.showMessage("Please enter your ZIP/Postal Code");
	}
}
			
PostalCodePanel.prototype.validateZipCode_callback = function(result, xmlResult, postalCodePanel) {
	if(result.isValid == "true") {
		postalCodePanel.postProcess();
	} else {
		postalCodePanel.showMessage(result.message);
	}
}

PostalCodePanel.prototype.showMessage = function(message) {
	var feedbackDiv = YAHOO.util.Dom.getElementsByClassName("feedback", "div", "postalCodePanel")[0];
	feedbackDiv.innerHTML = message;
	YAHOO.util.Dom.get("postalcode_pc").focus();
}

PostalCodePanel.prototype.postProcess = function() {
	this.hide();
	if (this.search_url == undefined || this.search_url == "") {
		localPostalPostProcessing();
	} else {
		setPage(this.search_url);
	}
}

PostalCodePanel.prototype.showPostalPanel = function(search_url, packageRecordId) {
	this.search_url = search_url;
	this.packageRecordId = packageRecordId;
	this.show();
	YAHOO.util.Dom.get("postalcode_pc").focus();
}

YAHOO.util.Event.addListener(window, "load", function() {
	// create div structures
	var postalCodePanelDiv = document.createElement("DIV");
	postalCodePanelDiv.setAttribute("id", "postalCodePanel");
//	postalCodePanelDiv.setAttribute("style", "display:none");
	document.body.appendChild(postalCodePanelDiv);
	postalCodePanelDiv.innerHTML = 
		'<div class="hd"><div class="tl"></div><span class="infotitle">Zip/Postal Code</span><div class="tr"></div></div>'
		+ '<div class="bd">'
		+ '		<div class="yui-content">'
		+ '			<label>Please enter your <B>5 digit zip or international postal code</b> to verify qualification for this offer</label><br><br>'
		+ '			<input style="width:80px" class="inputtext1" id="postalcode_pc" name="pc" type="text" value="" maxlength="10" onKeyDown="if(event.keyCode==13) {postalCodePanel.validateZipCode();}"> in '
		+ '			<label><select class="inputdrop1" id="postalcode_cn" name="cn"><option selected value="US">United States </option><option value="CA">Canada </option><option value="ZZ">Other </option></select></label>'
		+ '			&nbsp;&nbsp;' + (typeof postalCodePanelGoButton == 'undefined' ? '<a href="javascript:postalCodePanel.validateZipCode();"><img alt="vacation deals" border="0" src="images/web/go.gif" stype="cursor:pointer"></a>' : postalCodePanelGoButton)
		+ '			<div align="left" class="feedback"></div>'
		+ '     </div>'
		+ '</div>'
		+ '<div class="ft"><div class="bl"></div><div class="br"></div></div>';
	
	postalCodePanel = new PostalCodePanel("postalCodePanel", { title: "Postal Code", getpostalcodeserviceurl: postalValidationServiceUrl,
		fixedcenter:true,  visible:false, width:'230px', constraintoviewport:false, underlay: 'none'});
	postalCodePanel.render();
	postalCodePanel.center();
	
});

