var globalurlcount=0;
var GlobalComURL='';
var Wait=0;
var GlobalCheckURL='';
var CheckCount=0;
var TheRootURL = 'http://OakTreeSim.com/Micromatic/';
function CheckInternetConnection(){
  globalurlcount++;
  CheckCount++;
  var TheURL = 'images/blank.gif?TheGlobalURLCount='+globalurlcount+"&therandom="+Math.random();
  TheRootURL = 'http://OakTreeSim.com/Micromatic/';
  GlobalCheckURL = TheRootURL+TheURL
//  alert(GlobalCheckURL);
// removed for ajax solution instead......  
//  document.getElementById("ComCheckImage").src=GlobalCheckURL;
  var xmlReq = new XMLHttpRequest();
  xmlReq.open("GET", GlobalCheckURL, false);
  xmlReq.send(null);
  if (xmlReq.status == 200) {
    InternetConnectionOK();
  } else {
    InternetConnectionError();
  }
  return false;
}
function SubmitComRequest(URL,TheMess){
 if (parent.ComFrame) parent.ComFrame.document.location="about:blank";
  HideWaitText();
  HandleComURL=URL;
  GlobalComURL = URL;
  Wait=1;
  CheckCount=0;
  CheckInternetConnection();
  ShowComDiv(TheMess);
  WaitForReply();
//  parent.ComFrame.document.location=URL;
}
function InternetConnectionOK(){
  CheckCount=0;
  TheRootURL = 'http://OakTreeSim.com/Micromatic/';
  if (!GlobalComURL)alert('Unexpected error has occurred in the comunication system. Contact your system administrator.')
  if (GlobalComURL.length>0) parent.ComFrame.document.location=TheRootURL+GlobalComURL;
  GlobalComURL='';
}
function InternetConnectionError(){
  if (CheckCount<3){
     setTimeout("CheckInternetConnection()",7000);
     return false;
  }
  var SaveConnectionERRORURL=GlobalComURL;
  GlobalComURL='';
  var txt="Time is : "+Date()+"\n";
  txt+="Check Attempted: " + GlobalCheckURL + "\n"
  alert('ICE: Please take a print screen of this message and send it to sales@oaktreesim.com. Press Alt + PrtSc keys to take a print screen and then paste the message into a MS Word document.\n\nCurrently there is a connection problem between your computer and the Micromatic server that is preventing your save request from processing.  Wait 1 minute and retry.  Do not close Micromatic at this time; once the connection is re-established you will be able to save your decisions.  Open a new browser window and check your internet connection by viewing several pages at http://OakTreeSim.com. The Save function in Micromatic will work once that WWW site becomes available. \n\nCurrent Information = '+txt+'Connection String:'+ SaveConnectionERRORURL);

  HideWaitText();
  HideComDiv();
  if (SaveConnectionERRORURL.indexOf('Action=2')>-1 ||SaveConnectionERRORURL.indexOf('Action=SaveDec')>-1){
    var TheText = '<p class=QuickTipSave>Currently there is a connection problem between your computer and the Micromatic server that is preventing your save request from processing.  Your save request has not yet been processed.<br>&nbsp;<br>';
    var TheName;
    TheName=SubmissionLabel;
    OutSubmissionID = TheFieldValues["SubmissionID"].QuarterValues[CurrentQuarter];
  
  if (CurrentQuarter==MaxQuarter){
    StorePanelValues('panelMRKT',CurrentQuarter);
    StorePanelValues('panelCompInfoDec',CurrentQuarter);
    StorePanelValues('panelOPERDec',CurrentQuarter);
    StorePanelValues('panelPlantDec',CurrentQuarter);
    StorePanelValues('panelFINADec',CurrentQuarter);
  }
  var URL="http://OakTreeSim.com/Micromatic/HandleCom.cgi?SessionID="+SessionID+"&Action=SaveDec&Quarter="+CurrentQuarter+"&GameID="+GameID
  URL+="&Emergency=1&External=1";
  URL+="&OrigSubmissionID="+TheFieldValues["SubmissionID"].QuarterValues[CurrentQuarter];
  URL+="&SubmissionID="+OutSubmissionID
  URL+="&ProcessSub="+1
  URL+="&TheName="+TheName
  URL+=PutPanelValuesInURL("panelMRKT");
  URL+=PutPanelValuesInURL("panelCompInfoDec");
  URL+=PutPanelValuesInURL("panelOPERDec");
  URL+=PutPanelValuesInURL("panelPlantDec");
  URL+=PutPanelValuesInURL("panelFINADec");
  TheText+="<b>External Save Request:</b> <a href='"+URL+"' target=_SaveAttempt_>Click here</a> to attempt an external save of your current decisions.  <br>&nbsp;&nbsp;&nbsp;If the connection for the external attempt is also unavailable, bookmark the URL for the page that pops up <br>&nbsp;&nbsp;&nbsp;when you click on the above link.  This bookmark will allow you to return to this URL and save your <br>&nbsp;&nbsp;&nbsp;decisions when the connection with the Micromatic server becomes available.";
  TheText+="</p>";
  document.all.SavedDecisionTable.innerHTML=TheText;
  }
}
function InternetConnectionAbort(){
  if (CheckCount<3){
     setTimeout("CheckInternetConnection()",7000);
     return false;
  }
  var SaveConnectionERRORURL=GlobalComURL;
  GlobalComURL='';
  var txt=""
  txt+="Time is : "+Date()+"\n";
  txt+="GlobalCheckURL: " + GlobalCheckURL + "\n"
  alert('ICA: Please take a print screen of this message and send it to sales@oaktreesim.com. Press Alt + PrtSc keys to take a print screen and then paste the message into a MS Word document.\n\n Currently there is a connection problem between your computer and the Micromatic server that is preventing your save request from processing.  Wait 1 minute and retry.  Do not close Micromatic at this time; once the connection is re-established you will be able to save your decisions.  Open a new browser window and check your internet connection by viewing several pages at http://OakTreeSim.com. \n\nCurrent Information = '+txt+' \nConnection String:'+ SaveConnectionERRORURL);
  HideWaitText();
  HideComDiv();
  if (SaveConnectionERRORURL.indexOf('Action=2')>-1 ||SaveConnectionERRORURL.indexOf('Action=SaveDec')>-1){
    var TheText = '<p class=QuickTipSave>Currently there is a connection problem between your computer and the Micromatic server that is preventing your save request from processing.  Your save request has not yet been processed.<br>&nbsp;<br>';
    var TheName;
    TheName=SubmissionLabel;
    OutSubmissionID = TheFieldValues["SubmissionID"].QuarterValues[CurrentQuarter];
  
  if (CurrentQuarter==MaxQuarter){
    StorePanelValues('panelMRKT',CurrentQuarter);
    StorePanelValues('panelCompInfoDec',CurrentQuarter);
    StorePanelValues('panelOPERDec',CurrentQuarter);
    StorePanelValues('panelPlantDec',CurrentQuarter);
    StorePanelValues('panelFINADec',CurrentQuarter);
  }
  var URL="http://OakTreeSim.com/Micromatic/HandleCom.cgi?SessionID="+SessionID+"&Action=SaveDec&Quarter="+CurrentQuarter+"&GameID="+GameID
  URL+="&Emergency=1&External=1";
  URL+="&OrigSubmissionID="+TheFieldValues["SubmissionID"].QuarterValues[CurrentQuarter];
  URL+="&SubmissionID="+OutSubmissionID
  URL+="&ProcessSub="+1
  URL+="&TheName="+TheName
  URL+=PutPanelValuesInURL("panelMRKT");
  URL+=PutPanelValuesInURL("panelCompInfoDec");
  URL+=PutPanelValuesInURL("panelOPERDec");
  URL+=PutPanelValuesInURL("panelPlantDec");
  URL+=PutPanelValuesInURL("panelFINADec");
  TheText+="<b>External Save Request:</b> <a href='"+URL+"' target=_SaveAttempt_>Click here</a> to attempt an external save of your current decisions.  <br>&nbsp;&nbsp;&nbsp;If the connection for the external attempt is also unavailable, bookmark the URL for the page that pops up <br>&nbsp;&nbsp;&nbsp;when you click on the above link.  This bookmark will allow you to return to this URL and save your <br>&nbsp;&nbsp;&nbsp;decisions when the connection with the Micromatic server becomes available.";
  TheText+="</p>";
  document.all.SavedDecisionTable.innerHTML=TheText;
  }
}

