/*jslint browser: true, eqeqeq: true, undef: true, evil: true */
/*global UnSecure_Base_Href, Base_Href */
/*global jQuery, validateSiteSearchForm, validateAdvancedSearchForm */
/******************************************************************************
Lines above are for jslint, the JavaScript verifier.  http://www.jslint.com/
******************************************************************************/

/* depends on /scripts/gea_global.js */

var addthis_config = { 
	username: 'irenenewsom', 
	data_track_clickback: true 
};

(function () {

	var mastheadHTML;
	mastheadHTML = '<table cellpadding="0" cellspacing="0" border="0" id="masthead">';
	mastheadHTML += '<tr>';
	mastheadHTML += '<td class="colA"><a href="' + Base_Href + '/" target="_top"><img src="' + Base_Href + '/images/common/logo_geappliances.gif" width="146" height="44" alt="GE Electrodom\351sticos" title="GE Electrodom\351sticos" /></a></td>';
	mastheadHTML += '<td class="colB">';

	var form_type = "product";
	if (/(^|\/)search\/results\/support_literature_product\.htm\b/.test(location.pathname)) {
		form_type = "support-product-literature";
	}
	else if (/(^|\/)search\/results\/support_product_literature\.htm\b/.test(location.pathname)) {
		form_type = "support-literature-product";
	}

	mastheadHTML += '<p style="margin-bottom: 0.5em;">';
	mastheadHTML += '<a href="javascript:void(0);" onclick="oo_feedback.show()"><img src="' + Base_Href + '/images/sm_AB5F07_oo.gif" border="0" title="comentarios"> comentarios</a>';
	mastheadHTML += ' &nbsp;&nbsp;&nbsp;&nbsp; ';
	if (form_type === "product") {
		mastheadHTML += '<a href="' + Base_Href + 'http://espanol.geappliances.com/search/advanced.htm">B\372squeda avanzada</a>';
	}
	else if (form_type === "support-product-literature" ||
		 form_type === "support-literature-product") {
		mastheadHTML += '<a href="' + Base_Href + 'http://espanol.geappliances.com/search/advanced_support.htm">B\372squeda avanzada</a>';
	}
	mastheadHTML += ' &nbsp;|&nbsp; ';
	mastheadHTML += '<a href="' + Base_Href + '/search/google/user_help.html" target="_blank" rel="search_tips_popup">Consejos de b\372squeda</a>';
	mastheadHTML += '</p>';

	if (form_type === "product") {
		mastheadHTML += ('<form action="' + Base_Href + 'search/results/product.htm" ' + 
				 '      class="mastheadSearchForm" id="geaheader_searchform" method="get" target="_top">' +
				 '<input type="text" name="q" maxlength="150" value=""/>&nbsp;' +
				 '<input class="button" type="submit" value="Buscar" title="Product Search" alt="Product Search"/>' +
				 '</form>');
	}
	else if (form_type === "support-product-literature") {
		mastheadHTML += ('<form action="' + Base_Href + 'http://espanol.geappliances.com/search/results/support_literature_product.htm" ' + 
				 '      class="mastheadSearchForm" id="geaheader_searchform" method="get" target="_top">' +
				 '<input type="text" name="q" maxlength="150" value=""/>&nbsp;' +
				 '<input class="button" type="submit" value="Buscar" title="Service &amp; Support Search" alt="Service &amp; Support Search"/>' +
				 '</form>');
	}
	else if (form_type === "support-literature-product") {
		mastheadHTML += ('<form action="' + Base_Href + 'http://espanol.geappliances.com/search/results/support_product_literature.htm" ' + 
				 '      class="mastheadSearchForm" id="geaheader_searchform" method="get" target="_top">' +
				 '<input type="text" name="q" maxlength="150" value=""/>&nbsp;' +
				 '<input class="button" type="submit" value="Buscar" title="Service &amp; Support Search" alt="Service &amp; Support Search"/>' +
				 '</form>');
	}

	mastheadHTML += '<div class="mastheadBottomRight" style="margin-top: 1em;">';
	mastheadHTML += '  <a href="' + Base_Href + '/buy/" target="_top">d\363nde comprar</a>';
	mastheadHTML += '  &nbsp;|&nbsp; ';
	mastheadHTML += '  <a mporgnav href="http://www.geappliances.com/" onclick="return switchLanguage(\'es\'); function switchLanguage(lang) { MP.SrcUrl=unescape(\'http%3A%2F%2Fwww.geappliances.com%2Fincludes%2Fgea_standard_masthead.js\');MP.UrlLang=\'es\';MP.init(); MP.switchLanguage(MP.UrlLang==lang?\'en\':lang); return false;}">in English</a>';
	mastheadHTML += '  &nbsp;|&nbsp; share ';
	mastheadHTML += '  <span class="addthis_toolbox addthis_default_style">';
	mastheadHTML += '    <a class="addthis_button_facebook"></a> ';
	mastheadHTML += '    <a class="addthis_button_twitter"></a> ';
	mastheadHTML += '    <a class="addthis_button_print"></a> ';
	mastheadHTML += '    <a class="addthis_button_email"></a> ';
	mastheadHTML += '    <a class="addthis_button_compact"></a>';
	mastheadHTML += '  </span>';
	mastheadHTML += '</div>';

	mastheadHTML += '</td>';	// ends <td class="colB">
	mastheadHTML += '</tr>';
	mastheadHTML += '</table>';	// ends <table id="masthead">
	mastheadHTML += '<script type="text/javascript" src="' + Base_Href + '/scripts/addthis.js"></script>';

	document.write(mastheadHTML);

})();

