// kc masterpiece
// common functions
//

var sel;	// this is the selected item in list, so as to not roll off it

function swap( imgName, fileName ) {
	if ( document.images ) {
		document[imgName].src = "images/" + fileName;
	}
}

function choose( page, img ) {
	swap( 'bottle', page + '_bottle_' + img + '.jpg' );
	swap( 'caption', page + '_caption_' + img + '.gif' );
	if ( sel ) {
		// roll off old selected item
		swap( page + '_nav_' + sel, page + '_nav_' + sel + '.gif' );
	}
	sel = img;
	return false;
}

function chooseDefault( page, deflt ) {
	var img = document.location.search;
	if ( img.length > 1 ) {
		img = img.substring( 1, img.length );
		swap( page + '_nav_' + img, page + '_nav_' + img + '_f2.gif' );
		choose( page, img );
	}
	else {
		swap( page + '_nav_' + deflt, page + '_nav_' + deflt + '_f2.gif' );
		choose( page, deflt );
	}
}

function goTo( url ) {
	if ( url.value ) {
		document.location.href = url.value;
	}
}

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 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 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 < 10; i++ ) {
			output += newnum.charAt( i )
			if ( i == 2 || i == 5 )
				output += "-";
		}
	}
	// return value
	phone.value = output;
}

// coupon popup from IQ
function winOpen(url){
	var pWidth = 410;
	var pHeight = 550;
	var X = (screen.width - pWidth) / 2;
	var Y = (screen.availHeight - pHeight) / 2;
	var page = window.open(url,"specialty","toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,width=" + pWidth + ",height=" + pHeight + ",screenX=" + X + ",screenY=" + Y + ",top=" + Y + ",left=" + X + "");
	page.focus();
}

// validation on contact us form
function validateContact( form ) {
	if ( !isName( form.CRS_NAME_FIRST.value ) ) {
		alert("Please enter your correct first name");
		form.CRS_NAME_FIRST.select();
	}
	else if ( !isName( form.CRS_NAME_LAST.value ) ) {
		alert("Please enter your correct last name");
		form.CRS_NAME_LAST.select();
	}
	else if ( !form.CRS_MISC_INFO_2.value || !isEmail( form.CRS_MISC_INFO_2.value ) ) {
		alert("Please enter a valid email address");
		form.CRS_MISC_INFO_2.select();
	}
	else if ( form.CRS_ZIP.value && !isZip(form.CRS_ZIP.value) && !isPostalCode(form.CRS_ZIP.value) ) {
		alert("Please enter a valid zip/postal code");
		form.CRS_ZIP.select();
	}
	else if ( !form.CRS_NOTES.value ) {
		alert("Please enter your message");
		form.CRS_NOTES.focus();
	}
	else {
		return true;
	}
	return false;
}



// new window for external links functions

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;

