﻿//////////////////////////////////////////////////////////////////////////////////
// Global Vars
var report = true;
var btnDeleteClick = false;
var btnDeleteAllClick = false;
var btnLogonClick = false;
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// Directly invoked functions
//////////////////////////////////////////////////////////////////////////////////

function checkLogonForm(pContainer)
{
    if (!checkEmail(getFormField(pContainer, "txtEmail")))
    {
        alert('You need to enter your Email Address.');
        focus(pContainer, "txtEmail");
        return (false);
    }

    if (btnLogonClick)
    {
        btnLogonClick = false;

        if (getFormField(pContainer, "txtPassword").length == 0)
        {
            alert('You need to enter your Password');
            focus(pContainer, "txtPassword");
            return (false);
        }
    }
    
    return (true);
}

function checkDeleteCategory(pContainer,pName)
{
    if (btnDeleteClick)
    {
        btnDeleteClick = false;
        return(checkSure("Are you sure you want to DELETE this " + pName + " ?"));
    }
    
    return(true);
}

function checkDeleteProduct(pContainer, pName1, pName2)
{
    if (btnDeleteClick)
    {
        btnDeleteClick = false;
        return (checkSure("Are you sure you want to DELETE this " + pName1 + " ?"));
    }
    else if (btnDeleteAllClick)
    {
        btnDeleteAllClick = false;
        return (checkSure("Are you sure you want to DELETE ALL the " + pName2 + " ?"));
    }

    return (true);
}

function checkDeleteItem(pContainer, pName)
{
    if (btnDeleteClick)
    {
        btnDeleteClick = false;
        return (checkSure("Are you sure you want to DELETE this " + pName + " ?"));
    }

    return (true);
}

function checkDeleteDirect(pName)
{
    return (checkSure("Are you sure you want to DELETE this " + pName + " ?"));
}

/////////////////////////////////////////////////////////

function checkRegistrationForm(pContainer)
{
    if (!checkEmail(getFormField(pContainer,"txtEmail")))
    {
        alert('You need to enter your Email Address.');
        focus(pContainer,"txtEmail");
        return(false);
    }
    
    if (getFormField(pContainer,"txtEmail") != getFormField(pContainer,"txtRetypeEmail"))
    {
        alert('You need to confirm your Email Address');
        focus(pContainer,"txtRetypeEmail");
        return(false);
    }
    
    if (getFormField(pContainer,"txtPassword").length == 0)
    {
        alert('You need to enter your Password');
        focus(pContainer,"txtPassword");
        return(false);
    }
    
    if (getFormField(pContainer,"txtPassword").length <=4)
    {
        alert('You need to enter a longer Password');
        focus(pContainer,"txtPassword");
        return(false);
    }
    
    if (getFormField(pContainer,"txtPassword") != getFormField(pContainer,"txtRetypePassword"))
    {
        alert('You need to confirm your Password');
        focus(pContainer,"txtRetypePassword");
        return(false);
    }
    
    if (!checkDecimal(getFormField(pContainer,"txtDistance")))
    {
        alert('You need to enter a number in the Max Distance field');
        focus(pContainer,"txtDistance");
        return(false);
    }
    
    return(true);
}

function checkDelete(pName)
{
    if (btnDeleteClick)
    {
        btnDeleteClick = false;
        return(checkSure("Are you sure you want to DELETE this " + pName + " ?"));
    }
    
    return(true);
}

function checkMyPOIEntryForm(pContainer)
{
    if (btnDeleteClick)
    {
        btnDeleteClick = false;
        return(checkSure("Are you sure you want to DELETE this POI ENTRY ?"));
    }
    else if (getFormField(pContainer,"txtName") == "")
    {
        alert('You need to enter a POI name');
        focus(pContainer,"txtName");
        return(false);
    }
    
    return(true);
}

function highlight(pId)
{
    try
    {
        var x = document.getElementById(pId);
        
        x.className = "highlight";
    }
    catch (e)
    {
        reportError("highlight(pId)",e);
    }
}

function rollover(pObj,pImg)
{
    try
    {
        pObj.src = pImg;
    }
    catch (e)
    {
        reportError("rollover(pObj,pImg)",e);
    }
}