if (!String.prototype.negateEveryWord) {
	String.prototype.negateEveryWord = function () {
		var s = this;
		s = s.replace(/(^|\s)(?=\S)/g, "$1-");
		return s;
	};
}

if (!String.prototype.trim) {
        String.prototype.trim = function () {
                return this.replace(/^\s+|\s+$/g, "");
        };
}

function populateMastheadSearchForm () {
	if (typeof(jQuery) !== "undefined") {
		jQuery(document).ready(function ($) {
			var query;
			if (/(?:^|\?|\&)q=([^\?\&\=]+)/.test(location.search) ||
			    /(?:^|\?|\&)as_q=([^\?\&\=]+)/.test(location.search) ||
			    /(?:^|\?|\&)as_epq=([^\?\&\=]+)/.test(location.search) ||
			    /(?:^|\?|\&)as_oq=([^\?\&\=]+)/.test(location.search)) {
				// extract the first nonblank of the following:
				// - q      = regular search
				// - as_q   = advanced search: all these words
				// - as_epq =                  exact wording or phrase
				// - as_oq  =                  one or more of these words
				query = decodeURIComponent(RegExp.$1.replace(/\+/g, " ")).trim();
				if (/(?:^|\?|\&)as_eq=([^\?\&\=]+)/.test(location.search)) {
					// as_eq = unwanted words
					query = query + " " + 
						(decodeURIComponent(RegExp.$1.replace(/\+/g, " ")).
						 trim().
						 negateEveryWord());
				}
				$("form.mastheadSearchForm").find("input[name='q']").val(query);
			}
		});
	}
}

function addSearchFormValidation () {
	if (typeof(jQuery) !== "undefined") {
		jQuery(document).ready(function ($) {
			$("form.mastheadSearchForm").each(function () {
				var form = this;
				if (validateSiteSearchForm) {
					form.onsubmit = function () {
						return validateSiteSearchForm(this);
					};
				}
			});
			$("form.advancedSearchForm").each(function () {
				var form = this;
				if (validateAdvancedSearchForm) {
					form.onsubmit = function () {
						return validateAdvancedSearchForm(this);
					};
				}
			});
		});
	}
}

populateMastheadSearchForm();
addSearchFormValidation();


