// define a few variables that are required
var menu_popups = false;
var ignorequotechars = 0;

// lets define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// let's find out what DOM functions we can use
var DOMtype = '';
if (document.getElementById)
{
	DOMtype = "std";
}
else if (document.all)
{
	DOMtype = "ie4";
}
else if (document.layers)
{
	DOMtype = "ns4";
}

var MessageBoardObjs = new Array();

// function to emulate document.getElementById
function fetch_object(idname, forcefetch)
{
	if (forcefetch || typeof(MessageBoardObjs[idname]) == "undefined")
	{
		switch (DOMtype)
		{
			case "std":
			{
				MessageBoardObjs[idname] = document.getElementById(idname);
			}
			break;

			case "ie4":
			{
				MessageBoardObjs[idname] = document.all[idname];
			}
			break;

			case "ns4":
			{
				MessageBoardObjs[idname] = document.layers[idname];
			}
			break;
		}
	}
	return MessageBoardObjs[idname];
}

// function to handle the different event models of different browsers
// and prevent event bubbling
function do_an_e(eventobj)
{
	if (!eventobj || is_ie)
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		eventobj.stopPropagation();
		eventobj.preventDefault();
		return eventobj;
	}
}


// function to do a single-line conditional
function iif(condition, trueval, falseval)
{
	return condition ? trueval : falseval;
}

// function to check message length before form submission
function validatemessage(messageText, subjectText, minLength, maxLength, theForm)
{
	
 	// bypass Safari and Konqueror browsers with Javascript problems
	if (is_kon || is_saf || is_webtv)
	{
		return true;
	}

    if (theForm.state.value == "")
    {
      alert(help_phrase["NO_STATE"]);
      theForm.state.focus();
      return (false);
    }
   
    if (theForm.city.value == "")
    {
      alert(help_phrase["NO_CITY"]);
      theForm.city.focus();
      return (false);
    }
    
	if (theForm.Category.value == "")
    {
      alert(help_phrase["NO_CATEGORY"]);
      theForm.Category.focus();
      return (false);
    }
    
	if (theForm.SubCat.value == "")
    {
      alert(help_phrase["NO_SUBCATEGORY"]);
      theForm.SubCat.focus();
      return (false);
    }
	
	// check for completed subject
	if (subjectText.length < 1)
	{
		alert(help_phrase["NO_SUBJ"]);
		return false;
	}

	// attempt to get a code-stripped version of the text
	var strippedMessage = stripcode(messageText);
	
	// check for minimum message length
	if (strippedMessage.length < minLength)
	{
		alert(construct_phrase(help_phrase["MESG_SHORT"], minLength));
		return false;
	}
	// check for maximum message length
	if (strippedMessage.length > maxLength)
	{
		alert(help_phrase["MESG_LONG"]);
		return false;
	}

    if (theForm.contact_name.value == "")
    {
     alert(help_phrase["NO_NAME"]);
      theForm.contact_name.focus();
      return (false);
    }
   
    if (theForm.email.value == "")
    {
      alert(help_phrase["NO_EMAIL"]);
      theForm.email.focus();
      return (false);
    }
    
	var e = theForm.email.value;
    var i = 0;
    i = ((e.indexOf("@")) && (e.indexOf(".")));
    if (i<0)
    {
       alert(help_phrase["INVALID_EMAIL"]);
      theForm.email.focus();
      return (false);
    }
	
	var f = theForm.uploadedfile.value;
    if (f.indexOf("bmp") > -1) {
       alert(help_phrase["INVALID_IMAGE"]);
      theForm.uploadedfile.focus();
      return (false);
    }
	// everything seems okay
	return true;
}

// function to trim quotes and formatting_code tags
function stripcode(str)
{
	if (!is_regexp)
	{
		return str;
	}

	var html1 = new RegExp("<(\\w+)[^>]*>", "gi");
	var html2 = new RegExp("<\\/\\w+>", "gi");

	str = str.replace(html1, '');
	str = str.replace(html2, '');

	var html3 = new RegExp("&nbsp;");
	str = str.replace(html3, '');

	return str;
}


function construct_phrase()
{
	if (!arguments || arguments.length < 1 || !is_regexp)
	{
		return false;
	}

	var args = arguments;
	var str = args[0];

	for (var i = 1; i < args.length; i++)
	{
		re = new RegExp("%" + i + "\\$s", "gi");
		str = str.replace(re, args[i]);
	}
	return str;
}

function MessageBoard_init()
{
 	switch (DOMtype)
	{
		case "std": imgs = document.getElementsByTagName("img"); break;
		case "ie4": imgs = document.all.tags("img");             break;
		default:  imgs = false;                                break;
	}
      menu_popups = true;
   	return true;
}
