
//patterns used by validation
var pattern_email = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
var pattern_phone = /1?([0-9]{3})([0-9]{3})([0-9]{4})/;
var pattern_url = /^(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i;


//prototype extensions
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function focusSearch() {
	var sb = document.getElementById('searchbox');
	sb.focus();
	return false;
}


function validateDate(id) {
	var d = $(id);
	if (d.value.length != 0) {
		var pattern = /(\d{1,2})[\/\-\s,]?(\d{1,2})[\/\-\s,]?(\d{2,4})/;
		var regex = new RegExp(pattern);
		var match = regex.exec(d.value);
	
		if (null == match || match.length != 4) {
			alert('Invalid Date');
			d.value = '';
			displayDatePicker(id);
		}
		
		var month = match[1];
		if (month.length != 2) {
			month = '0' + month;
		}
		
		var day = match[2];
		if (day.length != 2) {
			day = '0' + day;
		}
	
		var year = match[3];
		if (year.length != 4) {
			year = '20' + year;
		}
		
		d.value = month + '/' + day + '/' + year;

	}
}


function changeSearchOptions() {
	var option = $F('report');
	if (option == 'ticket') {
		$('report_type_history').hide();
		$('report_type_tickets').show();
	}
	else {
		$('report_type_history').show();
		$('report_type_tickets').hide();
	}
}

function advancedSearch() {
	var advanced = $F('advanced_search');
	if (null != advanced) {
		$('options').show();
	}
	else {
		$('options').hide();
	}
}


function terms() {
	var termswin = window.open ("/img/terms.html","helpwin","location=0,status=0,scrollbars=1,width=300,height=300");
	termswin.focus();
	return false;
}

function hideShipping() {
	var obj = Form.getElements('shipping');
	if ($('shippingsameasbilling').checked) {
		$('shipping').hide();
		for (var i=0;i<obj.length;i++) {
			obj[i].disable();
		}	
	}
	else {
		$('shipping').show();
		for (var i=0;i<obj.length;i++) {
			obj[i].enable();
		}	
	}
}

function getValidateProperties(str) {
		var arr = new Array();
		
		if (-1 != str.indexOf(':')) {
			id = str.substring(0, str.indexOf(':'));
			type = str.substring(str.indexOf(':') + 1);
		}
		else {
			id = str;
			type = 'text';
		}
		arr.push(id);
		arr.push(type);
		return arr;
}

var btnSelected = null;

function validate() {
	var action;
	if (null == btnSelected) {
		action = 'save';
	}
	else {
		action = btnSelected.value.toLowerCase();
	}
	
	if ('cancel' != action) {
		var required = $('required');
		if (null != required) {
			if (!testElement(required.getValue().split(','), true)) {
				return false;
			}
		}
	
		var optional = $('optional');
		if (null != optional) {
			if (!testElement(optional.getValue().split(','), false)) {
				return false;
			}
		}
	}

	return true;
}

function testElement(fields, required) {
		
	var obj;
	var id;
	var type;
	var properties;
	

	for (var i=0; i<fields.length; i++) {

		properties = getValidateProperties(fields[i]);
		id = properties[0];
		type = properties[1];
		obj = $(id);

		if (null != obj) {
			if (!obj.disabled) {
				//trim any whitespace...
				obj.value = obj.value.trim();
				if (obj.value.length == 0) {
					if(required) {
						obj.setStyle({background:'pink'});
						obj.focus();
						alert("Value cannot be blank.");
						return false;
					}
				}
				else if (type == 'email' && !pattern_email.test(obj.value)) {
					alert("Must be a valid email address.");
					obj.setStyle({background:'pink'});
					obj.focus();
					return false;
				}
				else if (type == 'phone') {
					var match =  pattern_phone.exec(obj.value.replace(/[^0-9]/g, ''));
					if (null == match) {
						alert("Must be a valid 10 digit phone number.");
						obj.setStyle({background:'pink'});
						obj.focus();
						return false;
					}
					else {
						obj.value = "(" + match[1] + ") " + match[2] + "-" + match[3]; 
					}
				}
				else if (type == 'url' && !pattern_url.test(obj.value)) {
					alert("Must be a valid url address.");
					obj.setStyle({background:'pink'});
					obj.focus();
					return false;
				}				
				obj.setStyle({background:'none'});
			}
		}
	}

	return true;
}


function exportExcel() {
	$('action_form').submit(); 
	return false;
}


function sortReport(name) {
	$('action').value = 'search'; 

	if ($('sort').value == name) {
		if (0 == $('sort_dir').value) {
			$('sort_dir').value = 1;
		}
		else {
			$('sort_dir').value = 0;
		}
	}
	else {
		$('sort').value = name;
		$('sort_dir').value = 0;
	}

	$('action_form').submit(); 
}


function formFocus(id) {
	addPageLoadEvent(function() {$(id).focus()});
}

function addPageLoadEvent(event) {
	Event.observe(window, 'load', event, false);
}