// common js functions
// 
// Copyright (c) 2004 Tribal DDB Canada
// James Marshall www.tribalddb.ca
// 
// v3.01

// image swapping
function swap( id, img ) {
	document.getElementById( id ).src = img.src;
}

function isName( string ) {
	if ( string.match(/^[A-Za-z\'\- ]+$/) )
		return true;
	return false;
}

function isEmail( string ) {
	if ( string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1 ) {
		return true;
	} else {
		return false;
	}
}

function isEmailMulti( string ) {
	// email addresses separated by commas
	if ( string.search(/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(,( )?)?)+$/) != -1 ) {
		return true;
	} else {
		return false;
	}
}

function isDate(year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if( (tempDate.getYear() == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isPhone( phone ) {
	// 3+3+4 digit
	if( phone.match(/^\d\d\d\-\d\d\d\-\d\d\d\d$/) )
		return true;
	return false;
}

function fixPhone( phone ) {
	var num = phone.value;
	var newnum = "";
	var output = "";
	// strip out non-numbers
	for ( var i = 0; i < num.length; i++ )
		if ( num.charAt( i ).match(/\d/) )
			newnum += num.charAt( i );
	if ( newnum ) {
		// rebuild number with hyphen
		for ( var i = 0; i < newnum.length; i++ ) {
			if ( i == 3 || i == 6 )
				output += "-";
			else if ( i == 10 )
				output += " ";
			output += newnum.charAt( i )
		}
	}
	// return value
	phone.value = output;
}

function isZip( zip ) {
	// 5 digit zips
	if( zip.match(/^\d\d\d\d\d$/) )
		return true;
	// 5+4 digit zips
	if( ( zip.match(/^\d\d\d\d\d\d\d\d\d$/) ) || ( zip.match(/^\d\d\d\d\d\-\d\d\d\d$/) ) )
		return true;
	return false;
}

function isPostalCode( pc ) {
	// canadian postal codes (6 or 7 characters)
	if( ( pc.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/) ) || ( pc.match(/^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$/) ) )
		return true;
	return false;
}

function fixPostalCode( pc ) {
	var newpc = "";
	var output = "";
	// strip out non-alphanumeric
	for ( var i = 0; i < pc.value.length; i++ )
		if ( pc.value.charAt( i ).match(/\w/) )
			newpc += pc.value.charAt( i );
	if ( newpc ) {
		// rebuild number with space
		for ( var i = 0; i < 6; i++ ) {
			output += newpc.charAt( i )
			if ( i == 2 )
				output += " ";
		}
	}
	// return value in uppercase
	pc.value = output.toUpperCase();
}

// popup windows
function popup( url, name, w, h ) {
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
}