// lipton tea 2005
// james at tribalddb

// 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();
}

// popup windows
function popupImage( url, name, w, h, bgcolorRef ) {
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open("",name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=no,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.document.write("<html><body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'><img src='"+url+"'></body></html>");
	page.document.bgColor=bgcolorRef ;
	page.document.close();
	page.focus();
}

function popupmsg( 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=no,scrollbars=no,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
}

function popupWinnersRWG( lang ) {
	if ( lang )
		popup( 'winners_rwg_' + lang + '.asp', 'winners', 723, 545 );
	else
		popup( 'winners_rwg.asp', 'winners', 723, 545 );
}

function popupBevGuide( lang ) {
	if ( lang )
		popup( 'bevguide_' + lang + '.asp', 'bevguide', 573, 442 );
	else
		popup( 'bevguide.asp', 'bevguide', 573, 442 );
}

// contact us form validation
function validateContact( form ) {
	if ( !form.fname.value ) {
		alert("Please enter your first name");
		form.fname.focus();
	}
	else if ( !form.lname.value ) {
		alert("Please enter your last name");
		form.lname.focus();
	}
	else if ( !isEmail( form.email.value ) ) {
		alert("Please enter a valid email address");
		form.email.focus();
		form.email.select();
	}
	else if ( form.comments.value.length > 180000 ) {
		alert("Please limit your comments to 180,000 characters");
		form.comments.focus();
	}
	else {
		return true;
	}
	return false;
}

function validateContactFr( form ) {
	if ( !form.fname.value ) {
		alert("Veuillez entrer votre prénom");
		form.fname.focus();
	}
	else if ( !form.lname.value ) {
		alert("Veuillez entrer votre nom");
		form.lname.focus();
	}
	else if ( !isEmail( form.email.value ) ) {
		alert("Veuillez entrer une adresse de courriel");
		form.email.focus();
		form.email.select();
	}
	else if ( form.comments.value.length > 180000 ) {
		alert("Veuillez limiter vos commentaires à 180 000 caractères");
		form.comments.focus();
	}
	else {
		return true;
	}
	return false;
}

function definitionOver( id ) {
	document.getElementById( id ).style.display = "block";
}

function definitionOut( id ) {
	document.getElementById( id ).style.display = "none";
}
	