function ValidateFields(array)
{
	for(i=0; i < array.length; i+=2)
	{
		if(!array[i].value || array[i].value.length == 0)
		{
			alert(array[i+1]);
            array[i].focus();
			return false;
		}
	}
	return true;
}

function ValidateEmailWithError(emailInput)
{
    if(!echeck(emailInput.value))
    {
        alert('Please enter a valid email address.');
        emailInput.focus();
        return false;
    }
    return true;
}
    
function echeck(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false
	}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

	return true					
}

isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;

function ValidTopic(theForm) {
	if (theForm.q2)			// search form
	{
		if (theForm.q2.selectedIndex != 0)
		{
			if (theForm.q2.options[theForm.q2.selectedIndex].value == "")
			{
				alert("Please select one of the \"Topic\" options, not a category heading.");
				theForm.q2.focus();
				return(false);
			}
			else
			{
				theForm.topic.value = theForm.q2.options[theForm.q2.selectedIndex].value;
				return(true);
			}
		}
		else
		{
			theForm.q2.value = "";
			return(true);
		}
	}
	else
	{
		if (theForm.Topic.selectedIndex <= 0 || theForm.Topic.options[theForm.Topic.selectedIndex].value == "") {
		    alert("Please select one of the \"Topic\" options, not a category heading.");
		    theForm.Topic.focus();
		    return(false);
		}
		window.location.href = theForm.Topic.value;
		return(true);
	}
}

