// Global Define to verify browser type
var isIE = document.all?true:false;
var isNS = document.layers?true:false;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function launchPopWin(page) {
  PopWin=window.open
    (page,"popup","status=yes,toolbar=yes,location=yes,menubar=yes,scrollbars=yes,resizable=yes,width=640,height=480");
  PopWin.focus();
}

function MM_validateForm() { //v4.0
	var i, p, q, nm, test, num, min, max, errors = '', args = MM_validateForm.arguments;

	for (i=0; i < (args.length - 2); i += 3) { 
		test = args[i + 2]; 
		val = MM_findObj(args[i]);
	
		if (val) { 
			nm = val.name; 
			valData = val.value;
			
			if (valData != "") {
				if (test.indexOf('isEmail') != -1) { 
					p = valData.indexOf('@');
					if (p < 1 || p == (valData.length - 1)) {
						errors += '- ' + nm + ' must contain an e-mail address.\n';
					}
				} else if (test == 'C') {
					// make sure the named checkbox is ticked
					if (!val.checked) {
						errors += '- ' + nm + ' must be checked.\n';
					}
				} else if (test != 'R') {
					if (isNaN(valData)) {
						errors += '- ' + nm + ' must contain a number.\n';
					}
				
					if (test.indexOf('inRange') != -1) { 
						p = test.indexOf(':');
						min = test.substring(8, p); 
						max = test.substring(p + 1);
						if (valData < min || max < valData) {
							errors += '- ' + nm + ' must contain a number between ' + min + ' and ' + max + '.\n';
						} 		
					}
				}
			} else if (test.charAt(0) == 'R') {
				errors += '- '+nm+' is required.\n'; 
			}
		} 
	}
	
	if (errors) {
		alert('The following error(s) occurred:\n' + errors);
	}
	
	document.MM_returnValue = (errors == '');
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function submitLogin()
{
	f = document.loginform
	f.submit();
}

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

function  usernames()
{
	fname = document.forms[0].first_name.value;
	lname = document.forms[0].last_name.value;
	s = document.forms[0].susername;

	// Check if the firstname does not have the special char or space
	newfname = "";	
	for (i=0; i<fname.length; i++)
	{

		key = fname.charCodeAt(i);
		chr = fname.substr(i,1);
		//window.status = key;
		if ((key>=46 && key <=57) || (key >=65 && key<=90) || (key >=97 && key<=122)) {
					newfname += chr;  
		}
	}
	fname = newfname;	

	// Check if the firstname does not have the special char or space
	newlname = "";	
	for (i=0; i<lname.length; i++)
	{

		key = lname.charCodeAt(i);
		chr = lname.substr(i,1);
		//window.status = key;
		if ((key>=46 && key <=57) || (key >=65 && key<=90) || (key >=97 && key<=122)) {
					newlname += chr;  
		}
	}
	lname = newlname;	


	// First Suggestion
	myName = fname + lname.substr(0,1);
	myName = myName.toLowerCase()
	myNewOption = new Option(myName,myName);
	s.options[1] = myNewOption;
		
	// 2nd Suggestion
	myName = fname.substr(0,1)+lname;
	myName = myName.toLowerCase()
	myNewOption = new Option(myName,myName);
	s.options[2] = myNewOption;
	
	// third suggestion
	myName = fname.substr(0,3)+lname.substr(0,5);
	myName = myName.toLowerCase()
	myNewOption = new Option(myName,myName);
	s.options[3] = myNewOption;

	// fourth suggestion
	myName = "U3A"+fname.substr(0,1)+lname.substr(0,6);
	myName = myName.toLowerCase()
	myNewOption = new Option(myName,myName);
	s.options[4] = myNewOption;

	// fifth suggestion
	myName=  fname.substr(0,3)+lname.substr(1,6)+"02";
	myName = myName.toLowerCase()
	myNewOption = new Option(myName,myName);
	s.options[5] = myNewOption;
}

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function submitForm(whichForm)
{
	f = document.forms[0];
	if (whichForm != '')
	{
		if (whichForm == "subscription")
		{
			MM_validateForm('first_name','','R','last_name','','R','address','','R','suburb_town','','R','country','','R','email','','RisEmail','username','','R', 'paymentOption', '', 'R', 'appAgree', '', 'C');
		}
		if (whichForm == "subscriptionU3A")
		{
			MM_validateForm('first_name','','R','last_name','','R','address','','R','suburb_town','','R','country','','R','email','','RisEmail','username','','R');
		}
		if (whichForm == "contactus")
		{
			MM_validateForm('name','','R','email','','RisEmail','subject','','R');
		}
		if (whichForm == "createSubscriber") {
			var numaric = document.createSubscriber.username.value;
			for(var j=0; j<numaric.length; j++) {
				var alphaa = numaric.charAt(j);
				var hh = alphaa.charCodeAt(0);
				if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123))	{
				}	else	{
				 	alert("Only letters and numbers are allowed in this field");
					return;
				}
			}
			MM_validateForm('first_name','','R','last_name','','R','address','','R','suburb_town','','R','country','','R','email','','RisEmail','username','','R');
		}
		if (whichForm == "memberDetails")
		{
			MM_validateForm('first_name','','R','last_name','','R','address','','R','suburb_town','','R','country','','R','email','','RisEmail');
		}
		if(document.MM_returnValue)
		{
			document.forms[0].submit();
		}
	}
	else
	{
		f.submit();
	}
}


