/*Requires ajax_helper.js, implements DoSearchFormAjaxable function a copy of DoFormAjaxable but to allow input text to submit form with callback function too.*/
var first_visit = true;

var sessiontrack = new Class({
			implements : [Options],
			options : {
				size : 2000,
				track : {}
			},
			initialize : function (tracker, options) {
				
				try {
					if (top.name != '') {
						this.options.track = JSON.decode(top.name);
					}
				} catch (e) {}
			},
			read : function (t) {
				var templateValue = undefined;
				try {
					templateValue = JSON.decode(top.name)[t]
				} catch (e) {}
				return templateValue;
			},
			write : function (key, value) {
				this.options.track[key] = value;
				try {
					top.name = JSON.encode(this.options.track);
				} catch (e) {}
			},
			remove : function (t) {
				this.options.track[t] = undefined;
				try {
					top.name = JSON.encode(this.options.track);
				} catch (e) {}
			},
			clear : function () {
				try {
					top.name = undefined;
				} catch (e) {}
			}
		});

window.addEvent('domready', function () {
		try {
			if (typeof(supportTracker) == 'undefined')
				supportTracker = new sessiontrack();
		} catch (e) {}
	});

window.addEvent('load', function () {
		try {
			if (typeof(supportTracker) == 'undefined')
				supportTracker = new sessiontrack();
		} catch (e) {}
		
		try {
			if (supportTracker.read('hp_supp_open_results') != 'null') {
				//if(Cookie.read('hp_supp_open_results')){
				first_visit = true;
				//alert("declare variable: "+first_visit);
			}
		} catch (e) {}
	});
