// JavaScript Document

var searchresults

window.onload = init;

function init()
{
	
	
	
	//MENU
	
	var a = document.getElementsByTagName("DIV")
	
	for( i=0; i<a.length;i++ )
	{
		if( a[i].className )
		{
			if( a[i].className == 'nav2' )
			{
				a[i].onmouseover = function()
				{
					//alert("yes")
					if(document.getElementById('sub_'+this.id.split('_')[0]))
						document.getElementById('sub_'+this.id.split('_')[0]).style.display = 'block'
						//var thesub = document.getElementById('sub_'+this.id.split('_')[0])
						//opacity('thesub', 0, 100, 500)
				}
				
				a[i].onmouseout = function()
				{
						if( document.getElementById('sub_'+this.id.split('_')[0]))
							document.getElementById('sub_'+this.id.split('_')[0]).style.display = 'none'
							//var thesub2 = document.getElementById('sub_'+this.id.split('_')[0])
							//opacity('thesub2', 100, 0, 500)
				}
			}
			
			if(a[i].className == 'subtier' )
			{
				a[i].onmouseover = a[i].onfocus = function()
				{
					newid = this.id.split('_')[1]
					if( document.getElementById('tier_'+newid))
					thesub = document.getElementById('tier_'+newid)
					thesub.style.display = 'block'
				}
				
				a[i].onmouseout = a[i].onblur = function()
				{
					newid = this.id.split('_')[1]
					if( document.getElementById('tier_'+newid))
					thesub = document.getElementById('tier_'+newid)
					thesub.style.display = 'none'
				}
			}
		}
	}

}

function pullsmartSearch()
{
	if( xmlHttp.readyState == 4 )
	{
		searchresults = document.getElementById('ssresults');
		var seeall = document.getElementById('smart')
		var results = ''
		var matches = ''
		var sMatch = ''
		 matches = xmlHttp.responseText.split('*|*');
		 
		 results += '<div>'
		for( i=0;i<(matches.length-1);i++ )
			{	
				sMatch = matches[i].split("||");
				results += '<a href="/?id='+sMatch[0]+'">'+sMatch[1]+'</a>'
			}
		results += '<a class="all" href="smartSearch.asp?ssphrase='+seeall.value+'">See all results</a><br clear=all>'
		results += '</div>'
		searchresults.innerHTML = results
		
		if( results.value != '' )
		{
			delaymenu = setTimeout("showMenu()",2000);
		}
	}
}

function sendsmartSearch(searchphrase)
{
	checkAJAX();
	//alert(searchphrase)
	var url = "/Scripts/ajax/smartsearch.ajax.asp";
	url += "?smart="+searchphrase;
	xmlHttp.onreadystatechange = pullsmartSearch;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showMenu()
{
	searchresults = document.getElementById('ssresults');
	searchresults.style.display = 'block'
}

function hideMenu()
{
	var hide = document.getElementById('smart')
	searchresults = document.getElementById('ssresults');
	
	searchresults.style.display = 'none'
	hide.value = ''
			
}

// find Y position
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}


function validate(form,findclass)
{
	var tag=document.body.getElementsByTagName('*');
	var errormsg = document.getElementById('errormsg')
	var txt = ''
	var errorString = "";
	var id = "";
	for(var i=0; i<tag.length; i++) {
		
		switch(tag[i].className){
			
			case "req text":
				errorString += validateInput(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req select":
				errorString += validateInput(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req check":
				errorString += validateCheck(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req checkgroup":
				if(id != tag[i].id){
					id = tag[i].id;
					errorString += validateCheckGroup(form[id],form[tag[i].id + "_err"].value);
				}
				break;
			case "req date":
				errorString += validateDate(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req email":
				errorString += validateEmail(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req phone":
				errorString += validatePhone(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req soc":
				errorString += validateSocial(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "req zip":
				errorString += validateZip(tag[i],form[tag[i].id + "_err"].value);
				break;
			case "":
				break;
			
		}
		
	}
	if(errorString!=""){
		alert(errorString);
		return false;
	}
	return true;
	//sendForm()
}

function viewResults(drop){
	
	var current = getByClass("recipetextbox visible");
	
	if(current.id == "recipetextbox_" + drop.value){
		return false;
	}else{
		current.className = "recipetextbox";
	}
	
	
	document.getElementById("recipetextbox_" + drop.value).className = "recipetextbox visible";	
}

function viewResultsNext(){
	
	var current = getByClass("recipetextbox visible");
	var curArray = current.id.split("_");
	var currentNum = parseInt(curArray[1]);
	currentNum += 1;
	
	var next = document.getElementById("recipetextbox_" + currentNum);
	if(next){
		current.className = "recipetextbox";
		next.className = "recipetextbox visible";
		document.getElementById("selectedNav").selectedIndex = currentNum - 1;
	}else{
		return false;
	}
	
}

function viewResultsBack(){
	
	var current = getByClass("recipetextbox visible");
	var curArray = current.id.split("_");
	var currentNum = parseInt(curArray[1]);
	currentNum -= 1;
	
	var next = document.getElementById("recipetextbox_" + currentNum);
	if(next){
		current.className = "recipetextbox";
		next.className = "recipetextbox visible";
		document.getElementById("selectedNav").selectedIndex = currentNum - 1;
	}else{
		return false;
	}
	
}

function getByClass(theClass){
	
	var allDivTags = new Array();
	var allDivTags=document.getElementsByTagName("div");
	
	for (i=0; i<allDivTags.length; i++) {
		if(allDivTags[i].className == theClass){
			return allDivTags[i];
		}
	}	
}

function printRecipe(id){
	
	window.open("/Scripts/Admin/PopUps/printRecipe.asp?recipe=" + id,"Print Recipe","width=635,height=300,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes");	
	
}

function SendToDownload(download)
{
	window.open("/pdf/downloads/"+download,"Print Recipe","width=635,height=300,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes");
	//window.open.location.assign("/pdf/"+download);
}