////////////////////////////////////////////////////////////////////////////////////////
// Utility functions
////////////////////////////////////////////////////////////////////////////////////////

function reportError(fn,e)
{
	
	if (report)
		alert ("Sorry - an error has occured...\n\n" + fn + ": >> " + e.description);
	
}

function checkEmail(pCheck)
{
    var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
    
    return(emailFilter.test(pCheck))
}

function getFormField(pContainer,pField)
{
    var returnValue = "";
    
    try
    {
        pContainer = pContainer.replace(/\$/g,"_");
        var thisField = document.getElementById(pContainer + "_" + pField);
        returnValue = trim(thisField.value);
    }
    catch (e)
    {
        reportError("getFormField(pContainer,pField)",e);
    }
    
    return(returnValue);
}

function getDropdownFormField(pContainer,pField)
{
    var returnValue = "";
    
    try
    {
        pContainer = pContainer.replace(/\$/g,"_");
        var thisField = document.getElementById(pContainer + "_" + pField);
        returnValue = trim(thisField[thisField.selectedIndex].value);
    }
    catch (e)
    {
        reportError("getDropdownFormField(pContainer,pField)",e);
    }
    
    return(returnValue);
}

function focus(pContainer,pField)
{
    try
    {
        pContainer = pContainer.replace(/\$/g,"_");
        var thisField = document.getElementById(pContainer + "_" + pField);
        thisField.focus();
    }
    catch (e)
    {
        reportError("focus(pContainer,pField)",e);
    }
}

function trim(what)
{
	try
	{
		if (what != "")
		{
			while(what.charAt(0)==' ')
				what = what.substring(1,what.length);
	
			while(what.charAt(what.length-1)==' ')
				what = what.substring(0,what.length-1);
		}
		
		return (what);
	}
	catch (e)
	{
		reportError("trim(what)",e);
	}
}

function checkDecimal(what)
{
	try
	{
		var valid = "0123456789.";
	
		what = trim(what);
	
		if (what == "")
		{
			return (false);
		}
	
		for (outer = 0; outer <= what.length-1; outer++)
		{
			if (valid.indexOf(""+what.charAt(outer)) == -1)
			{
				return (false);
			}
		}
		
		if (what.indexOf(".") != -1)
		{
			if (what.length < 2)
			{
				return (false)
			}
			
			if ("" + what.charAt(0).indexOf(".")!= -1 || "" + what.charAt(what.length-1).indexOf(".") != -1)
			{
				return (false);
			}
		}
	
		return (true);
	}
	catch (e)
	{
		reportError("checkDecimal(what)",e);
	}
}

function checkInteger(what)
{
	try
	{
		var valid = "0123456789";
	
		what = trim(what);
	
		if (what == "")
		{
			return (false);
		}
	
		for (outer = 0; outer <= what.length-1; outer++)
		{
			if (valid.indexOf(""+what.charAt(outer)) == -1)
			{
				return (false);
			}
		}
	
		return (true);
	}
	catch (e)
	{
		reportError("checkInteger(what)",e);
	}
}

function checkSure(msg)
{
	try
	{
		var ok;
	
		ok = confirm (msg);

		if (ok)
		{
			return (true);
		}
		else
		{
			return (false);
		}
	}
	catch (e)
	{
		reportError("checkSure()",e);
	}
}

function displayPage(url)
{
	try
	{
		document.location.href = url;
	}
	catch (e)
	{
		reportError("displayPage(url)",e);
	}
}

function displayIFrame(iFrame,url)
{
	try
	{
		window.parent[iFrame].document.location.href = url;
	}
	catch (e)
	{
		reportError("displayIFrame(iFrame,url)",e);
	}
}

function popUp(what,where,x,y)
{
	try
	{
	
		var param;
		
		param = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=" + x + ",height=" + y;

		window.open (what,where,param);
	}
	catch (e)
	{
		reportError("popUp(what,where,x,y)",e);
	}
}

function alertJump(msg,url)
{
    try
    {
        alert(msg);
        
        document.location.href = url;
    }
    catch (e)
    {
        reportError("alertJump(msg,url)",e);
    }
}




