//constants for javascript
var sAdminURL = "/ghad/"
var sURL = "http://www.glasshouse.co.uk"
var sSitetitle = "Glasshouse"
var  sAdmintitle = "Glasshouse Admin System"

//script to return itemID
function goToItem()
{
var oForm,sItem,sName,sURL;
oForm  = document.forms["pageinfo"];
sItem = oForm.itemid.value;
sName = oForm.itemname.value
sURL = sAdminURL + "levela.asp?itemid= " + sItem +"&itemname="+sName ;
//alert(sURL)
location.href = sURL
}

//script to return itemID
function goToCountryPage()
{
var oForm,sItem,sCat,sURL;
oForm  = document.forms["pageinfo"];
sItem = oForm.itemid.value;
sCat = oForm.catid.value
sURL = sAdminURL + "countrysplit.asp?itemid= " + sItem +"&catid="+sCat ;
//alert(sURL)
location.href = sURL
}

//choose categories by country
//assumes that category will be wine as this is the only section split by country
function editCountryItems(itemID,catID,countryID,countryName)
{
	var oForm,sURL;
	oForm  = document.forms["pageinfo"];
	oForm.itemid.value = itemID;
	oForm.catid.value = catID;
	oForm.countryregionid.value = countryID;
	oForm.countryname.value = countryName;
	oForm.action = "wineitemlist.asp"
	oForm.submit();
}

//submit form to display category items
function editCategoryItems(itemid,catid,catname)
{
var oForm,sURL;
oForm  = document.forms["pageinfo"];
oForm.itemid.value = itemid;
oForm.catid.value = catid;
oForm.catname.value = catname;

 if (itemid == 1)
 {
 oForm.action = "menuitemlist.asp"
 }
 if (itemid == 2) 
 {
	/*
	If categories are RED or White then we need to split
	the wines up by country as well tf redirect to another page
	where the user can choose which country category they wish to edit
	*/
	switch (catid)
	{
		case 6 :
		case 7 :
			oForm.action = "countrysplit.asp";
		break;
		default :
			oForm.action = "wineitemlist.asp"

	}
		
 
 }
 if (itemid == 3) 
 {
 oForm.action = "infoitemlist.asp"
 }
 
	

oForm.submit();
}

function updateOrder(itemid,catname)
{
var oForm,oPosForm,iPos;
oForm  = document.forms["pageinfo"];
oPosForm = document.forms["itemlisting"];
iPos = eval("oPosForm.pos_"+itemid+".selectedIndex");
oForm.thisitem.value = itemid;
oForm.thisitemposition.value = iPos;
oForm.directive.value = "updateposition";
	switch (catname)
	{
		case "wine" :
			oForm.action = "wineitemlist.asp"
		break;
		case "menu" :
			oForm.action = "menuitemlist.asp"
		break;
		case "info" :
			oForm.action = "infoitemlist.asp"
		break;
	}
if (iPos == 0)
{
alert("Please select a position");
return false;
}
else
{ oForm.submit();}
}

function changeDirective(strDirective,strSection,ID)
{

var oForm;
oForm  = document.forms["pageinfo"];
oForm.directive.value =  strDirective;
oForm.thisitem.value = ID;


	switch (strDirective)
	{
		case "add" :
			switch (strSection)
			{
				case "wine" :
					if (eval_wineitem())
					{
						oForm.action = "wineitemlist.asp"
						oForm.submit();
					}
					
				break;
				case "menu" :
					if (eval_menuitem())
					{
						oForm.action = "menuitemlist.asp"
						oForm.submit();
					}
				break;
				case "info" :
					if (eval_infoitem())
					{
						oForm.action = "infoitemlist.asp"
						oForm.submit();
					}
				break;
			}
		break;
		case "edit" :
			switch (strSection)
			{
				case "wine" :
					oForm.submit();
				break;
				case "menu" :
					oForm.submit();
				break;
				case "info" :
					oForm.submit();
				break;
			}
		break;
		case "update" :
			switch (strSection)
			{
				case "wine" :
					if (eval_wineitem())
					{
					oPosForm = document.forms["itemlisting"];
					oForm.thisitemposition.value = oPosForm.position.value;
					oForm.submit();
					}
				break;
				case "menu" :
					if (eval_menuitem())
					{
					oPosForm = document.forms["itemlisting"];
					oForm.thisitemposition.value = oPosForm.position.value;
					oForm.submit();
					}
				break;
				case "info" :
					if (eval_infoitem())
					{
					oPosForm = document.forms["itemlisting"];
					oForm.thisitemposition.value = oPosForm.position.value;
					oForm.submit();
					}
				break;
			}
		break;
		case "delete" :
			switch (strSection)
			{
				case "wine" :
					if (confirm("Are you sure you want to delete this item ?"))
					{
					oForm.submit();
					}
					
				break;
				case "menu" :
					if (confirm("Are you sure you want to delete this item ?"))
					{
					oForm.submit();
					}
					
				break;
				case "info" :
					if (confirm("Are you sure you want to delete this item ?"))
					{
					oForm.submit();
					}
					
				break;
			}
		break;
	}
	
}


//hack function to pass id to hidden form
function getPos(id)
{
var oForm;
oForm  = document.forms["pageinfo"];
oForm.thisregioncountry.value = id;	
}

//validation function
//-----------------------------------------------------------------------------------------

function containsNumber(s)
{
	var bDigit;
	if (s.match(/\d+/))
	{
		bDigit = true;
	}
	else
	{
		bDigit = false;
	}
	return bDigit;
}

