
function isWebview(func)
{
  if(func == null)
  {
    func = 'SetField';
  }

  var x=  (window.external && typeof window.external[func] != "undefined" ) ? true : false;

  return x;

}

function callWebview(func,defaultReturnValue)
{
  var ret = defaultReturnValue;

  if ( isWebview(func) )
  {
    var args = Array.prototype.slice.call(arguments);
    if(args.length < 3)
    {
      ret = window.external[func]();
    }
    else
    {
      //we have to call eval and do this for loop because
      // window.external[func].apply(null,args.slice(2));
      // cannot be called since it really isn't a Function object
      var call = "window.external."+func+"(";
      for(var i = 2; i < args.length; i++)
      {
        call += 'args['+i+']';
        if(i+1 != args.length)
          call +=',';
      }
      call+=');';
      ret = eval(call);

    }
  }

  return ret;
}

//window.external['CancelRegistration'] is == 'unknown' not 'function'
function CancelApp()  //Compatible with Common webview pre 4/25/2008
{
  return CancelRegistration();
}

function CancelRegistration()  //Compatible with Common webview pre 4/25/2008
{
  if ( isWebview('CancelRegistration') ) {
        window.external.CancelRegistration();
    }
    return false;  //used to prevent application from trying to validate on "post" cancel button.
}

function SetField(field,val)  //Compatible with Common webview pre 4/23/2008
{
    if ( isWebview('SetField') ) {
        //alert(field+" set to: "+val);
        window.external.SetField(field,val);
        return true;

    }
    return false;
}

function GetField(fld)
{

    if ( isWebview('GetField') ) {
        return window.external.GetField(fld);
    }
    else {
    	return "";
    }
}

function MarkWebRegSuccess()
{
    if ( isWebview('MarkWebRegSuccess') ) {

        window.external.MarkWebRegSuccess();
        return true;
    }
    return false;
}

function SetRegStatus(status)
{
    if ( isWebview('SetRegStatus') ) {
        window.external.SetRegStatus(status);
        return true;
    }
    return false;
}

function ReloadPage()  // Reload the page / URL
{
    callWebview('ReloadPage');
}

function GotoPage(pageName)  // Goto a specific page in the app
{
    callWebview('GotoPage',null,pageName);
}

function GetSystemParameter(paramName)  // return system parameter or false if it doesn't work
{
    return callWebview('GetSystemParameter',false,paramName);
}

function RedrawScreen()  // redraw the screen
{
    callWebview('RedrawScreen');
}



function OpenLinkInBrowser(url)
{
    if ( isWebview('OpenLinkInBrowser') ) {
        window.external.OpenLinkInBrowser(url);
    }
    else
    {
        window.open(url,'_blank');
    }
}


function LTAlert(msg)
{
    alert(msg);
    RedrawScreen();
}


function update(FormName) {  //Compatible with Common webview 4/23/2008
  if (FormName == null) {
    theForm = document.forms[0];
  }else{
  	theForm = eval("document."+FormName);
  }

	if(theForm == null) { return; }

  for(var i=0; i<theForm.elements.length; i++){
    updateField(theForm.elements[i]);
  }

}

function updateField(theField)
{
  var alertText = "";
  var fld = "";
  var val = null;

  if (theField.style.visibility=="hidden" || theField.style.display=="none"){
    //Clear the field if its hidden
    fld=theField.name;
    val="";
  }
  else
  {
  	// Hidden field will not be saved back to prefs file - intentionally
    if(theField.type == "text" || theField.type == "textarea" ){  //can add hidden fields by adding "hidden"
      alertText += "Element Type: " + theField.type + "\n";
      alertText += "Element Name: "  + theField.name + "\n";
      alertText += "Element Value: " + theField.value + "\n";
      fld=theField.name;
      val=theField.value;
    }
    else if(theField.type == "checkbox"){
      alertText += "Element Type: " + theField.type + "\n";
      alertText += "Element Name: "  + theField.name + "\n";
      alertText += "Element Checked? " + theField.checked + "\n";
      fld=theField.name;
	  val=theField.checked ? "1":"0";
    }
    else if(theField.type == "select-one"){
      alertText += "Element Type: " + theField.type + "\n";
      alertText += "Element Name: "  + theField.name + "\n";

	  fld=theField.name;
	  if(theField.selectedIndex > -1)
	  {
        alertText += "Selected Option's Value: " + theField.options[theField.selectedIndex].value + "\n";
		val=theField.options[theField.selectedIndex].value;
	  }
	  else
	  {
		val = "";
	  }
    }

    //No value will be prepopulated if no choice was previously made for the radio group
    else if(theField.type == "radio"){
      if (theField.checked){
      	alertText += "Element Type: " + theField.type + "\n";
      	alertText += "Element Name: "  + theField.name + "\n";
      	alertText += "Selected Radio's Value: "+theField.value + "\n";
      	fld=theField.name;
      	val=theField.value;
      }
    }
  }

  if (fld !="" && val!=null) {
    SetField (fld,val);
  }

//   if(alertText != "")
//     alert(alertText);

}