function RefreshSession(){
  parent.ComFrame.document.location='HandleCom.cgi?Action=5&SessionID='+SessionID;
  setTimeout("RefreshSession()",2700000);
}
function ShowComDiv(TheMess){
  document.getElementById('ComDiv').style.visibility="visible";
  if (TheMess !='' && TheMess!=null){
    document.getElementById('ComText').innerHTML = "<center>" + TheMess + 
                           "<br> <img src=images/wait.gif border=0></center>";
  }
}
function HideWaitText(){
  document.getElementById('ComText').innerHTML="";
}
function NewSubFocus(){
  document.all.PastSubmissionID[0].checked=true;
}
function HideComDiv(){
  document.getElementById('ComDiv').style.visibility="hidden";
}
function WaitForReply(){
  document.getElementById('ComText').innerHTML="<center>Processing your request.  Please wait.<br><img src=images/wait.gif border=0></center>";
}


function SaveSettings(){
  SubmitComRequest("HandleCom.cgi?SessionID="+SessionID+"&Action=1&ShowQuickTip="+ShowQuickTip+"&ShowTransition="+ShowTransition);
}
function RefreshSavedList(){
  URL = "HandleCom.cgi?SessionID="+SessionID+"&Action=2&GameID="+GameID+"&DefaultSubmissionID="+TheFieldValues["SubmissionID"].QuarterValues[CurrentQuarter];
  SubmitComRequest(URL);
  setTimeout("CheckCom('"+URL+"')",30000);

}
function RefreshReviewList(SubmissionID){
  SubmitComRequest("HandleCom.cgi?SessionID="+SessionID+"&Action=ReviewDec&GameID="+GameID+"&SubmissionID="+SubmissionID);
}
function RetrieveDecision(SubmissionID){
  CheckOnUnLoad();
  SubmitComRequest("HandleCom.cgi?SessionID="+SessionID+"&Action=3&GameID="+GameID+"&SubmissionID="+SubmissionID);
}
function UpdateTeamInfo(){
  CheckOnUnLoad();
  SubmitComRequest("HandleCom.cgi?SessionID="+SessionID+"&Action=4&GameID="+GameID+"&TeamName="+document.all.EditTeamName.value+"&TeamSlogan="+document.all.EditTeamSlogan.value+"&TeamLogoURL="+document.all.EditTeamLogoURL.value);
}
function DelSubmission(SubmissionID){
  SubmitComRequest("HandleCom.cgi?SessionID="+SessionID+"&Action=DeleteSub&GameID="+GameID+"&SubmissionID="+SubmissionID);
}