function CFpopUp(sURL)
{
	window.open("/corporatefirst/popup.asp?pageURL=" + sURL, "LMpopup", "top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function LMpopUp(sURL)
{
	window.open("/popup.asp?pageURL=" + sURL, "LMpopup", "top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}
function LMnewPopUp(sURL)
{
	window.open("/popup.asp?pageURL=" + sURL, "", "top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function LMnewLargePopUp(sURL)
{
	window.open("/popup.asp?pageURL=" + sURL, "", "top=20,left=20,width=640,height=600,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function LMlargepopUp(sURL)
{
	window.open("/popup.asp?pageURL=" + sURL, "LMpopup", "top=20,left=20,width=640,height=600,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function QLpopUp(sURL)
{
	window.open("/popup.asp?pageURL=" + sURL, "LMpopup", "top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function openPopUp(sURL, width, height)
{
	window.open("/popup.asp?pageURL=" + sURL, "Popup", "top=20,left=20,width=" + width + ",height=" + height + ",toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function openNewPopUp(sURL, width, height)
{
	window.open("/popup.asp?pageURL=" + sURL, "NewPopup", "top=20,left=20,width=" + width + ",height=" + height + ",toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes",true);
}

function setSKU(frm)
{
	b_checked = false;

	for (i = 0; i < frm.pf_id.length; i++)
	{
		if (frm.pf_id[i].checked)
		{
			b_checked = true;
			frm.dept_id.value = frm.pf_id[i].value.substr(0,4);
			frm.sku.value = frm.pf_id[i].value + "-" + frm.state.value;
		}
	}

	if (!b_checked)
	{
		alert("Please select a product before continuing.");
	}

	return b_checked	
}

function setSKUproduct(frm)
{
	b_checked = false;

	if (frm.pf_id.value.toString() != "")
	{
		b_checked = true;
		frm.sku.value = frm.pf_id.value + "-" + frm.state.value;
	}

	if (!b_checked)
	{
		alert("Please select a product before continuing.");
	}

	return b_checked	
}

function set_pf_id(frm)
{
	b_checked = false;

	for (i = 0; i < frm.sku.length; i++)
	{
		if (frm.sku[i].checked)
		{
			b_checked = true;
			frm.pf_id.value = frm.sku[i].value.substr(0,7);
		}
	}

	if (!b_checked)
	{
		alert("Please select a product before continuing.");
	}

	return b_checked	
}

function PrintIt()
{
	factory.printing.header = "Legalmart: Your Instant Lawyers Online";
	factory.printing.footer = "(c)<% =Year(Date) %> Legalmart. All Rights Reserved.&b&bPage &p of &P";
	factory.printing.Portrait = true;
	factory.printing.printBackground = true;
	factory.printing.Print(true);
}

function setFocus(question)
{
	if(question.length) question = question[0]
	question.focus();
}

function checkRequired(theform)
{
	if(!document.all) return true;
	
	var i, question, visibleQuestion;
	var reqInfo = document.all('ReqInfo');
	
	if(!reqInfo) return true;

	if(reqInfo.length)
	{
		for(i=0;i<reqInfo.length;i++)
			if(!checkQuestion(reqInfo[i]))
				return false;
	}
	else if(!checkQuestion(reqInfo)) return false;
			
	return true;
}

function checkQuestion(Item)
{
	var question = document.all('Req_' + Item.className);
	var visibleQuestion = document.all(Item.className);

	if(!isAnswered(question))
	{
		showObject(Item);
		setFocus(visibleQuestion);
		return false;
	}
	else hideObject(Item);
	return true;
}

function isAnswered(question)
{
	if(question.length) question = question[0];
	return (question.value.length!=0);
}

function checkHiddenField(txt, isChecked)
{
	if (isChecked)
		txt.value='Yes';
	else
		txt.value='No';
}

function checkPostCode(frm)
{
	if (!isNaN(frm.postcode.value) && frm.postcode.value > 0 && frm.postcode.value <= 9999)
		return true;
		
	alert('You have entered an invalid postcode. Please try again.');
	frm.postcode.value = '';
	frm.postcode.focus();
	return false;
}

function toMoney(amount)
{	
	var sAmount = amount.toString()
	var iDotPos = sAmount.lastIndexOf('.');

	if(iDotPos == -1)
		sAmount += '.00';
	else
	{
		sAmount = sAmount.substr(0, iDotPos + 3);
		for(i=sAmount.length; i < iDotPos + 3; i++)
			sAmount += '0';
	}

	return '$' + sAmount;
}

function trimLeadingSpaces(str)
{
	var i;
	var c;
	
	for(i=0;i<str.length; i++)
	{
		c = str.charAt(i);
		
		if(c != ' ')
			break;
	}
	if(i < str.length)
		str = str.substring(i, str.length)
	
	return str;
}

function isNS()
{
	if(navigator.appName == 'Netscape') return true;
	return false	
}

function isIE()
{
	if(navigator.appName == 'Netscape') return false;
	return true
}

function showSectionNS6(sections, sectionName)
{
	var objSections = document.getElementsByTagName('div');
	
	for(var i=0; i < objSections.length; i++)
		if(objSections[i].id == sections)
			testSection(objSections[i], sectionName);
}

function showSectionNS4(sections, sectionName)
{
	var objSections = document.layers(sections);
	
	for(var i=0; i < objSections.length; i++)
		testSection(objSections[i], sectionName);
}

function showSection(sections, sectionName)
{	
	var objSections = document.all[sections];

	if(!objSections) return false;

	if(!objSections.length)
		return testSection(objSections, sectionName);
	
	for(i=0; i < objSections.length; i++) 
		testSection(objSections[i], sectionName);
		
	return true;
}

function testSection(section, value)
{
	if(section.className == value) //If it matches then show it
		showObject(section);
	else if(section.className == value + "_NOT") //If it matches a negative then hide it
		hideObject(section);
	else if(section.className.indexOf("_NOT") >= 0) //If it finds a negative non match then show it
		showObject(section);
	else
		hideObject(section);
	return true;
}

function radioButtonClick(obj, qNum, section, sectionName, required)
{
	if(required)
		if(isNS())
			document.wizard['Req_Q' + qNum].value = obj.value;
		else
			wizard.all('Req_Q' + qNum).value=obj.value;

	if(section!='')
	{
		if(isNS6)
			showSectionNS6('Section_' + qNum, sectionName);
		else if(isNS4)
			showSectionNS4('Section_' + qNum, sectionName);
		else
			showSection('Section_' + qNum, sectionName);
	}
}

function showObject(object)
{
    if (document.getElementById && document.getElementById(object) != null)
        node = document.getElementById(object).style.visibility='visible';
    else if (document.layers && document.layers[object] != null)
        document.layers[object].visibility = 'visible';
    else if (document.all)
		object.style.display = '';
}

function hideObject(object)
{
    if (document.getElementById && document.getElementById(object) != null)
        node = document.getElementById(object).style.visibility='hidden';
    else if (document.layers && document.layers[object] != null)
        document.layers[object].visibility = 'hidden';
    else if (document.all)
		object.style.display = 'none';
}

function propagateControl(ctl)
{
	var n, obj

	if(document.getElementsByName)
		obj = document.getElementsByName(ctl.name);
	else if(document.all)
		obj = document.all(ctl.name);

	if(obj && obj.length && obj.length > 1)
		for(n=0;n<obj.length;n++)
			obj[n].value = ctl.value;
}

function openChat()
{
	window.open('/chat/logon.asp','Chat','top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no',false);
}

function openQuicklawChat()
{
	window.open('http://www.legalmart.com.au/quicklaw/chat/logon.asp','Chat','top=20,left=20,width=500,height=500,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no',false);
}

function isEmail(email)
{
	if(!email.search) return (email.indexOf('@')>=0);
	if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}
