// Page Load init()
function init_front_end(){
	configureValidations();
	showActiveLink();
	selectDefaultField();
}
function init_admin(){
	configureValidations();
	showActiveAdminLink();
	selectDefaultField();
}

function selectDefaultField() {
	if ($('default')) {
		$('default').focus();
	}
}

function configureValidations(){
	var forms = document.getElementsByTagName('form');
	for (var i=0; i < forms.length; i++) {
		if (forms[i].className.indexOf("required") > -1) {
			new Validation(forms[i].id, {useTitles : true});
			Validation.add('validate-phone', 'A valid phone number is required.\nEx. 866-394-2848', {
			     pattern : new RegExp(/^((\+\d{1,3}(-| |\.)?\(?\d\)?(-| |\.)?\d{1,5})|(\(?\d{2,6}\)?))(-| |\.)?(\d{3,4})(-| |\.)?(\d{4})(( x| ext)\d{1,5}){0,1}$/) // only letter allowed
			});
		};
	};
}

// show or hide elements onclick
function showHide(id) {
	Element.toggle(id);
	return false;
}

// alternate table row class (background color)
function alternateRows(){

	var rows = document.getElementsByTagName('tr');
	for (var i=0; i < rows.length; i++) {
		if (i % 2) {
					rows[i].className = "odd";
				} else {
					rows[i].className = "even";
				};
	};
}

// popup window for guarantee
var newwindow;
function popwindow(url){
	newwindow=window.open(url,'name','height=600,width=370,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}

// Check for Dupe E-Mail
function emailCheck(email) {
	var update="user_email";
	var url=base_url+"/bin/check_dupe_email.php";
	var params="email="+email;
	var aUpdater=new Ajax.Updater(update,url,{
		method:"post",parameters:params,onComplete:function(transport){
			if(transport.responseText != ""){
				alert(transport.responseText);
				$(update).focus();
				$(update).style.cssText='border:solid 2px #f00;';
			} else {
				$(update).style.cssText='border:inset 2px #ddd;';
			}
		}
	})
	return false;
}

function isUSA(value, type){
	if (value == "United States") {
		$(type + '_province').disabled = true;
		$(type + '_state').disabled = false;
	}else{
		$(type + '_province').disabled = false;
		$(type + '_state').disabled = true;
	};
}

function checkSpelling(id){
	var area = $(id);
	var speller = new spellChecker(area);
	speller.openChecker();
	return false;
}

// show active link
function showActiveLink(){
	var a = document.getElementsByTagName('a');
	var url = location.href;
	
	for (var i=0; i < a.length; i++) {
		if (a[i].href == location.href && a[i].className != 'no_highlight') {
			a[i].style.cssText = "color:#000;text-decoration:none;";
		};	
	};
}

function showActiveAdminLink(){
	var a = document.getElementsByTagName('a');
	var url = location.href;
	
	for (var i=0; i < a.length; i++) {
		if (a[i].href == url) {

			switch(a[i].className){
				case('pager_link'):
					a[i].className = 'active_pager_link';
					break
				case('image'):
				case('nav_link'):
				case('active_nav_link'):
					// do nothing
					break
				default:
					a[i].style.cssText = "font-weight:bold;color:#000;text-decoration:none;";
			}
		}
		else if (url.search('/admin-message/') != '-1') {
			$('nav_admin_message').className = 'active_nav_link';
		}
		
	}
}

function deleteObject(objectClass, id) {
	if (confirm('Are you sure you want to delete this '+objectClass+'?')) {
		var cell = $(objectClass+'_'+id);
		var params = "class="+objectClass+"&id="+id;
		var url = base_url+"/bin/delete_object.php";
		var req = new Ajax.Request(url,{method:"post",parameters:params});
		$(cell).remove();
	}
	return false;
}

function checkPasswordMatch() {
	var p1 = $('user_password');
	var p2 = $('user_password_confirm');
	if (p1.value != p2.value) {
		alert('The passwords entered do not match.');
		p1.style.cssText = 'border:1px solid #902727;';
		p2.style.cssText = 'border:1px solid #902727;';
		p1.focus();
		return false;
	}
	else {
		return true;
	}
}

function addImageRow() {
	var file_input = '<input type="file" name="album[]" value="" class="album_upload" size="50" />';
	var row_template = new Template('#{file_row}');
	
	$('new_file_fields').insert(row_template.evaluate({file_row: file_input}));
	
	return false;
}




