// o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
// awtp.js
// copyright 2003-2009 tireswing systems, inc. all rights reserved. DO NOT REUSE.
// o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o


function ValidEmail( Email ) {
	Email = trim( Email );
	if( ( ! Email ) || ( ! Email.match( /^[\w\.\-\_]+@[^\.][\w\.\-\_]+$/ ) ) || ( ! Email.match( /\.[a-zA-Z]{2,}$/ ) ) ) {
		return false;
	} else {
		return true;
	}
}


//# o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o

function PopUp( URL , Name , X , Y , Xoff , Yoff ) {
	if( ! Xoff ) { Xoff = 25; }
	if( ! Yoff ) { Yoff = 25; }
	var newWindow = window.open( URL , Name , "screenX=" + Xoff + ",left=" + Xoff + ",screenY=" + Yoff + ",top=" + Yoff + ",width=" + X + ",height=" + Y + ",scrollbars=yes,resizable=yes" , false );
	newWindow.focus();
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #
//
// Function to pop up the bigger picture in a new window.


function PopUpStorePic( myPic ) {
	var myURL = ( "/cgi-bin/p/awtp-showpic.cgi?p=" + myPic );
	var PreviewWindow = window.open( myURL , "storepic" , "width=600,height=600,top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	PreviewWindow.focus();
}

function PopUpPic ( myPic ) {
	var myURL = ( "/cgi-bin/p/awtp-showpic.cgi?p=" + myPic );
	var PreviewWindow = window.open( myURL , "mypic" , "width=600,height=600,top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	PreviewWindow.focus();
}

function PopUpScrapbook ( myPic, width, height ) {
	var myURL = ( "/cgi-bin/p/awtp-showpic.cgi?p=" + myPic );
	var PreviewWindow = window.open( myURL , "mypic" , "width=" + width + ",height=" + height + ",top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	PreviewWindow.focus();
}

function PopUpFaq ( entry, dealer, width, height ) {
	var myURL = ( "/cgi-bin/p/awtp-faqentry.cgi?d=" + dealer + "&id=" + entry );
	var PreviewWindow = window.open( myURL , "faq" , "width=" + width + ",height=" + height + ",top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	PreviewWindow.focus();
}

function PopUpProductQueryForm ( dealer, product, width, height ) {
	var myURL = ( "/cgi-bin/p/awtp-product-query-form.cgi?d=" + dealer + "&id=" + product );
	var ProductQueryWindow = window.open( myURL , "faq" , "width=" + width + ",height=" + height + ",top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	ProductQueryWindow.focus();
}

function PopUpProductCategoryQueryForm ( dealer, category, width, height ) {
	var myURL = ( "/cgi-bin/p/awtp-product-category-query-form.cgi?d=" + dealer + "&id=" + category );
	var ProductQueryWindow = window.open( myURL , "faq" , "width=" + width + ",height=" + height + ",top=25,left=50,screenX=50,screenY=25,resizable,scrollbars" , false );
	ProductQueryWindow.focus();
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #



function VFGB() {
	var Mesg = "";
	if( ! document.guestbook.name.value ) {
		Mesg += "      Please fill in the NAME field.          \n";
	}
	if( ! ValidEmail( trim( document.guestbook.email.value ) ) ) {
		Mesg += "  Please complete the EMAIL field (it will never be visible to the public).      \n";
	}
	if( Mesg ) {
		alert( "\n\n    The form cannot be submitted:          \n\n" + Mesg + "\n\n" );
		return false;
	} else {
		document.guestbook.thing.value="desmond";
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function VFForm() {
	var Mesg = "";
	if( ! document.contact.name.value ) {
		Mesg += "      + Name is empty          \n";
	}
	if( ! document.contact.comments.value ) {
		Mesg += "      + Comments field is empty          \n";
	}
	if( Mesg ) {
		alert( "\n    The form can't be submitted yet:          \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

function VFSignin() {
	var Mesg = "";
	if( ! ValidEmail( trim( document.signin.email.value ) ) ) {
		Mesg += "  + Email is empty or invalid      \n";
	}
	if( ! trim( document.signin.password.value ) ) {
		Mesg += "  + Password is empty      \n";
	}
	if( Mesg ) {
		alert( "\n  The sign in form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}

function VFMemberSignup( forumflag ) {
	var Mesg = "";
	var GoodEmail = false;
	var e1 = trim( document.signup.email.value );
	var e2 = trim( document.signup.emailv.value );

	if( ! trim( document.signup.name.value ) ) {
		Mesg += "    + Name field is empty        \n";
	}
	if( ValidEmail( e1 ) && ( e1 == e2 ) ) {
		GoodEmail = true;
	}
	if( ! GoodEmail ) {
		Mesg += "    + Email fields must be valid and match exactly      \n";
	}
	if( forumflag && ! trim( document.signup.alias.value ) ) {
		Mesg += "    + Alias field is empty      \n";
	}
	if( forumflag && ! document.signup.agree.checked ) {
		Mesg += "    + You must accept the membership terms      \n";
	}
	if( Mesg ) {
		alert( "\n  The form cannot be submitted yet:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		document.signup.thing.value = "desmond";
		return true;
	}
}

function VFPublicMemberSettings() {
	var Mesg = "";
	var Email = trim( document.settings.email.value.toLowerCase() );
	var Emailv = trim( document.settings.emailv.value.toLowerCase() );
	var allowChangePw = trim( document.settings.allow_change_password.value );


	if( ! trim( document.settings.name.value ) ) {
		Mesg += "    + Name field is empty      \n";
	}

	if( ! trim( document.settings.password.value ) || ! trim( document.settings.passwordv.value ) ) {
		Mesg += "    + Invalid password      \n";
	}

	if( Email != Emailv ) {
		Mesg += "    + Both Email fields must match exactly      \n";
	} else {
		if( ! ValidEmail( Email ) ) {
			Mesg += "    + Email must be a valid email pattern      \n";
		}
	}

	if( allowChangePw == "1" ) {
		var subMesg = VFPublicMemberSettingsPW();
		if( subMesg != "" ) {
			Mesg += subMesg;
		}
	}

	if( Mesg ) {
		alert( "\n  The form cannot be submitted:      \n\n" + Mesg + "\n" );
		return false;
	} else {
		return true;
	}
}


function VFPublicMemberSettingsPW () {
	var Password = document.settings.password.value.toLowerCase();
	var Passwordv = document.settings.passwordv.value.toLowerCase();
	var Mesg = "";
	if( Password && Passwordv && ( Password == Passwordv ) ) {
		// Good!
	} else {
		Mesg += "    + Both Password forms must be valid and match exactly      \n";
	}
	return Mesg;
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

function trim( myStr ) {
	myStr = myStr.replace( /^\s+/ , "" );
	myStr = myStr.replace( /\s+$/ , "" );
	return myStr;
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function HL( Row , Color ) {
	document.getElementById( Row ).bgColor=Color;
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function UHL( Row , Color ) {
	document.getElementById( Row ).bgColor=Color;
}


//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

function ValidPhone( Phone ) {
	var GoodSwitch = 0;
	if( Phone.match( /^[\d \-\(\)\.]+$/ ) ) {
		if( Phone.replace( /[^\d]/g , "" ).match( /^1?[23456789]\d{9}$/ ) ) {
			GoodSwitch++;
		}
	}
	return GoodSwitch;
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #

function UpdateShipping( Item ) {
	var ppform;
	var ppformname = "paypal" + Item;
	var selname = "shipsel" + Item;

	for ( var i = 0; i<document.forms.length; i++ ) {
		if( document.forms[i].name == ppformname ) {
			// Found the proper form.
			// Now grab the selector to find the new shipping cost

			var selector = document.all.item( selname );
			var si = selector.selectedIndex;
			var newshipid = selector.options[si].value;
			var shipmethod = selector.options[si].innerText;

			document.forms[i].shipid.value = newshipid;
		}
	}
}

//# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - #


function ToggleCB( CB ) {
	var MYCB = eval( "document." + CB );
	if( MYCB.checked == true ) {
		MYCB.checked = false;
	} else {
		MYCB.checked = true;
	}
}


//# o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o


function ToggleRadio( RADIONAME , POSITION ) {
	eval( "document." + RADIONAME + "[" + POSITION + "].checked=true" );
}


//# o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o


var FirstClick = 1;
var FirstClickSimple = 1;

function VFSearch( WHICH ) {
	var DF;
	if( WHICH == "simple" ) {
		var DF = document.search;
	} else {
		var DF = document.searchfull;
	}
	var SEARCHTYPE = 0;
	var TERM = DF.searchterm.value;
	TERM = TERM.replace( /^(\"|\s)+/g , "" );
	TERM = TERM.replace( /(\"|\s)+\$/g , "" );
	TERM = TERM.replace( /\s+/g , " " );

	if( WHICH == "simple" ) {
		SEARCHTYPE = 0; // no matter what
	} else {
		if( DF.searchtype[ 1 ].checked == true ) {
			SEARCHTYPE = 1;
		} else if( DF.searchtype[ 2 ].checked == true ) {
			SEARCHTYPE = 2;
		}
	}

	if( SEARCHTYPE == 2 ) {
		// exact string
		if( TERM ) {
			TERM = TERM.replace( /[\~\!\#\$\%\^\&\*\(\)\=\+\[\]\{\}\\\|\;\:\"\,\<\>\/\?]/g , "" ); // " #\@\.\-\_
			DF.qsearch.value = TERM;
			DF.qtype.value = "exact";
			DF.thing.value = "desmond";
			return true;
		} else {
			alert( "Please enter some text for which to search..." );
			return false;
		}

	} else {
		// either any or all, but filter out unnecessary words, no matter.
		TERM = TERM.replace( /[\~\!\#\$\%\^\&\*\(\)\=\+\[\]\{\}\\\|\;\:\"\,\<\>\/\?]/g , "" ); // " #\@\.\-\_
		var Words = TERM.split( " " );
		var RemoveHash = new Object();
		var RemoveArray = new Array();
		for( var i = 0 ; i < Words.length ; i++ ) {
			var ThisWord = Words[i].toLowerCase();
			if( ThisWord.length < 3 ) {
				RemoveHash[ ThisWord ] = 1;
			}
		}
		var TOOCOMMON = new Array( "and" , "are" , "for" , "that" , "the" , "them" , "these" , "this" , "those" , "was" , "were" );
		for( var i = 0 ; i < TOOCOMMON.length ; i++ ) {
			var _regex = new RegExp( "(^|\\s)" + TOOCOMMON[ i ] + "(\\s|$)" , "i" );
			if( TERM.match( _regex ) ) {
				RemoveHash[ TOOCOMMON[ i ] ] = 1;
			}
		}
		for( Word in RemoveHash ) {
			var _regex = new RegExp( "(^|\\s)" + Word + "(\\s|$)" , "ig" );
			TERM = TERM.replace( _regex , " " );
			RemoveArray[ RemoveArray.length ] = Word;
		}

		RemoveArray.sort();
		TERM = trim( TERM );
		if( TERM ) {
			TERM = TERM.replace( / /g , "," );
			DF.qsearch.value = TERM;
			DF.qignore.value = RemoveArray.join( "," );
			if( SEARCHTYPE == 1 ) {
				DF.qtype.value = "all";
			} else {
				DF.qtype.value = "any";
			}
			DF.thing.value = "desmond";
			return true;
		} else {
			var MESG = "There are no (significant) words for which to search...          \n";
			if( RemoveArray.length ) {
				if( RemoveArray.length == 1 ) {
					MESG += "\nThe word \"" + RemoveArray[ 0 ] + "\" is too short / common for searching.          \n";
				} else {
					MESG += "\nThe following words are too short / common for searching:          \n";
					MESG += "      " + RemoveArray.join( ", " ) + "        \n";
				}
			}
			alert( MESG + "\n" );
			return false;
		}
	}
}

function CheckClearTerms( WHICH ) {
	var DF;
	if( WHICH == "simple" ) {
		var DF = document.search;
		if( FirstClickSimple ) {
			if( DF.searchterm.value.match( /^search$/ ) ) {
				DF.searchterm.value = "";
			}
			FirstClickSimple = 0;
		}
	} else {
		var DF = document.searchfull;
		if( FirstClick ) {
			if( DF.searchterm.value.match( /^text to search for$/ ) ) {
				DF.searchterm.value = "";
			}
			FirstClick = 0;
		}
	}
}

//# o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
//# o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