function isString(s)
{
	if (s.length > 0 )
	{
		return true;
	}
}


function contains(sLook,sFind)
{
	if (sLook.indexOf(sFind) > 0 )
	{
		return true;
	}
}

	
//---------------------------------------------------------------------------------------------------
// Non generic functions
function eval_wineitem()
{
//get all fields
//return true if all valid
var oForm,oSubmitForm;
var desc,regcty,vintage,price,sze,bABG,glassprice;
var bValid,strErr;
bValid = true;
strErr = "";
oForm  = document.forms["itemlisting"];
desc = oForm.description.value; 

	if  (oForm.regioncountry.type == "hidden")
	{
		regcty = oForm.regioncountry.value;
	}
	else
	{
		regcty = oForm.regioncountry.options[oForm.regioncountry.selectedIndex].value;
	}

vintage = oForm.vintage.value; 
price = oForm.price.value; 
sze = oForm.size.value;
bABG = oForm.bABG.checked;
glassprice = oForm.glassprice.value;

	if (!(isString(desc)))
	{
		bValid = false;
		strErr += "Please specify a description\n";
	}
	if (!(containsNumber(price)))
	{
		bValid = false;
		strErr += "Please specify a price\n";
	}
	if (oForm.bABG.checked)
	{
		if (!(containsNumber(glassprice)))
		{
			bValid = false;
			strErr += "As wine is available by glass,\nA price by the glass must be specified.\n";
		}
	}
	if (bValid)
	{
		oSubmitForm = document.forms["pageinfo"];
		oSubmitForm.thisdescription.value = desc;
		oSubmitForm.thisregioncountry.value = regcty;
		oSubmitForm.thisvintage.value = vintage;
		oSubmitForm.thisprice.value = price;
		oSubmitForm.thissize.value = sze;
		oSubmitForm.thisbABG.value = bABG;
		oSubmitForm.thisglassprice.value = glassprice;
		return true;
		
	}
	else
	{
		alert(strErr);
	}
	
}

function eval_menuitem()
{
	var oForm,oSubmitForm,bValid;
	var strErr;
	var description,price;
	
	oForm  = document.forms["itemlisting"];
	oSubmitForm = document.forms["pageinfo"];
		if (oSubmitForm.catid.value != "29")
		{
			description = oForm.description.value;
		}
		else
		{
			header = oForm.header.value;
			line1 = oForm.line1.value;
			line2 = oForm.line2.value;
			line3 = oForm.line3.value;
			bold1 = oForm.bold1.value;
			bold2 = oForm.bold2.value;	
		}

	//price = oForm.price.value
	bValid = true;
	strErr = "";
	
	/*if (!(containsNumber(price)))
	{
		strErr += "Please fill in a valid price.\n";
		bValid = false; 
	}*/
	
	if (oSubmitForm.catid.value != "29")
	{
		if (!(isString(description)))
		{
			strErr += "Please fill in a description.\n";
			bValid = false;
		}
	}
	
	if (bValid)
	{
		if (oSubmitForm.catid.value != "29")
		{
			oSubmitForm.thisdescription.value = description;
			//oSubmitForm.thisprice.value = price;
			return true;
		}
		else
		{
			
			oSubmitForm.thisheader.value = header;
			oSubmitForm.thisline1.value = line1;
			oSubmitForm.thisline2.value = line2;
			oSubmitForm.thisline3.value = line3;
			oSubmitForm.thisbold1.value = bold1;
			oSubmitForm.thisbold2.value = bold2;	
			return true;
		}
	}
	else
	{
		alert(strErr);
	}
}

function eval_infoitem()
{
	var oForm,oSubmitForm,bValid;
	var strErr;
	var description,title,startdate,enddate;
	bValid = true;
	oForm  = document.forms["itemlisting"];
	oSubmitForm = document.forms["pageinfo"];
	title = oForm.title.value;
	description = oForm.description.value;
	startdate = oForm.startdate.value;
	enddate = oForm.enddate.value;
	strErr = "";
	
	if (!(isString(description)))
	{
		strErr += "Please fill in a description .\n";
		bValid = false;
	}
	
	if (bValid)
	{
		oSubmitForm.thistitle.value = title;
		oSubmitForm.thisdescription.value = description;
		oSubmitForm.thisstartdate.value = startdate;
		oSubmitForm.thisenddate.value = enddate;
		
		return true;
	
	}
	else
	{
		alert(strErr);
	}
}

function subWineSearch()
{
var oForm;
oForm = document.forms["searchwine"];
oForm.submit();
}

function openPreView(itemid)
{
	if (itemid == 1) 
	{
		openWin("cbad/word.asp?itemid=1","MENU",600,800);
	}
	if (itemid == 2) 
	{
		openWin("cbad/word.asp?itemid=2","WINELIST",800,800);
		//window.focus();
	}
	else
	{
	alert("Preview not yet available for this section.")
	}
}

/*
uses convention
-img name imgname
-over state [imgname_over]
*/
function roll(imgName,blnState)
{
	var imgSrc;

	if (blnState == "1")
    {
    imgSrc = imgName + "_over";
    }
    else
    {
    imgSrc = imgName;
    }
	document.images[imgName].src = "/cb/images/" + imgSrc + ".gif";
}


function openWin(sSrc,sName,sWidth,sHeight)
{
window.open(sSrc,sName,"scrollbars=yes,width="+sWidth+",height="+sHeight+",statusbar=no,toolbar=no");
}
