/*
 * Javascript functions for paging web sites
 */


/*
 * show or hide the specified element on the page
 */
function show(a)
{
	var args = arguments;
	var len_el = args.length;
  	for (var i = 0; i < len_el; i++)
  	{
  		document.getElementById(args[i]).style.clear = "both";
    	document.getElementById(args[i]).style.display = "inline";
  	}
	return true;
} 
	
function hide(a)
{
	var args = arguments;
	var len_el = args.length;
	for (i = 0;i < len_el;i++)
	{
		document.getElementById(args[i]).style.display = "none";
	}
	return true;
}	
	
/*
 * Highlight the specified field in yellow or set it back to white
 */

function err_hiliteOn(a)
{
	var args = arguments;
	var len_el = args.length;
	
	for (i = 0;i < len_el;i++)
	{
		document.getElementById(args[i]).style.backgroundColor = "#edba24";
	}
	return true;
}
	
function err_hiliteOff(a)
{
	var args = arguments;
	var len_el = args.length;
	
	for (i = 0;i < len_el;i++)
	{
		document.getElementById(args[i]).style.backgroundColor = "#ffffff";
	}
	return true;
}

/*
 * Allow a form to be submitted by pressing the enter key
 * use the following on any fields that this should be possible from
 * onKeyPress="return submitenter(this,event)"
 */

function submitenter(myfield,e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

	if (keycode == 13)
   	{
  		checkInitForm(document.regcheckform);
		return false;
	}
	else
	return true;
}

 
 /*
  * selectall(theForm)  -  Selects all checkboxes in the form specified
  * Usage: onClick(selectall(form)
  */
 function selectall(theForm)
{
	var formLength = theForm.length;	
						
	if(theForm.delAll.checked == true)
	{
		for (i=0;i<formLength;i++)
		{
			var type = theForm.elements[i].type;
			if (type=="checkbox")
			{
				theForm.elements[i].checked=true;
			}
		}
	}
	else
	{
		for(i=0;i<formLength;i++)
			{	
			var type = theForm.elements[i].type;
			if (type=="checkbox")
			{
				theForm.elements[i].checked=false;
			}
		}
	}
}


 /*
  * focusOn(form, element)  -  Set the focus to a specific form element
  * Usage: onClick(focusOn(form,element)
  */
function focusOn(form, element)
{
	form.element.focus();
}

 /*
  * isNumber(c) & isMultiDigitNumber  -  check for numerics etc
  */ 
function isNumber(c)
{
	if (c != '0' &&
		c != '1' &&
		c != '2' &&
		c != '3' &&
		c != '4' &&
		c != '5' &&
		c != '6' &&
		c != '7' &&
		c != '8' &&
		c != '9')
		return 0;
	return 1;
}
	
function isMultiDigitNumber (inNumber)
{
	for (i=0; i<inNumber.length; i++)
	{
		var thisChar = inNumber.substring (i,i+1);
		if (!isNumber(thisChar) )
		return 0;
	}
	return 1; 
}