function DoSearchFormAjaxable(form, submitButton, validationFunction, onCompleteHandler, document, extraParamsForRequestFunction) {
	
	//For regular FORMS
	if (!document) {
		var submit = $(submitButton); //if the submit button doesn't get passed then it does ajaxable ALL submit buttons on the form
		var form = $(form);
		
		var submits;
		var textsubmits;
		if (submit != null) {
			submits = [submit];
		} else {
			submits = $$('form#' + form.id + ' input[type=submit]');
			textsubmits = $$('form#' + form.id + ' input[type=text]');
		}
	}
	//For FORMS inside an IFRAME
	else {
		form = $(document.getElementById(form));
		submits = form.getElements('input[type=submit]');
		textsubmits = form.getElements('input[type=text]');
		
	}
	
	form.action = unescape(form.action); //We decode any url passed as the action
	
	//We add the form id to the request, to be able to track back from where is it coming...
	addHiddenFieldToForm(window.document, form.id, "postFormId", form.id);
	
	submits.each(function (submit) {
			if ((submit) && (form)) {
				
				//Validate if we have an function to add extra data in the POST or GET of the form, per FORM per SUBMIT
				//This function [extraParamsForRequestFunction] is used to have the option of adding extra parameters to the REQUEST of the FORM, without adding HIDDEN fields manually
				if (typeof(extraParamsForRequestFunction) == 'function') {
					extraParamsForRequestFunction(form, submit);
				}
				
				submit.addEvent('click', function (evt) {
						
						//Stops the submission of the form
						new Event(evt).stop();
						
						//Set the event handler on the onComplete event of the asynchronous call
						var preloader = new Element('div', {
									'class' : 'loading_screen'
								});
						preloader.adopt(new Element('div', {
									'class' : 'loading_image'
								}));
						
						if (typeof(onCompleteHandler) == 'function') {
							//addAjaxLoadingMask();
							form.set('send', {
									onRequest : function () {
										$('body').adopt(preloader);
									},
									onSuccess : function () {
										preloader.destroy();
									},
									onFailure : function () {
										preloader.destroy();
									},
									onException : function () {
										preloader.destroy();
									},
									onComplete : onCompleteHandler
									
								});
						}
						
						//Validate the form if we have a validation function
						if (typeof(validationFunction) == 'string') {
							//formValidated = eval(validationFunction);
							//metricsVars=metricsString.split('|');
							
							if (supportTracker.read('hp_supp_open_results') != 'null' || first_visit == false) {
								//if(!Cookie.read('hp_supp_open_results') || first_visit==false ) {
								
								if (supportTracker.read('hp_supp_open_results') != "js_rollup" && supportTracker.read('hp_supp_open_results') != "js_querylessthan") {
									//if( Cookie.read('hp_supp_open_results')!="js_rollup"){
									
									//cuando hiciste busqueda, dio rollup,luego dio otra cosa no rollup, luego haces otra busqueda , luego das f5 te regresaba lo del despues del rollup
									//(Cookie.write('hp_supp_search_query', '/ie/en/system/include/support-ajax-handler.jsp?searchtype=s-002&searchterm=c27', {duration: 0.04, domain:'.hp.com', path:'/'})).dispose();
									supportTracker.remove('hp_supp_search_query');
									
									//(Cookie.write('hp_supp_search_query', '/ie/en/system/include/support-ajax-handler.jsp?searchtype=s-002&searchterm=c27')).dispose();
									supportTracker.remove('hp_supp_search_query');
									try {
										if (form.get('id') == 'search_drivers'){supportTracker.write('searchterm', $$('#drivers_search_box')[0].value);}
										if (form.get('id') == 'search_products'){supportTracker.write('searchterm', $$('#products_troubleshooting_search_box')[0].value);}
									} catch (e) {}
								}
								
								if (supportTracker.read('searchterm') != undefined && supportTracker.read('searchterm') != $$('#drivers_search_box')[0].value && $$('.js_drivers_field_value')[0].innerHTML != $$('#drivers_search_box')[0].value) {
									try {
									if (form.get('id') == 'search_drivers'){supportTracker.write('searchterm', $$('#drivers_search_box')[0].value);}
									if (form.get('id') == 'search_products'){supportTracker.write('searchterm', $$('#products_troubleshooting_search_box')[0].value);}
									} catch (e) {}
								}
								
								first_visit = false;
								//alert("change to false: "+first_visit);
								drivers_field = $$('.js_drivers_field_value');
								drivers_input = $$('#drivers_search_box');
								drivers_error_msg = $$('.js_drivers_error_msg');
								
								products_troubleshooting_field = $$('.js_products_troubleshooting_field_value');
								products_troubleshooting_input = $$('#products_troubleshooting_search_box');
								products_troubleshooting_error_msg = $$('.js_products_troubleshooting_error_msg');
								
								//alert("before check :"+drivers_input[0].value);
								var blank_spaces = /^\s{1,}$/g;
								if (form.get('id') == "search_drivers") {
									if (drivers_input[0].value == "" || drivers_input[0].value == null || drivers_input[0].value.length == 0 || ((drivers_input[0].value.search(blank_spaces)) > -1)) {
										alert(drivers_error_msg.get('html'));
										formValidated = false;
									} else if (drivers_input[0].value == drivers_field.get('html')) {
										alert(drivers_error_msg.get('html'));
										formValidated = false;
									} else {
										formValidated = true;
									}
									
								} else if (form.get('id') == "search_products") {
									if (products_troubleshooting_input[0].value == "" || products_troubleshooting_input[0].value == null || products_troubleshooting_input[0].value.length == 0 || ((products_troubleshooting_input[0].value.search(blank_spaces)) > -1)) {
										alert(products_troubleshooting_error_msg.get('html'));
										formValidated = false;
										
									} else if (products_troubleshooting_input[0].value == products_troubleshooting_field.get('html')) {
										alert(products_troubleshooting_error_msg.get('html'));
										formValidated = false;
									} else {
										formValidated = true;
									}
								} else {
									
									formValidated = true;
								}
							} else {
								formValidated = true;
								first_visit = false;
							}
							
						} else {
							formValidated = true;
						}
						
						//Sends the form to the action path (submits via Ajax)
						if (formValidated) {
							//We add the submit button id to the request, to be able to track back from where is it coming...
							addHiddenFieldToForm(window.document, form.id, "postButtonId", submit.id);
							form.send();
						}
					});
			}
		});
	
	textsubmits.each(function (submit) {
			if ((submit) && (form)) {
				
				//Validate if we have an function to add extra data in the POST or GET of the form, per FORM per SUBMIT
				//This function [extraParamsForRequestFunction] is used to have the option of adding extra parameters to the REQUEST of the FORM, without adding HIDDEN fields manually
				if (typeof(extraParamsForRequestFunction) == 'function') {
					extraParamsForRequestFunction(form, submit);
				}
				
				submit.addEvent('keypress', function (event) {
						
						//Stops the submission of the form
						
						if (event.key == 'enter') {
							new Event(event).stop();
							//Set the event handler on the onComplete event of the asynchronous call
							var preloader = new Element('div', {
										'class' : 'loading_screen'
									});
							preloader = new Element('div', {
										'class' : 'loading_image'
									});
							if (typeof(onCompleteHandler) == 'function') {
								form.set('send', {
										onRequest : function () {
											$('body').adopt(preloader);
										},
										onSuccess : function () {
											preloader.destroy();
										},
										onFailure : function () {
											preloader.destroy();
										},
										onException : function () {
											preloader.destroy();
										},
										onComplete : onCompleteHandler
										
									});
							}
							
							//Validate the form if we have a validation function
							if (typeof(validationFunction) == 'string') {
								//formValidated = eval(validationFunction);
								//metricsVars=metricsString.split('|');
								
								//if(!Cookie.read('hp_supp_open_results') || first_visit==false ) {
								if (supportTracker.read('hp_supp_open_results') != 'null' || first_visit == false) {
									
									if (supportTracker.read('hp_supp_open_results') != "js_rollup" && supportTracker.read('hp_supp_open_results') != "js_querylessthan") {
										//if( Cookie.read('hp_supp_open_results')!="js_rollup"){
										//cuando hiciste busqueda, dio rollup,luego dio otra cosa no rollup, luego haces otra busqueda , luego das f5 te regresaba lo del despues del rollup
										//(Cookie.write('hp_supp_search_query', '/ie/en/system/include/support-ajax-handler.jsp?searchtype=s-002&searchterm=c27', {duration: 0.04, domain:'.hp.com', path:'/'})).dispose();
										supportTracker.remove('hp_supp_search_query');
										
										//(Cookie.write('hp_supp_search_query', '/ie/en/system/include/support-ajax-handler.jsp?searchtype=s-002&searchterm=c27')).dispose();
										supportTracker.remove('hp_supp_search_query');
										try {
											if (form.get('id') == 'search_drivers'){supportTracker.write('searchterm', $$('#drivers_search_box')[0].value);}
											if (form.get('id') == 'search_products'){supportTracker.write('searchterm', $$('#products_troubleshooting_search_box')[0].value);}
										} catch (e) {}
									}
									if (supportTracker.read('searchterm') != undefined && supportTracker.read('searchterm') != $$('#drivers_search_box')[0].value && $$('.js_drivers_field_value')[0].innerHTML != $$('#drivers_search_box')[0].value) {
										try {
											if (form.get('id') == 'search_drivers'){supportTracker.write('searchterm', $$('#drivers_search_box')[0].value);}
											if (form.get('id') == 'search_products'){supportTracker.write('searchterm', $$('#products_troubleshooting_search_box')[0].value);}
										} catch (e) {}
									}
									first_visit = false;
									//alert("change to false: "+first_visit);
									drivers_field = $$('.js_drivers_field_value');
									drivers_input = $$('#drivers_search_box');
									drivers_error_msg = $$('.js_drivers_error_msg');
									
									products_troubleshooting_field = $$('.js_products_troubleshooting_field_value');
									products_troubleshooting_input = $$('#products_troubleshooting_search_box');
									products_troubleshooting_error_msg = $$('.js_products_troubleshooting_error_msg');
									
									//alert("before check :"+drivers_input[0].value);
									var blank_spaces = /^\s{1,}$/g;
									if (form.get('id') == "search_drivers") {
										if (drivers_input[0].value == "" || drivers_input[0].value == null || drivers_input[0].value.length == 0 || ((drivers_input[0].value.search(blank_spaces)) > -1)) {
											alert(drivers_error_msg.get('html'));
											formValidated = false;
										} else if (drivers_input[0].value == drivers_field.get('html')) {
											alert(drivers_error_msg.get('html'));
											formValidated = false;
										} else {
											formValidated = true;
										}
										
									} else if (form.get('id') == "search_products") {
										if (products_troubleshooting_input[0].value == "" || products_troubleshooting_input[0].value == null || products_troubleshooting_input[0].value.length == 0 || ((products_troubleshooting_input[0].value.search(blank_spaces)) > -1)) {
											alert(products_troubleshooting_error_msg.get('html'));
											formValidated = false;
											
										} else if (products_troubleshooting_input[0].value == products_troubleshooting_field.get('html')) {
											alert(products_troubleshooting_error_msg.get('html'));
											formValidated = false;
										} else {
											formValidated = true;
										}
									} else {
										
										formValidated = true;
									}
								} else {
									formValidated = true;
									first_visit = false;
								}
								
							} else {
								formValidated = true;
							}
							
							//Sends the form to the action path (submits via Ajax)
							if (formValidated) {
								//We add the submit button id to the request, to be able to track back from where is it coming...
								addHiddenFieldToForm(window.document, form.id, "postButtonId", submit.id);
								form.send();
							}
						}
					});
			}
		});
}

/* Publish Info (JS)
Date: 5/31/2011 5:54:34 PM
Images: Published all
 */ 

/* UnCompressed - Reason: DISABLED_TARGET-LIVECWADEPLOYER# */

/*
Date: 11/10/2011 11:43:22 AM
All images published
*/
