var lAEs;
var st;
st=false;

function Detect(myfield,e,dec) {
  var key;
  var keychar;
  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || 
      (key==9) || (key==27) )
     return true;

  // control keys
  if (key==13) {
   if (myfield.name=='ISN') {
      GetAE();
    }else{
      GetAEwithISL();
    }
    return false;
  }

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
     return true;

  // decimal point jump
  else if (dec && (keychar == "."))
     {
    myfield.form.elements[dec].focus();
    return false;
    }
  else
     return false;
}

function GetAE() {
  onUpdating();
  var ISN;
  ISN = $get("ISN");
  if (ISN.value == '') {
    alert ('Please enter your ISN');
  }else {
    PageMethods.GetAE(ISN.value,OnSucceeded, OnFailed);
  }
}

function GetAEwithISL() {
  onUpdating();
  var ISL;
  ISL = $get("ISL");
  if (ISL.value == '') {
    alert ('Please enter your ISL. Enter a zero if you do not know your ISL.');
  }else {
    PageMethods.GetAEwithISL(lAEs,ISL.value, OnSucceeded, OnFailed);
  }
}

function OnSucceeded(result, userContext, methodName) {
  onUpdated();
  var ae=result[0];
  if (methodName == "GetAE") {
     var aPosition;
      aPosition = ae.PageResult.indexOf("ViewTeam.aspx"); 
      if (aPosition == -1) { //Error or ISL required
        var aError = ae.PageResult.startsWith("We cannot verify your ISN");
        if (aError == false) { //ISL required
          $get("AEISL").style.display = 'block';
          $get("ISL").focus();
          $get("ISL").value = '';
          lAEs = result;
          $get("ae_info").innerHTML = '';
        }else{ //Display error
          $get("ae_info").style.display = 'block';
          $get("AEISL").style.display = 'none';
          alert(ae.PageResult);
        }
      }else{ //Show AE no ISL
        $get("AEISL").style.display = 'none';
        $get("ae_info").style.display = 'block';
        if ($get("divState") != null) {
          $get("State").selectedIndex = 0;
          $get("divState").style.display = 'block';
        }
        $get("ae_info").innerHTML = ae.PageResult;
        if  ($get("isn_btn") != null) {
          $get("isn_btn").focus();
        } 
      } 
    }else{ //GetAE with ISL
       $get("ae_info").style.display = 'block'; 
       $get("ae_info").innerHTML = ae.PageResult;
       if ($get("divState") != null) {
        $get("State").selectedIndex = 0; 
        $get("divState").style.display = 'block';
       }
    }
}

function RS() {
  $get("AEISL").style.display = 'none';
  $get("ae_info").value = ''; 
  $get("ae_info").style.display = 'none';
  $get("ISL").value = '';
  if ($get("divState") != null) {
    $get("divState").style.display = 'none';
    $get("State").selectedIndex = 0;
  } 
}

function RSCorr() {
  //$get("ae_info").innerHTML = "<br/><input id='btnShowTeam' type='button' value=' View Team ' onClick='ShowTeam(\"\")'>"; 
}

function OnFailed(error, userContext, methodName) {
  onUpdated();
  if(error !== null)  {
    alert("An error occurred: "+ error.get_message());
  }
}

function FindAppraiser(whichButton,pageURL) {
  var x=window.confirm("By clicking \"OK\" and accessing CitiMortgage, Inc.'s proprietary and confidential information provided here:  1) you are bound by the confidentiality provisions of your Correspondent Loan Purchase Agreement with CitiMortgage, Inc., including without limitation the provisions of the Correspondent Manual (the \"Agreement\"); or 2) if you are a person or entity that is not a party to the Agreement, you agree to use the information only for appropriate business purposes on behalf of a party to the Agreement, to not disclose the information to any other party without the express written consent of CitiMortgage, Inc. and to protect the information in the same fashion that you would protect your own confidential information.");
  if (x) {
    if (pageURL.length==0) {
    GetAE();
    } else {
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 )) {
      xposition = (screen.width - 980) / 2;
      yposition = (screen.height - 850) / 2;
    }
    var args = "";
    args += "width=980,height=850,"
    + "location=0,"
    + "menubar=0,"
    + "resizable=0,"
    + "scrollbars=1,"
    + "statusbar=false,dependent,alwaysraised,"
    + "status=false,"
    + "titlebar=no,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only
    //fullscreen=yes, add for full screen
    //var dmcaWin = window.open(pageURL,'',args );
    //dmcaWin.focus();
    window.showModalDialog(pageURL,"","dialogWidth:980px;dialogHeight:850px;center:1;status:0");
    } 
  } 
}

function onUpdating(){
  // get the update progress div
  var pnlPopup = $get('pnlPopup'); 
  //  get the gridview element        
  var divAE = $get('ae_locator');
  // make it visible
  pnlPopup.style.display = '';
  // get the bounds of both the locator div and the progress div
  var divBounds = Sys.UI.DomElement.getBounds(divAE);
  //var pnlPopupBounds = Sys.UI.DomElement.getBounds(pnlPopup);
  //  center of locator div
  //var x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(pnlPopupBounds.width / 2);
  //var y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(pnlPopupBounds.height / 2);
 //  top left of locator div 
  var x = divBounds.x;
  var y = divBounds.y;
  //    set the progress element to this position
  Sys.UI.DomElement.setLocation(pnlPopup, x, y);           
}

function onUpdated() {
  // get the update progress div
  var pnlPopup = $get('pnlPopup'); 
  // make it invisible
  pnlPopup.style.display = 'none';
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