/*
 * REGINIT.JSP specific javascripts
 * 
 * This whole section below contains the complete javascripts for reginit.jsp
 * lifted directly from the page
 * 
 * TODO: These can all be 'generalised' at somepoint
 * 
 */
 
   var warnings = [ 'Sorry, but we are not able to process your request, yet!',
                   'Pager number with email address already registered' ];

  function checkInitForm(theForm)
  {
    var pager = theForm.pagernum.value;
    var len;
    var firstEmail = theForm.email.value;
    var secondEmail = theForm.email_check.value;
    var failed = false;

    clearWarnings();

    while( firstEmail.length > 0 && firstEmail.charAt(firstEmail.length-1) == ' ' )
    {
      firstEmail = firstEmail.substring(0,firstEmail.length-1);
    }

    while( secondEmail.length > 0 && secondEmail.charAt(secondEmail.length-1) == ' ' )
    {
      secondEmail = secondEmail.substring(0,secondEmail.length-1);
    }

    if (pager == "" && firstEmail == "" && secondEmail == "")
    {
		show("err_form","err_pager","err_email");
		err_hiliteOn("pagernum","email","email_check");
      	return false;
    }

    len = pager.length;
    if ( len >= 7 )
    {
      var i = 0;
      if ( pager.charAt(0) == '+' )
      {
        i++;
      }
      while( i<len )
      {
        if ( !( pager.charAt(i) == ' ' || (pager.charAt(i) >= '0' && pager.charAt(i) <= '9') ) )
        {
          failed = true;
          i = len;
        }
        i++;
      }
    } else {
      failed = true;
    }

    if (failed)
    {
		//errMess = new Array("err_form","err_pager");
		show("err_form","err_pager");    	         	
      	err_hiliteOn("pagernum");
    }

    if ((firstEmail == "") || (firstEmail != secondEmail) || (echeck(firstEmail) == false))
    {
      //alert("Sorry, emails don't match");
		//errMess = new Array("err_form","err_email");
		show("err_form","err_email");
		hiliteFields = new Array("email","email_check");
		err_hiliteOn("email","email_check");
      	//err_hiliteOn("email");
      	//err_hiliteOn("email_check");
      failed = true;
    }

    if ( failed == false )
    {
      theForm.submit();
    }

    return failed;
  }

  function generalWarning(index)
  {
	show("err_form");
    document.getElementById("oops").innerHTML = warnings[index];

    pagerWarning();
    if ( index == 1 )
    {
      emailWarning();
    }
  }

  function pagerWarning()
  {
    show("err_pager");
    err_hiliteOn("pagernum");
  }

  function emailWarning()
  {
	show("err_email");
	err_hiliteOn("email","email_check");
  }

  function clearWarnings()
  {
    //clear existing colouring
	hide("err_form","err_pager","err_email","actualpagerno");
	err_hiliteOff("pagernum","email","email_check");

  }

  function clearFields()
  {
    document.regcheckform.pagernum.value = "";
    document.regcheckform.email.value = "";
    document.regcheckform.email_check.value = "";

    document.getElementById("1stpager").innerHTML = "Pager number";

    clearWarnings();
  }

  function echeck(str) {
    // Remove trailing spaces
    while ( str.length > 0 && str.charAt(str.length-1) == ' ' )
    {
      str = str.substring(0,str.length-1);
    }

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)

    if (str.indexOf(at)==-1 || str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr ||
        str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr ||
        str.indexOf(at,(lat+1))!=-1 ||
        str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot ||
        str.indexOf(dot,(lat+2))==-1 ||
        str.indexOf(" ")!=-1)
     {
       return false
     }

     return true
  }
  

// function showActualPagerField()
// {
//   document.getElementById("1stpager").innerHTML = "Group number";
//   document.getElementById("actualpagerno").style.display = "block";
//   document.getElementById("userId2").style.backgroundColor = "#edba24";
//   document.regcheckform.actualpagernum.focus();
// }
 
 function showInvalidPager()
 {
   document.getElementById("err_form").style.display = "block";
   document.getElementById("err_pager").style.display = "block";
   document.getElementById("pagernum").style.backgroundColor = "#edba24";
 }



/*
 * 
 * Javascript originally from regformA.jsp
 * TODO: Make this generic same as reginit.jsp javascript 
 * 
 */


  function checkFormA(theForm)
  {
    var failed = false;
    var isChecked = false;

    clearWarningsA();

    var id = theForm.securitycheck;
    for (i=0; i<id.length; i++)
    {
      if ( id[i].checked )
      {
        isChecked = true;
      }
    }

    if ( isChecked == false )
    {
      show("err_form","securityCheckWarning");
      failed = true;
    }

    if ( theForm.account.value == "" &&
         theForm.password.value == "" &&
         theForm.postcode.value == "" )
    {
      show("err_form","answerCheckWarning");
      err_hiliteOn("account","password","postcode");
      
      failed = true;
    }

    if ( theForm.authorised.checked == false )
    {
      show("err_form","authorisedCheckWarning");
      failed = true;
    }

    id = theForm.morereg;
    if ( !id[0].checked && !id[1].checked )
    {
      show("err_form","multiregCheckWarning");
      failed = true;
    }

    if ( failed == false )
    {
      document.forms["regcheckform"].submit();
    }

    return (false);
  }

  function clearFieldsA()
  {
	document.regcheckform.reset();

    clearWarningsA();
  }

  function clearWarningsA()
  {
    hide("err_form","answerCheckWarning","accountCheckWarning");

    if ( document.getElementById("securityCheckWarning") )
    {
      hide("securityCheckWarning");
    }
    if ( document.getElementById("passwordCheckWarning") )
    {
      hide("passwordCheckWarning");
    }
    if ( document.getElementById("postcodeCheckWarning") )
    {
      hide("postcodeCheckWarning");
    }
    if ( document.getElementById("authorisedCheckWarning") )
    {
      hide("authorisedCheckWarning");
    }
    err_hiliteOff("account");
    if ( document.getElementById("password") )
    {
      err_hiliteOff("password");
    }
    if ( document.getElementById("postcode") )
    {
      err_hiliteOff("postcode");
    }

    if ( document.getElementById("multiregCheckWarning") )
    {
      hide("multiregCheckWarning");
    }
    if ( document.getElementById("accountsingle") )
    {
      err_hiliteOff("accountsingle");
    }
	if ( document.getElementById("secInput") )
    {
      err_hiliteOff("secInput");
    }
  }

  function highlightAccount()
  {
  	show("err_form","accountCheckWarning");
    err_hiliteOn("secInput");
    document.forms["regcheckform"].secInput.focus();
    var radioGroup = document.forms["regcheckform"].secRad;
    setCheckedValue(radioGroup, "account");
  }

  function highlightPassword()
  {
    show("err_form","passwordCheckWarning");
    err_hiliteOn("secInput");
    document.forms["regcheckform"].secInput.focus();
	var radioGroup = document.forms["regcheckform"].secRad;
    setCheckedValue(radioGroup, "existpass");
  }

  function highlightPostcode()
  {
   	show("err_form","postcodeCheckWarning");
    err_hiliteOn("secInput");
    document.forms["regcheckform"].secInput.focus();
    var radioGroup = document.forms["regcheckform"].secRad;
    setCheckedValue(radioGroup, "postcode");
  }

  function highlightAccountSingle()
  {
	show("err_form","answerCheckWarning");
    err_hiliteOn("account");
    document.forms["regcheckform"].account.focus();
  }

  function fieldinput()
  {
    var theForm = document.forms["regcheckform"];
    if ( theForm.account.value != "" ||
         theForm.password.value != "" ||
         theForm.postcode.value != "" )
    {
      clearWarningsA();
    }
  }


