//FORM VALIDATION
function isEmailAddress (string) {
	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return addressPattern.test(string);
}

function notEmptyStr(strValue) {
	var objRegExp  = /\S/;
	return objRegExp.test(strValue);
}
	
function isPhoneNumber(strValue) {
	var cleanPhone = strValue.replace(/\D/g, "")
	var tenDigits  = /\d{10}/;
	return tenDigits.test(cleanPhone);
}
function isZipCode(strValue) {
	var fiveDigits  = /^\d{5}$/;
	return fiveDigits.test(strValue);
}
function isInt(strValue) {
	var anyDigits  = /^\d+$/;
	return anyDigits.test(strValue);
}
function is3Int(strValue) {
	var anyDigits  = /^\d{3}$/;
	return anyDigits.test(strValue);
}
function is4Int(strValue) {
	var anyDigits  = /^\d{4}$/;
	return anyDigits.test(strValue);
}

function check_larger_form() {
	var error='';
	if (notEmptyStr(document.getElementById('larger_quantity').fname.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('larger_quantity').lname.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('larger_quantity').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (notEmptyStr(document.getElementById('larger_quantity').quantity.value)==false) {
		error+='Please enter the quantity you need.\r\n';
	}
	if (error=='') {
		document.getElementById('larger_quantity').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function check_contact_form() {
	var error='';
	if (notEmptyStr(document.getElementById('contact_us').fname.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('contact_us').lname.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('contact_us').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (error=='') {
		document.getElementById('contact_us').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function recipe_check() {
	var error='';
	if (notEmptyStr(document.getElementById('recipeSubmitter').full_name.value)==false) {
		error+='Please enter your name.\r\n';
	}
	if (isEmailAddress(document.getElementById('recipeSubmitter').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (notEmptyStr(document.getElementById('recipeSubmitter').title.value)==false) {
		error+='Please enter the title of your recipe.\r\n';
	}		
	if (notEmptyStr(document.getElementById('recipeSubmitter').servings.value)==false) {
		error+='Please enter the number of servings your recipe creates.\r\n';
	}
	if (notEmptyStr(document.getElementById('recipeSubmitter').ingredients.value)==false) {
		error+='Please enter the ingredients for your recipe.\r\n';
	}	
	if (notEmptyStr(document.getElementById('recipeSubmitter').directions.value)==false) {
		error+='Please enter the directions for your recipe.\r\n';
	}	
	if (document.getElementById('recipeSubmitter').terms_and_conditions.checked==false) {
		error+='Please read and agree to the Terms & Conditions.\r\n';
	}			
	if (error=='') {
		document.getElementById('recipeSubmitter').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function contact_check() {
	var error='';
	if (notEmptyStr(document.getElementById('contactUs').first_name.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('contactUs').last_name.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('contactUs').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (notEmptyStr(document.getElementById('contactUs').message.value)==false) {
		error+='Please enter a message.\r\n';
	}	
	if (document.getElementById('contactUs').terms_and_conditions.checked==false) {
		error+='Please read and agree to the Terms & Conditions.\r\n';
	}	
	if (error=='') {
		document.getElementById('contactUs').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function newsletter_check() {
	var error='';
	if (isEmailAddress(document.getElementById('newsletterForm').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (error=='') {
		document.getElementById('newsletterForm').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function launch_pop() {
	if (document.getElementById('terms_conditions')) {
		centerDiv("terms_conditions", 20, 540);
	}
}

function hide_pop() {
	if (document.getElementById('terms_conditions')) {
		document.getElementById('terms_conditions').style.display = "none";
	}
	hideOverlay();
}

/* FUNCTIONS FOR PHOTO UPLOADER */
function showPhotoLayer() {
	document.getElementById('photoDiv').style.display='block';	
	document.getElementById('photoMsg').style.display='none';	
	document.getElementById('photoCloser').style.display='none';	
	if (document.getElementById('choosePhotos')) {
		centerDiv("choosePhotos", 20, 540);	
	}
}
function hidePhotoLayer() {
	if (document.getElementById('choosePhotos')) {
		document.getElementById('choosePhotos').style.display = "none";
	}
	hideOverlay();
}
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(targetDiv){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;

		//IF TARGET DIV IS TALLER THAN DOCUMENT - ADDED BY JOHN 8.29.09
		if (document.getElementById(targetDiv)) {
			
			//GET HEIGHT OF POP UP + Y POSITION OF POP UP
			var divHeight = parseInt(document.getElementById(targetDiv).offsetHeight) + parseInt(document.getElementById(targetDiv).style.top);
			
			//IF BIGGER, USE THAT
			if (divHeight > yScroll) {
				yScroll = divHeight;
			}
		}

	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function showOverlay(targetDiv) {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var arrayPageSize = getPageSize(targetDiv);
	var arrayPageScroll = getPageScroll();
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}
function hideOverlay() {
	// get objects
	//alert("hiding ... ");
	if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
		var objOverlay = document.getElementById('overlay');
		// hide lightbox and overlay
		objOverlay.style.display = 'none';
	}
}
function initOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {
										//hideOverlay();
										//alert("click");
										//close_adding_layer();
										//ToggleFloatingLayer('FloatingLayer', 0, '');
										//close_div();
										//close_reg();
										//reset_account_layer();
										hidePhotoLayer();
										hide_pop();
										if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
										return false;
										}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '5000';
 	objOverlay.style.width = '100%';
 	objOverlay.style.height = '100%';
	objOverlay.style.backgroundColor = '#000000';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}
 
 function centerDiv(targetDiv, topMargin, divWidth) {

	//GET SCREEN SIZE
	var intH = 0;
	var intW = 0;

	if (self.innerHeight) {
	   intH = window.innerHeight;
	   intW = window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			intH = document.documentElement.clientHeight;
			intW = document.documentElement.clientWidth;
		} else {
			if (document.body) {
				intH = document.body.clientHeight;
				intW = document.body.clientWidth;
			}
		}
	}
	
	//GET SCROLL POSITION FOR POP-UP Y-POSITION
	var spos = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	
	var ypos = parseInt(spos + parseInt(topMargin), 10); //INCLUDES TOP MARGIN
	var xpos = parseInt((intW - parseInt(divWidth)) / 2, 10); //INCLUDES WIDTH OF DIV

	document.getElementById(targetDiv).style.left = xpos+"px";
	document.getElementById(targetDiv).style.top = ypos+"px";

	//SHOW TARGET DIV
	document.getElementById(targetDiv).style.display = "block";

	//SHOW OVERLAY
	showOverlay(targetDiv);		
 
 }

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", fnc, false );
  }
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}