function changeTitle(title)
{
	top.frames[2].document.frames[0].location="title.php?title="+title;
}

function changeFinaliseTitle(title)
{
	top.document.frames[0].location="title.php?title="+title;
}
function changeAdminTitle(title)
{
	//top.frames[2].document.frames[0].location="admin_title.php?title="+title;
}


function changeGuideTitle(title)
{
	top.frames[2].document.frames[0].location="../title.php?title="+title;
}


// From Here validate date function
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }

  return result;
}

function validRequired(formField,fieldLabel)
{
  var result = true;
  
  if (formField.value == "")
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  
  return result;
}

function validEmail(formField,fieldLabel,required)
{
  var result = true;
  
  if (required && !validRequired(formField,fieldLabel))
    result = false;

  if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    formField.focus();
    result = false;
  }
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
  var result = true;

  if (required && !validRequired(formField,fieldLabel))
    result = false;
  
   if (result)
   {
     var num = parseInt(formField.value,10);
     if (isNaN(num))
     {
       alert('Please enter a number for the "' + fieldLabel +'" field.');
      formField.focus();    
      result = false;
    }
  } 
  
  return result;
}

function validDate(formField,fieldLabel,required)
{
  var result = true;

  if (required && !validRequired(formField,fieldLabel))
    result = false;
  
   if (result)
   {
     var elems = formField.value.split("-");
     
     result = (elems.length == 3); // should be three components
     
     if (result)
     {
       var day = parseInt(elems[0]);
       var month = parseInt(elems[1],10);
       var year = parseInt(elems[2],10);
       result = !isNaN(month) && (month > 0) && (month < 13) &&
                !isNaN(day) && (day > 0) && (day < 32) &&
                !isNaN(year) && (elems[2].length == 4);
			
	   // valid the day of month is correct
	   if (result) {
		   result = (day <= validDays(month, year));
	   }
     }
     if (!result)
     {
       alert('Please enter a valid date in the format DD-MM-YYYY for the "' + fieldLabel +'" field.');
      formField.focus();    
    }
  } 
  
  return result;
}


function validDays(month, year)
{
	if (month == 2) { // if it is February 
		if (year % 4 == 0 ) { // it is leap year then 29 days in February 
			totaldays = 29;
		}
		else { // it is not leap Year then 28 days in February
			totaldays = 28; 
		}
	}
	else { // other months
		if (month == 4 || month == 6 || month == 9 || month == 11) { // 30 days in April, June, Sept, Nov
			totaldays = 30;
		}
		else { //  31 days in Jan, March, May, July, August, October, December
			totaldays = 31;
		}
	}
	
	return totaldays;
}


function onlyDigits(e) {
	var _ret = true;
	if (isIE) {
	if (window.event.keyCode < 46 || window.event.keyCode > 57) {
	window.event.keyCode = 0;
	_ret = false;
	}
	}
	if (isNS) {
	if (e.which < 46 || e.which > 57) {
	e.which = 0;
	_ret = false;
	}
	}
	return (_ret); 
}

function onlyCharsDigits(e) {
	var _ret = false;
	key = e.keyCode
	window.status = key;
	if ((key>=46 && key <=57) || (key >=65 && key<=90) || (key >=97 && key<=122)) {
		_ret = true;
	}	else {
		_ret = false;
		window.event.keyCode = 0;
		alert("Only letters and numbers are allowed in this field");
	}
	return (_ret); 
}
