// SearchValidation.js

//////////////////////////////////////////////////////////
// This function will prevent submission of the specified
// form if the value of the specified field is null or whitespace.
// 
// If submission is prevented, the specified error will be 
// displayed in a messagebox.
//////////////////////////////////////////////////////////
function doSubmit(form, fieldName, errorMessage) {
	var fieldValue = form.elements[fieldName].value;
	var trimmed = fieldValue.replace(/^\s+|\s+$/g, '') ;
	if (trimmed=="") {
		alert(errorMessage);
		return false;
	} else {
		return true;
	}	
}

// if the current page is https and the form is submitted to a
// non-https url then the browser pops up a dialogue box to alert
// the user to the https-to-http protocol transition.  To suppress this
// we build a url from the form elements and transition to that, and FF & IE
// are happy to do this without popping up a dialogue box.
function formToGet(form) {
	var targetProtocol = (form.action.indexOf('https:') == 0) ? 'https:' : ((form.action.indexOf('http:') == 0) ? 'http:' : window.location.protocol);
	// only do the workaround if the current protocol is https and the target is http
	if (window.location.protocol == 'http:' || targetProtocol == 'https:') return true;
	
	var href="";
	$(form).find('input[@type != "image"]').each(function() {
		if (href.length > 0) href += '&';
		href += $(this).attr('name') + '=' + encodeURIComponent($(this).attr('value'));
    	});
    
	href = form.action + '?' + href;

	// if the url is too long then let the form submit normally otherwise IE has problems.
	if (href.length < 2000) {
		window.location.href=href;
		// return false to abort the form submit
		return false;
	} else {
		return true;
	}
}

