// Generic Declarations
var popUpWin=0;
var popUpWinScroll=0;

// Form Validation
function onlyNumbers(inputString)
{
  var searchForNumbers = /\D+\_+\W+\s+\S+/
  return (searchForNumbers.test(inputString)) ? false : true;
}

function stripString(source, needles)
{
	var returnStr = "";

	for (var c = 0; c < source.length; c++)
	{
		if (needles.indexOf(source.charAt(c)) != -1)
			returnStr += source.charAt(c);
	}

	return returnStr;
}

function isNumberInt(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}

function isNumberFloat(inputString)
{
  return (!isNaN(parseFloat(inputString))) ? true : false;
}

function valLength (theVal, theLength)
{
	if (theVal == null) return false;
	var checkVal = theVal.replace(/ /g, '');
	if (checkVal.length < theLength)
		return false;
	else
		return true
}

// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
function valEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		   return false;

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		   return false;

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		    return false;

		 if (str.indexOf(at,(lat+1))!=-1)
		    return false;

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		    return false;

		 if (str.indexOf(dot,(lat+2))==-1)
		    return false;

		 if (str.indexOf(" ")!=-1)
		    return false;

 		 return true;
}

function valPhone(theTest)
{
	// Strip all non-numeric characters
	var theNumber = new String(stripString(theTest, "0123456789"));

	if (theNumber.length != 10)
		return false;

	if (!isNumberInt(theNumber))
		return false;

	return "(" + theNumber.substr(0, 3) + ")" + " " + theNumber.substr(3, 3) + "-" + theNumber.substr(6, 4);
}

function valDate(theTest)
{
	// Strip all non-numeric characters
	var theNumber = new String(stripString(theTest, "0123456789"));

	if (theNumber.length != 8)
		return false;

	if (!isNumberInt(theNumber))
		return false;

	if (theNumber.substr(0, 2) > 12)
		return false;

	if (theNumber.substr(2, 2) > 31)
		return false;

	if (theNumber.substr(4, 4) > 3000)
		return false;

	return theNumber.substr(0, 2) + "/" + theNumber.substr(2, 2) + "/" + theNumber.substr(4, 4);
}

// Popup Windows
function popUpWindowScroll(URLStr, width, height, left, top)
{
	if (!left)
		left = 30;

	if (!top)
		top = 30;

	if(popUpWinScroll)
	{
		if(!popUpWinScroll.closed) popUpWinScroll.close();
	}

	popUpWinScroll = window.open(URLStr, 'popUpWinScroll', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height/*+',left='+left+',top='+top*/);
	if (popUpWinScroll)
	{
		popUpWinScroll.opener = window;
		popUpWinScroll.focus();

		return popUpWinScroll;
	}

	if (Math.random() > .9) alert('Your web browser is blocking pop-up windows.\n\nTo get the full use out of our website, please consult your web browser\'s documentation for enabling pop-up windows from trusted sites.');
	return false
}

function popUpWindow(URLStr, width, height, left, top)
{
	if (!left)
		left = 30;

	if (!top)
		top = 30;

	if (popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}

	popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

	if (! popUpWin)
	{
		alert('A pop-up blocker has prevented this site from openning a window');
		return false;
	}


	popUpWin.opener = window;
	popUpWin.focus();

	return popUpWin;
}

// Generic Page Functions
function printPage() { print(document); }

function simplePreload()
{
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

function toggleTbody(id, status)
{
	if (document.getElementById)
	{
		var tbod = document.getElementById(id);
		if (tbod && typeof tbod.className == 'string')
		{
			tbod.className = status;
		}
	}
	return false;
}

var state = 'none';

function showhide(layer_ref)
{
	switch($(layer_ref).style.display)
	{
		case 'none':
			$(layer_ref).style.display = 'block';
		break;
		default:
			$(layer_ref).style.display = 'none';
		break;
	}
}

function hide(layer_ref)
{
	state = 'none';
	$(layer_ref).style.display = state;
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}


//Quick Hide / Show for DIVS supports both visibility and display methods.

function DIV( divID ){
	this.prototype.divID = divID;

	if( typeof DIV._initialized == 'undefined'){
		DIV.prototype.hideMe = function(){
			document.getElementById( this.divID ).style.visibility = 'hidden';
		};
		DIV.prototype.showMe = function(){
			document.getElementById( this.divID ).style.visibility = 'visible';
		};
		DIV.prototype.openMe = function(){
			document.getElementById( this.divID ).style.display = 'block';
		};
		DIV.prototype.closeMe = function(){
			document.getElementById( this.divID ).style.display = 'none';
		};
	DIV._initialized = true;
	}
}

DIV.hide = function( divID ){
	document.getElementById( divID ).style.visibility = 'hidden';
}
DIV.show = function( divID ){
	document.getElementById( divID ).style.visibility = 'visible';
}
DIV.open = function( divID ){
	document.getElementById( divID ).style.display = 'block';
}
DIV.close = function( divID ){
	document.getElementById( divID ).style.display = 'none';
}
DIV.zIndex = function( divID ,  z ){
	document.getElementById( divID ).style.zIndex = z;
}
DIV.height = function( divID , height ){
	document.getElementById( divID ).style.height = height;
}
//Comment Form Validatin
function validateComments(frm){
	msg = '';
	if(!valEmail(frm.commentemail.value))
		msg += 'Please provide a valid email address.\n';
	if(!valLength(frm.commentname.value, 1))
		msg += 'Please provide your name.\n';
	if(!valLength(frm.commentcity.value, 1) || frm.commentstate.value.length != 2)
		msg += 'Please provide your city and state.\n';
	if(!valLength(frm.comment.value, 1))
		msg += 'Please leave a comment\n';

	if(msg.length > 0){
		alert(msg);
		return false;
	}else{
		return true;
	}
}

function validateRecipe(frm){
	msg = '';
	if(!valEmail(frm.recipeemail.value))
		msg += 'Please provide a valid email address.\n';
	if(!valLength(frm.recipename.value, 1))
		msg += 'Please provide your name.\n';
	if(!valLength(frm.recipecity.value, 1) || frm.recipestate.value.length != 2)
		msg += 'Please provide your city and state.\n';
	if(!valLength(frm.recipetitle.value, 1))
		msg += 'Please leave a title for your recipe\n';
	if(!valLength(frm.recipeingredients.value, 1))
		msg += 'Please enter at least one ingredient\n';
	if(!valLength(frm.recipeinstructions.value, 1))
		msg += 'Please enter instructions for your recipe\n';

	if(msg.length > 0){
		alert(msg);
		return false;
	}else{
		return true;
	}
}

function validateTripReview(frm){
	msg = '';
	if(!valEmail(frm.recipeemail.value))
		msg += 'Please provide a valid email address.\n';
	if(!valLength(frm.recipename.value, 1))
		msg += 'Please provide your name.\n';
	if(!valLength(frm.recipecity.value, 1) || frm.recipestate.value.length != 2)
		msg += 'Please provide your city and state.\n';
	if(!valLength(frm.recipetitle.value, 1))
		msg += 'Please leave a title for your recipe\n';
	if(!valLength(frm.recipeingredients.value, 1))
		msg += 'Please enter at least one ingredient\n';
	if(!valLength(frm.recipeinstructions.value, 1))
		msg += 'Please enter instructions for your recipe\n';

	if(msg.length > 0){
		alert(msg);
		return false;
	}else{
		return true;
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