/*
 * 
 * Javascript originally from regformB.jsp
 * TODO: Make this generic same as reginit.jsp & regformA.jsp javascript 
 * 
 */

  function checkFormB(theForm)
  {
    var id = theForm.title;
    var len = id.length;
    var index = -1;
    var failed = false;

    clearWarningsB();

    for (var i=0; i<len; i++)
    {
      if ( id[i].checked )
      {
        index = i;
      }
    }

    if ( index == -1 || theForm.firstname.value == "" || theForm.lastname.value == "" )
    {
      show("personalCheckWarning");
      err_hiliteOn("firstname","lastname");
      failed = true;
    }

    if (theForm.password.value == "" || theForm.password.value.length < 6 )
    {
      show("passwordCheckWarning");
      err_hiliteOn("password");
      theForm.password.focus();
      failed = true;
    }

    if ( theForm.password2.value != theForm.password.value )
    {
      show("password2CheckWarning");
      err_hiliteOn("password2");
      theForm.password2.focus();
      failed = true;
    }

    if ( theForm.memorable.selectedIndex == 0 )
    {
      show("memorableCheckWarning");
      theForm.memorable.focus();
      failed = true;
    }

    if ( theForm.memorable.selectedIndex == 1 && theForm.myOwnQuestion.value == "" )
    {
      show("ownQuestionCheckWarning");
      theForm.memorable.focus();
      failed = true;
    }

    if ( theForm.secret.value == "" )
    {
      show("secretCheckWarning");
      err_hiliteOn("secret");
      theForm.secret.focus();
      failed = true;
    }

    if ( theForm.terms.checked == false )
    {
      show("termsCheckWarning");
      theForm.terms.focus();
      failed = true;
    }

    if ( failed == false )
    {
      document.forms["regcheckform"].submit();
    }
    return (false);
  }


  function clearFieldsB()
  {
	document.regcheckform.reset();
    hide("ownquestion");

    clearWarningsB();
  }
  function clearWarningsB()
  {
  	hide("personalCheckWarning","passwordCheckWarning","password2CheckWarning","memorableCheckWarning","ownQuestionCheckWarning","secretCheckWarning","termsCheckWarning");
	err_hiliteOff("firstname","lastname","password","password2","secret");
  }

  function changeMemorableQuestion(index)
  {
    if ( index == 1 )
    {
      show("ownquestion");
    } else {
      hide("ownquestion");
    }
  }

function submitenterLogin(myfield,e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

	if (keycode == 13)
   	{
   		return submit_Validator(document.loginForm) && document.loginForm.submit();
		return false;
	}
	else
	return true;
}


/*
 * RADIO BUTTON CHECKS
 */

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


