// JavaScript Document
function validate_required(field)
{
	with (field)
	{
		if (value==null||value=="")
		{
			return false;
		} else {
			field.focus();
			return true;
		}
	}
}
function validate_expression(field, expr)
{
	var ex = '';
	switch (expr) {
		case 'alpha':
			ex = /^[a-zA-Z ]+$/;
			break;
		case 'number':
			ex  = /^[0-9]+$/;
			break;
		case 'alphanum':		
			ex = /^[0-9a-zA-Z ]+$/;
			break;
		case 'email':
			ex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;	
			break;
		default: // alpha
			ex = /^[a-zA-Z ]+$/;
			break;
	}

	var val = field.value.replace(/ /,'');
	if (val.match(ex)) {
		return true;	
	} else {
		field.focus();
		return false;
	}
}

function compare_values(field_a, field_b) {
	if (field_a.value == field_b.value) {
		return true;
	} else {
		field_a.focus();
		return false;
	}
}


function confirm_delete(obj_name, id, page) {
	if (confirm("Are you sure you want to delete " + obj_name + "?")) {
		window.location.href = page + "?id=" + id + "&action=remove";
	}
}

function detect_ie() {
	var ver = 0;
	var msg = '';
	var ua = navigator.userAgent;
	var av = navigator.appVersion;
	if (ua != null)
	{
		if (ua.indexOf("MSIE") != -1)
		{
			if (av.indexOf("MSIE")!=-1){
				var temp=av.split("MSIE");
				ver=parseFloat(temp[1]);
			}
		}
	}
	return ver;
}
function gotoCategory () {
	var ddl = document.getElementById('category_id');
	var idx = ddl.selectedIndex;
	var category_id = ddl.options[idx].value;
	var incPath = './categories/';
	if(location.href.indexOf('categories/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'categories.php?category=' + category_id;	
}
function gotoVendor () {
	var ddl = document.getElementById('vendor_id');
	var idx = ddl.selectedIndex;
	var vendor_id = ddl.options[idx].value;
	var incPath = './categories/';
	if(location.href.indexOf('categories/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'vendors.php?vendor=' + vendor_id;	
}
function gotoLocalCategory () {
	var ddl = document.getElementById('localcategory_id');
	var idx = ddl.selectedIndex;
	var category_id = ddl.options[idx].value;
	var incPath = './local/';
	if(location.href.indexOf('local/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'categories.php?category=' + category_id;	
}
function gotoLocalVendor () {
	var ddl = document.getElementById('vendor_id');
	var idx = ddl.selectedIndex;
	var vendor_id = ddl.options[idx].value;
	var incPath = './local/';
	if(location.href.indexOf('local/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'vendors.php?vendor=' + vendor_id;	
}
function gotoLocalCounty () {
	return; //debug
	// not used in current site implementation 2008.11.06 Matt Cooper
	var ddl = document.getElementById('county_id');
	var idx = ddl.selectedIndex;
	var county = ddl.options[idx].text;
	var incPath = './local/';
	if(location.href.indexOf('local/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'regional_merchants.php?region=' + county;	
}

function gotoLocalCity () {
	// not used in current site implementation 2008.11.06 Matt Cooper
	var ddl = document.getElementById('city_id');
	var idx = ddl.selectedIndex;
	var city = ddl.options[idx].value;
	var incPath = './local/';
	if(location.href.indexOf('local/') > 0)
	{
		incPath = './';
	}
	
	window.location = incPath + 'city.php?city=' + city;	
}

function showImage(){
	document.write('<img src="'+theImages[whichImage]+'" alt="Marketplace Northwest" />');
}

function toggleFeature(featureId, show)
{
	var vis = 'hidden';
	if(show == 1)
		vis = 'visible';
	
	document.getElementById('feature'+featureID).style.visibility = vis;	
}

function checkContactForm()
{
	var retval = true;
	var msg = '';
	var name = document.getElementById('txtName');
	var phone = document.getElementById('txtPhone');
	var email = document.getElementById('txtEmail');
	if(!validate_required(name))
	{
		retval = false;
		document.getElementById('txtName').focus();
		msg = msg + 'Please enter your name before proceeding.\n';		
	}
	if(!validate_expression(name, 'alphanum'))
	{
		retval = false;
		document.getElementById('txtName').focus();
		msg = msg + 'Invalid entry.  Please re-enter or remove the contents of the Name field.\n';
	}
	if(!validate_required(email))
	{
		retval = false;
		document.getElementById('txtEmail').focus();
		msg = msg + 'Please enter your email address before proceeding.\n';		
	}
	if(!validate_expression(email, 'email'))
	{
		retval = false;
		document.getElementById('txtEmail').focus();
		msg = msg + 'Please enter a valid email address.\n';		
	}
	if(msg.length > 0)
	{
		alert(msg);
	}	
	
	return retval;
}

function checkSearchForm()
{
	var retval = true;
	var msg = '';
	var term = document.getElementById('search');
	if(!validate_required(term) || !validate_expression(term, 'alphanum'))
	{
		retval = false;
		document.getElementById('search').focus();
		msg = msg + 'Invalid search criteria.  Please try again.\n';
	}	
	if(msg.length > 0)
	{
		alert(msg);
	}
	
	return retval;
}

function checkStoreForm()
{
	var retval = true;
	var msg = '';
	// REQUIRED STORE INFO: storename, storeareacode, storephone1, storephone2, website
	var storename = document.getElementById('storename');
	var storeareacode = document.getElementById('storeareacode');
	var storeprefix = document.getElementById('storephone1');
	var storesuffix = document.getElementById('storephone2');
	var website = document.getElementById('website');
	if(!validate_required(storename))
	{
		retval = false;
		storename.focus();
		msg = msg + 'Invalid Store Name.\n';
	}
	if(!validate_required(storeareacode) && !(validate_expression(storeareacode, 'number')))
	{
		retval = false;
		storeareacode.focus();
		msg = msg + 'Please enter the complete Phone Number for your store.\n';
	}
	if(!validate_required(storeprefix) && !(validate_expression(storeprefix, 'number')))
	{
		retval = false;
		storeprefix.focus();
		msg = msg + 'Please enter the complete Phone Number for your store.\n';
	}
	if(!validate_required(storesuffix) && !(validate_expression(storesuffix, 'number')))
	{
		retval = false;
		storesuffix.focus();
		msg = msg + 'Please enter the complete Phone Number for your store.\n';
	}
	if(!validate_required(website))
	{
		retval = false;
		website.focus();
		msg = msg + 'Please enter the your store\'s website.\n';
	}
	
	// REQUIRED PRODUCT INFO: category, title
	var title = document.getElementById('title');
	var category = document.getElementById('category');
	if(!validate_required(title))
	{
		retval = false;
		title.focus();
		msg = msg + 'Please enter a title for your product.\n';
	}
	if(!validate_required(category) && !(validate_expression(category, 'number')))
	{
		retval = false;
		category.focus();
		msg = msg + 'Please select a category for your product.\n';
	}
	
	// REQUIRED CONTACT INFO: name, email
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	if(!validate_required(name) && !(validate_expression(name, 'alphanum')))
	{
		retval = false;
		name.focus();
		msg = msg + 'Please enter your contact name.\n';
	}
	if(!validate_required(email) && !(validate_expression(email, 'email')))
	{
		retval = false;
		email.focus();
		msg = msg + 'Please enter your email address.\n';
	}
	
	if(msg.length > 0)
	{
		alert(msg);
	}
	
	return retval;
}

function checkLogin()
{
	var retval = true;
	var msg = '';
	var usr = document.getElementById('username');
	var pwd = document.getElementById('passcode');

	if(!validate_required(usr) || !validate_expression(usr, 'email'))
	{
		retval = false;
		document.getElementById('username').focus();
		msg = msg + 'Please enter your email address before proceeding.\n';		
	}
	if(!validate_required(pwd) || !validate_expression(pwd, 'alphanum'))
	{
		retval = false;
		document.getElementById('passcode').focus();
		msg = msg + 'Invalid password.  Please try again.\n';		
	}
	if(msg.length > 0)
	{
		alert(msg);
	}	
	
	return retval;
}






