var bolChange = false;

// browser variables
var ns6 = (navigator.userAgent.substring(0,9) == 'Mozilla/5');
var ns4 = (document.layers) && (navigator.userAgent.substring(0,9) == 'Mozilla/4');
var ns = (ns4 || ns6)? true:false;
var ie4 = (navigator.appVersion.indexOf('Windows') != -1) && (navigator.appVersion.substring(17,23) == 'MSIE 4')? true:false;
var ie5 = (document.all) && (navigator.appVersion.substring(17,23) == 'MSIE 5')? true:false;
var ie6 = (document.all) && (navigator.appVersion.substring(17,23) == 'MSIE 6')? true:false;
var ieMac = (document.all) && (navigator.userAgent.indexOf('Mac')!=-1)? true:false;
var ie = (ie4 || ie5 || ie6 || ieMac)? true:false;
var ie5Mac = (ie5 && ieMac)? true:false;
var strConfirmation = "You have made a change and did not SAVE your work.\n\n" + 
				"Select OK to discard the changes and move to the next screen.\n\n" + 
				"Select CANCEL to return to the screen and click on the SAVE button to save your work.\n\n" +
		        "Note: Using the \"Back\" navigation in your browser after leaving\n" +
		        "this screen will make it appear that you have saved your information. However,\n" +
		        "unless you have actually pressed the SAVE button your work will be lost.";
var strReadOnly = "You are designated as a team member and can view this page.\n\n" + 
				"Only the project coordinator can update the \"Project Section\" of this application.";


function LimitSize(TheObject, Size)
{
   var theText = TheObject.value;

   if(theText.length < Size)
   {
      return true;
   }// End If
   else
   {
      TheObject.value = theText.substr(0, Size);
      return false;
   }// End Else
}// Close LimitSize

function Confirm(NextLocation)
{
   if(bolChange)
   {
     if(confirm(strConfirmation))
     {
         location.href = NextLocation;
     }// End If
   }//End If
   else
   {
      location.href = NextLocation;
   }// End If  
}// Close Confirm

function doChange(TheElement)
{
   bolChange = true;
   switch(TheElement.name)
   {
		case "ddlparticipated":
			doDisable();
			break;
		case "ddltenure":
			doDisable();
			break;
		case "ddlcitizen_country_id":
			doDisable();
			break;
   }// Close Switch
}// Close doChange

function doReadOnly(TheElement)
{
	alert(strReadOnly);
}// Close doReadOnly

function Init()
{
    for(i = 0; i < document.forms[0].elements.length; i++)
    {
		eval('document.forms[0].elements[i].onchange = new Function("doChange(this)")');
    }// End For
}// Close Init

function InitReadOnly()
{
    for(i = 0; i < document.forms[0].elements.length; i++)
    {
		eval('document.forms[0].elements[i].onkeydown = new Function("doReadOnly(this)")');
    }// End For
}// Close InitReadOnly

function checkMaxLength(textarea, evt, maxLength)
{
  var evtKeyCode = GetEventKeyCode(evt);
  var escChars = ",8,17,18,19,33,34,35,36,37,38,39,40,45,46,";
  if (escChars.indexOf(',' + evtKeyCode + ',') == -1) 
  {
    if (textarea.value.length >= maxLength-1) // if text is beyond limit, truncate and alert user...
    {
      textarea.value = textarea.value.substring(0, maxLength-1);
      alert('You\'ve reach the character limit for this question.  Please verify your answer since the text may have been truncated.');
      return false;
    }    
  }
  return true;

}// Close checkMaxLength

function GetEventKeyCode(evnt) {
   return evnt.keyCode ? evnt.keyCode : evnt.charCode ? 
          evnt.charCode : evnt.which ? evnt.which : void 0;
}

function storeSelection (field)
{
  if (document.all)
  {
    field.selected = true;
    field.selectedLength = field.createTextRange ? document.selection.createRange().text.length : 1;
  }// End If
}// Close storeSelection