function prepopulateForm(FormName)
{
  var theForm;

  if (FormName == null) {
    theForm = document.forms[0];
  }else{
  	theForm = eval("document."+FormName);
  }

  for(var i=0; i<theForm.elements.length; i++){
    prepopulateField( theForm.elements[i] );
  }
}

function prepopulateFieldName(FormName,FieldName) {  //Compatible with Common webview 4/23/2008
  var theForm;

  if (FormName == null) {
    theForm = document.forms[0];
  }else{
  	theForm = eval("document."+FormName);
  }

  //This is not correct for radios
  prepopulateField(theForm[FieldName]);
}


function prepopulateField(theField,always) {  //Compatible with Common webview 4/23/2008

  if(always == null)
    always = true;

  var alertText = "";
  if(theField)
  {
  	 //alert('iffield: ' + theField.name);
    if (theField.style.visibility=="hidden" || theField.style.display=="none")
    {
      return;
    }
    else
    {
      var selectOne = true;

      switch(theField.type)
      {
      case "hidden":
      case "text":
      case "textarea":
        if(always || theField.value == "")
          theField.value = GetField(theField.name);
        break;

      case "checkbox":
        //BHL is null == false?
        if(always || theField['checked'] == false || theField['checked'] == null)
        {
          var val = GetField(theField.name);
          if(val == 1 || val == 'Y' || val == 'y')
          {
            theField['checked'] = true;
          }
        }
        break;

        //      case "select-multiple":
        //        selectOne = false;
        //  //Fall through here...no break;
      case "select-one":
        //BHL This isn't quite right. -- Double check 0
        if(always || theField.selectedIndex <= 0)
        {
          var found = false;
          var val = GetField(theField.name);

          //search the option values
          for(var i = 0 ; i < theField.options.length ; i++)
          {
            if(theField.options[i].value == val)
            {
              found = true;
              if( selectOne )
              {
                theField.selectedIndex = i;
                break;
              }
              else
              {
                theField.options[i]['selected'] = true;
              }
            }
          }

          //search the option text
          if(! found)
          {
            for(var i = 0 ; i < theField.options.length ; i++)
            {
              if(theField.options[i].text == val)
              {
                found = true;
                if( selectOne )
                {
                  theField.selectedIndex = i;
                  break;
                }
                else
                {
                  theField.options[i]['selected'] = true;
                }
              }
            }
          }

          if( selectOne && ! found )
          {
            theField.selectedIndex = 0;
          }
        }
        break;

      case "radio":
        //BHL : Need to check Whole radio group before setting the checked attribute
        var theFieldObj = theField.form[theField.name];
        if(always || isRadioGroupChecked(theFieldObj) != true)
        {
          var val = GetField(theField.name);
          //alert(val + ' and ' + theField.value);
          if(val == theField.value)
            theField['checked'] = true;
          else
            theField['checked'] = false;
        }
        break;
      }
    }
  }
//   if(alertText != "")
//     alert(alertText);
}

function isRadioGroupChecked(fieldObj)
{
  for (var i=0; i < fieldObj.length; i++)
  {
    if(fieldObj[i].checked)
      return true;
  }

  return false;
}

function initWebviewPage(FormName,skipList)
{
  if (FormName == null) {
    theForm = document.forms[0];
  }else{
  	theForm = eval("document."+FormName);
  }

	if(theForm == null) { return; }

  //Build a skip list object / hash for easy lookup.
  var skipListObj = {};
  if(skipList != null)
  {
    for(var j=0; j<skipList.length; j++){
      skipListObj[skipList[j]] = true;
    }
  }


  for(var i=0; i<theForm.elements.length; i++){
    var theField = theForm.elements[i];
    switch(theField.type)
    {
    case 'hidden':
        if(skipListObj[theField.name] != true)
        {
      		if(theField.name.length == 4 && theField.value == "" ) {
            	prepopulateField(theField);
          	}
        }
        break;

    case 'text':
    case 'textarea':
    case 'checkbox':
    case 'select-one':
//    case 'select-multiple':
    case 'radio':
      //skip any fields in the skip list.
      if(skipListObj[theField.name] != true)
      {
        if(theField.name.length == 4)
        {
        	prepopulateField(theField,false);
		}

        if(theField.onchange)
        {
          theField.onchange();
        }

      }

      if(theField['onblur'] == null)
      {
        theField.onblur=function(event){updateField(this)};
      }

    }

  }
}

