// JavaScript Document
//this is for the navigation
startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
window.onload=startList;

//this is for the AAG Agri-Index subscribe page
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function verifyForm(pForm) {
	
		if (pForm.Name.value == "") {
			alert("Please enter your name");
			pForm.Name.focus();
			return false;
		}
        if (pForm.Surname.value == "") {
			alert("Please enter your surname");
			return false;
		}
		if (!validEmail(pForm.Email.value)) {
			alert("Please enter a valid email address");
			pForm.Email.focus;
			pForm.Email.select();
			return false;
		}
		
		if (pForm.Phone_Number.value == "") {
			alert("Please enter your contact phone number");
			pForm.Phone_Number.focus();
			return false;
		}
		
		document.contactForm.Submit.disabled = true;
		return true;
	}
	
	function validEmail(email) {
	 invalidChars = "/;,:"
	 
		if (email == "") {
		 	return false
		}
		
		for(i=0;i<invalidChars.length;i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0)>-1) {
				return false;
			}
		}
		
		atPos = email.indexOf("@",1);
		if (atPos == -1) {
			return false;
		}
		
		if (email.indexOf("@", atPos+1)>-1)
		{
			return false;
		}
		
		periodPos = email.indexOf(".", atPos);
		if(periodPos == -1) {
			return false;
		}
		
		if (periodPos+3 > email.length) {
			return false
		}
		return true;
	}
	
	
	
	
// this is for the new user form
function checkRegForm( formObj)
{
    /* check title */
    var title_chosen=0;
    var warnings="";
    for( i=0;i<formObj.title.length;i++)
    { if( formObj.title[i].checked){title_chosen=1;} }
    if( title_chosen==0) { warnings+="- Title\n"; }
    /* check text fields */
    if( formObj.fname.value.length<2) { warnings+="- First Name\n"; }
    if( formObj.lname.value.length<2) { warnings+="- Last Name\n"; }
    if( !checkEmail(formObj.email.value)) { warnings+="- Email\n"; }
    if( formObj.jobtitle.value.length<3) { warnings+="- Job Title\n"; }
    if( formObj.company.value.length<3) { warnings+="- Company Name\n"; }
    if( formObj.streetaddress.value.length<5) { warnings+="- Street Address\n"; }
    if( formObj.suburb.value.length<2) { warnings+="- Suburb\n"; }
    if( formObj.postcode.value.length<3) { warnings+="- Postcode\n"; }
    if( formObj.phone.value.length<6) { warnings+="- Phone\n"; }
    if( formObj.fax.value.length<6) { warnings+="- Fax\n"; }
    if( warnings.length>3)
    {   alert(  "Please make sure the following fields\n"+
                "are entered correctly:\n \n"+warnings );
        return false;
    }
    return confirm("Are you sure all fields entered are valid and accurate?");
}










// mis.js
function newPW( url )
{
	var newpasswd = prompt( "Please enter a new password.\nNote: alphanumeric characters only, (a~z, A~Z, 0~9)", "" ); 
	if ( newpasswd.length < 2 )
	{
		alert( "The password you have entered is too short, please make it at least 2 characters long." );
		return false;
	}
	else
	{
		window.location = url + '&np=' + newpasswd;
		return true;
	}
}

function confirmDel ( url )
{
	if ( confirm("Are you sure you want to DELETE this user?\nNOTE: This action is not reversible.") )
	{
		window.location = url;
		return true;
	}
}

function confirmAction( url, msg)
{
    if( msg == "" || msg == null) { msg = "Are you sure?"; }
    if( confirm( msg ))
    {
        window.location = url
    }
}

function gotoPage ( objSelect )
{
  n = objSelect.selectedIndex;
  //baseurl = 'http://www.ausagrigroup.com.au/mis/';
  baseurl = '';
  url = baseurl + objSelect.options[n].value;
  if ( objSelect.options[n].value.length >= 3 ) { window.location = url; }
}

// popups
function popupWin( url, winame, wintype )
{
  if ( wintype == null ) { extras = '' }
  else if ( wintype == 'basic' ) { extras = 'scrollbars,resizable,width=450,height=450,left=20,top=20' }
  else if ( wintype == 'mis' ) { extras = 'scrollbars,resizable,width=650,height=450,left=30,top=20,status' }
  else if ( wintype == 'full' ) { extras = 'menubar,toolbar,location,status,scrollbars,resizable,width=780,height=450,left=20,top=20' }

  aagPop = window.open(url, winame, extras)
  aagPop.focus();
}

function checkEmail(email_address)
{
  var reEmail = /^[a-zA-Z0-9\-_]+\.*[a-zA-Z0-9\._\-]*\@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\.\-]+[a-zA-Z0-9_]+$/
    if ( parseInt(navigator.appVersion) >= 4 )
    { // JavaScript 1.3 REGEXP Email Validation
        if(!reEmail.test(email_address))
        { return false; }
        return true;
    }
    else
    {
        if ( email_address.indexOf(" ") >= 0 ||
             email_address.indexOf("@") == -1 ||
             email_address.indexOf(".") == -1 ||
             email_address.length <= 5 )
        { return false; }
        return true;
    }
}