function customWindow(name, URL)
{
	var sH = screen.availHeight - 250;
	var sW = screen.availWidth - 350;
	
	var win = window.open(URL, name,'height=' + sH + ',width=' + sW + ',scrollbars=yes,status=no,toolbar=yes,menubar=yes,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openWindow

function openInstitution(QueryString, WindowName)
{
	//var sH = screen.availHeight - 250;
	//var sW = screen.availWidth - 350;
	var sH = 600;
	var sW = 700;
	
	var win = window.open('/programs/list/?' + QueryString, WindowName,'height=' + sH + ',width=' + sW + ',scrollbars=yes,status=no,toolbar=no,menubar=yes,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openInstitution

function openInstitutionList(QueryString, WindowName)
{
	//var sH = screen.availHeight - 250;
	//var sW = screen.availWidth - 350;
	var sH = 600;
	var sW = 700;
	
	var win = window.open('/programs/list/list.aspx?' + QueryString, WindowName,'height=' + sH + ',width=' + sW + ',scrollbars=yes,status=no,toolbar=no,menubar=yes,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openInstitution

function openReferee(QueryString, WindowName)
{
	var sH = 550;
	var sW = 500;
	
	var win = window.open('/programs/referee/?' + QueryString, WindowName,'height=' + sH + ',width=' + sW + ',scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openReferee

function openParticipant(QueryString, WindowName)
{
	var sH = 380;
	var sW = 500;
	
	var win = window.open('/programs/participant/?' + QueryString, WindowName,'height=' + sH + ',width=' + sW + ',scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openParticipant

function closeList()
{
	if(window.opener && !self.opener.closed)
		self.opener.focus();

	self.window.close();
}// Close closeList

function openOSXWindow()
{
	var strURL = "/programs/faq/mac_osx.htm";
	
	var win = window.open(strURL, 'OSXWindow', 'height=400,width=450,scrollbars=yes,status=no,toolbar=no,menubar=yes,location=no,resizable=yes');
	
	if(win)
	{
		win.focus();
		win.moveTo(10,10);
	}// End If
}// Close openOSXWindow

function checkHelp()
{
	if (document.forms[0].txaproblem.value == "")
	{
		alert('Please describe the problem that you are having!');
	}
	else
	{
		document.forms[0].submit();
	}
}// Close checkHelp

function confirmSubmit()
{
	var msg = "By clicking 'OK' you will submit your application. At that point you will be unable to make any further revisions." +
				"\n\nDo you wish to continue?";
	if(confirm(msg))
	{
		document.forms[0].submit();
	}// End If
}// Close confirmSubmit

function confirmWithoutUpload()
{
	var msg = "Your proposal has not been uploaded successfully.\n\n" + 
				"Please try uploading it again, following the instructions on the \"Proposal Upload\" page.\n\n" + 
				"If you continue to experience difficulty with your upload, please contact ACLS using the OFAHELP button on the top right of any page.\n\n" +
				"If you click \"ok\" and submit your application now, it will be incomplete. No further revisions may be made after submission.";
	if(confirm(msg))
	{
		document.forms[0].submit();
	}// End If
}// Close confirmWithoutUpload

function confirmDeadline()
{
	var msg = "Your proposal has not been uploaded successfully.\n\n" + 
				"Please try uploading it again, following the instructions on the \"Proposal Upload\" page.\n\n" +
				"If you continue to experience difficulty with your upload: Because the submission\n" +
				"deadline is approaching, make sure to submit your application before the deadline\n" + 
				"even if your document has not uploaded successfully. Contact us at ofahelp@acls.org\n" +
				"before the deadline to notify us of the situation; your email must contain an attached\n" + 
				"copy of the document you have attempted to upload.";
	if(confirm(msg))
	{
		document.forms[0].submit();
	}// End If
}// Close confirmDeadline

function confirmWithoutPubUpload()
{
	var msg = "Your publication list has not been uploaded successfully.\n\n" + 
				"Please try uploading it again, following the instructions on the \"Publication List Upload\" page.\n\n" + 
				"If you continue to experience difficulty with your upload, please contact ACLS using the OFAHELP button on the top right of any page.\n\n" +
				"If you click \"ok\" and submit your application now, it will be incomplete. No further revisions may be made after submission.";
	if(confirm(msg))
	{
		document.forms[0].submit();
	}// End If
}// Close confirmWithoutPubUpload

function confirmPubDeadline()
{
	var msg = "Your publication list has not been uploaded successfully.\n\n" + 
				"Please try uploading it again, following the instructions on the \"Publication List Upload\" page.\n\n" +
				"If you continue to experience difficulty with your upload: Because the submission\n" +
				"deadline is approaching, make sure to submit your application before the deadline\n" + 
				"even if your document has not uploaded successfully. Contact us at ofahelp@acls.org\n" +
				"before the deadline to notify us of the situation; your email must contain an attached\n" + 
				"copy of the document you have attempted to upload.";
	if(confirm(msg))
	{
		document.forms[0].submit();
	}// End If
}// Close confirmPubDeadline
