﻿// global vars
var currentHelpSection = null;
var helpCache = null;
var timer = null;
var oldValue = null;

function init() {
  initHelpCache();
}

function showSection (element) {
  document.getElementById(element).style.visibility = 'visible';
  document.getElementById(element).style.display = 'block';
}

function hideSection (element) {
  document.getElementById(element).style.visibility = 'hidden';
  document.getElementById(element).style.display = 'none';
}

function showHelp (element, content) {
  if(element != "") {
    if(currentHelpSection != null) 
      hideSection(currentHelpSection);
      
    showSection(element);
    currentHelpSection = element;
    document.getElementById(element).innerHTML = content;
  }
}

function help(article, helpSection) {
  if(helpCache.containsKey(article)) {
    showHelp(helpSection, helpCache.get(article));
  } else {
    showHelp(helpSection, article);    
	}
}

function Hashtable() {
  this.clear = hashtable_clear;
  this.containsKey = hashtable_containsKey;
  this.containsValue = hashtable_containsValue;
  this.get = hashtable_get;
  this.isEmpty = hashtable_isEmpty;
  this.keys = hashtable_keys;
  this.put = hashtable_put;
  this.remove = hashtable_remove;
  this.size = hashtable_size;
  this.toString = hashtable_toString;
  this.values = hashtable_values;
  this.hashtable = new Array();
}

function hashtable_clear(){
  this.hashtable = new Array();
}

function hashtable_containsKey(key){
  var exists = false;
  for (var i in this.hashtable) {
    if (i == key && this.hashtable[i] != null) {
      exists = true;
      break;
    }
  }
  
  return exists;
}

function hashtable_containsValue(value){
  var contains = false;
  if (value != null) {
    for (var i in this.hashtable) {
      if (this.hashtable[i] == value) {
        contains = true;
        break;
      }
    }
  }
  
  return contains;
}

function hashtable_get(key){
  return this.hashtable[key];
}

function hashtable_isEmpty(){
  return (parseInt(this.size()) == 0) ? true : false;
}

function hashtable_keys(){
  var keys = new Array();
  for (var i in this.hashtable) {
    if (this.hashtable[i] != null)
      keys.push(i);
  }
  
  return keys;
}

function hashtable_put(key, value){
  if (key == null || value == null) {
    throw "NullPointerException {" + key + "},{" + value + "}";
  } else {
    this.hashtable[key] = value;
  }
}

function hashtable_remove(key){
  var rtn = this.hashtable[key];
  this.hashtable[key] = null;
  return rtn;
}

function hashtable_size(){
  var size = 0;
  for (var i in this.hashtable) {
    if (this.hashtable[i] != null)
      size ++;
  }
  
  return size;
}

function hashtable_toString(){
  var result = "";
  for (var i in this.hashtable) {     
    if (this.hashtable[i] != null)
      result += "{" + i + "},{" + this.hashtable[i] + "}\n";  
  }
  
  return result;
}

function hashtable_values(){
  var values = new Array();
  for (var i in this.hashtable) {
    if (this.hashtable[i] != null)
      values.push(this.hashtable[i]);
  }
  
  return values;
}

function initContactControl()
{
    toggleAlternativeContact();
}

function enableDatePicker()
{
    document.forms[0].ctl00_contentPalaceHolder1_txtAreasOfOperationChanged_image.height = "22";
    document.forms[0].ctl00_contentPalaceHolder1_txtAreasOfOperationChanged_image.width = "28";
}

function initCreateAccount() {
  toggleAlternativeContact();
  toggleCompanyRegNo();
}

function initApplicationForm2() {
  toggleStreetAddress();
}

function toggleAlternativeContact() {
  if(document.forms[0].ctl00_contentPalaceHolder1_ContactControl1_chkAlternativeContact.checked)
    showSection("sctAlternativeContact");
  else
    hideSection("sctAlternativeContact");
}

function toggleCompanyRegNo() {
  if(document.forms[0].ctl00_contentPalaceHolder1_radCompany_1.checked)
    showSection("sctCompanyRegNo");
  else
    hideSection("sctCompanyRegNo");
}

function toggleStreetAddress() {
  if(document.forms[0].ctl00_contentPalaceHolder1_radSameAddress_1.checked)
    showSection("sctStreetAddress");
  else
    hideSection("sctStreetAddress");
}

function initApplicationForm3() {
  if(document.forms[0].ctl00_contentPalaceHolder1_radCharityType_0.checked)
    showSection('sctTrust');
  else if(document.forms[0].ctl00_contentPalaceHolder1_radCharityType_1.checked)
    showSection('sctSociety');
    
  if(document.forms[0].ctl00_contentPalaceHolder1_radMaoriTrustBoard_0.checked) {
    showSection('sctMaoriTrustBoard');
    showSection('sctMaoriReservation');
  }
  
  if(document.forms[0].ctl00_contentPalaceHolder1_radMaoriReserve_0.checked)
    showSection('sctMaoriIdentifiedReserve');
    
  if(document.forms[0].ctl00_contentPalaceHolder1_radMaoriReserveTeWhenua_0.checked)
    showSection('sctGazette');
    
  if(document.forms[0].ctl00_contentPalaceHolder1_radGazetteUploadNow.checked)
    showSection('sctGazetteUpload');
}

function initWithholdRequestBox()
{
	if(document.forms[0].ctl00_contentPalaceHolder1_radWithholdUploadNow.checked) 
	{
		showSection('sctUploadWithhold');
	}
	else
	{
		hideSection('sctUploadWithhold');
	}
}

function initApplicationForm4() {
	if(document.forms[0].ctl00_contentPalaceHolder1_radCharityRulesUploadNow) 
	{
		if(document.forms[0].ctl00_contentPalaceHolder1_radCharityRulesUploadNow.checked) 
		{
			showSection('sctUploadRules');
		}
	}
	else
	{
		hideSection('sctUploadRules');
	}
	

  toggleOther('ctl00_contentPalaceHolder1_chkOtherSectors', 'ctl00_contentPalaceHolder1_txtSectorsOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherActivities', 'ctl00_contentPalaceHolder1_txtActivitiesOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherBeneficiaries', 'ctl00_contentPalaceHolder1_txtBeneficiariesOther');
}

function initApplicationForm5() 
{
	toggleOther('ctl00_contentPalaceHolder1_chkOtherSources', 'ctl00_contentPalaceHolder1_txtOtherSources');
}

function initApplicationForm6() {
  initHelpCache();
  
  hideSection('sctOtherDetails');
  
  if(document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_radOfficerType_0.checked)
  {
    showSection('sctIndividual');
    showSection('sctOtherDetails');
  }
  if(document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_radOfficerType_1.checked)
  {
    showSection('sctBodyCorporate');
    showSection('sctOtherDetails');
  }
  if(document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_radOCFOnline.checked)
    showSection('sctOnlineOCF');
    
}

function initEditOfficerControl()
{
	if(document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_radOCFOnline.checked)
		if(document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_divProcessingCenterUpload)
			showSection('document.forms[0].ctl00_contentPalaceHolder1_EditOfficerControl1_divProcessingCenterUpload');
}

function initHelpCache() {
  if(helpCache == null)
    helpCache = new Hashtable();
}

function dropMonthSelectDay() {
  document.forms[0].ctl00_contentPalaceHolder1_cmbDay[30] = null;
  document.forms[0].ctl00_contentPalaceHolder1_cmbDay[29] = null;
  document.forms[0].ctl00_contentPalaceHolder1_cmbDay[28] = null;
  
  for(z = 0; z < 12; z++)
    if(document.forms[0].ctl00_contentPalaceHolder1_cmbMonth[z].selected) {
      switch(z) {
        case 1:
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[27].selected = true;
          break;
        case 3:
        case 5:
        case 8:
        case 10:
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[28] = new Option("29", "29");
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[29] = new Option("30", "30");
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[29].selected = true;
          break;
        default:
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[28] = new Option("29", "29");
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[29] = new Option("30", "30");
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[30] = new Option("31", "31");
          
          document.forms[0].ctl00_contentPalaceHolder1_cmbDay[30].selected = true;
          break;
      }
    }
  
}

function toggleOther(check, textbox) {
  txt = document.getElementById(textbox);
  if(document.getElementById(check).checked) {
    txt.style.display = "block";
    txt.style.visibility = "visible";
  } else {
    txt.style.display = "none";
    txt.style.visibility = "hidden";
  }
}

var oldLength = 0;
var oldText = "";

function dontPaste() {
  alert('Please type the email address, to confirm it is accurate.');
  return false;
}

function detectPaste(newText) {
  var newLength = newText.length;
  if(oldLength - newLength < -1) {
    dontPaste();
    return oldText;
  }
  
  oldLength = newLength;
  
  return oldText = newText;
}

function titleCase(frmObj) {
  var index;
  var tmpStr;
  var tmpChar;
  var preString;
  var postString;
  var strlen;
  tmpStr = frmObj.value.toLowerCase();
  strLen = tmpStr.length;
  if (strLen > 0)  {
    for (index = 0; index < strLen; index++)  {
      if (index == 0)  {
        tmpChar = tmpStr.substring(0,1).toUpperCase();
        postString = tmpStr.substring(1,strLen);
        tmpStr = tmpChar + postString;
      } else {
        tmpChar = tmpStr.substring(index, index+1);
        if (tmpChar == " " && index < (strLen-1))  {
          tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
          preString = tmpStr.substring(0, index+1);
          postString = tmpStr.substring(index+2,strLen);
          tmpStr = preString + tmpChar + postString;
        }
      }
    }
  }
  frmObj.value = tmpStr;
}

function initGroupDetails() {
  if(document.forms[0].ctl00_contentPalaceHolder1_radAddCharity_0.checked)
    showSection('sctAddNewCharity');
  if(document.forms[0].ctl00_contentPalaceHolder1_radAddCharity_1.checked)
    showSection('sctAddExistingCharity');    
}

function initGroupForm2() {
  if(document.forms[0].ctl00_contentPalaceHolder1_ContactControl1_chkAlternativeContact.checked)
    showSection('sctAlternativeContact');
}

function subTotal(subText, totalText)
{
  var num = 0;
  
  // First check to see if it's a valid number
  if(IsNumeric(num))
  {
	num = subText.value;
  }

  // If > 1000, round to nearest dollar, otherwise round to nearest thousand
  if( num < 1000 )
  {
	num = Math.round(num);
  }	
  else 
  {
	num = Math.round(num / 1000) * 1000;
  }
	
  //subText.value = num;
  
  totalText.value = parseInt(totalText.value) - parseInt(oldValue) + parseInt(num);
}

function getSurplus(plusText, minusText, totalText)
{
    var num1 = 0;
    var num2 = 0;

	if(IsNumeric(plusText.value))
	{
		num1 = plusText.value;
	} 
	
	if(IsNumeric(minusText.value))
	{
		num2 = minusText.value;
	}
	
	totalText.value = parseInt(num1) - parseInt(num2);
}

function ValidateNumeric(text, errorLabel)
{
    if(IsNumeric(text.value))
    {
        errorLabel.className = "errorCaptionHidden";
    }
    else
    {
        errorLabel.className = "errorCaption";            
    }
}

function calculateTotal(incomeArray, expenditureArray, incomeTotal, expenditureTotal, surplusText, add)
{
    var inTotal = 0;
    var exTotal = 0;
    
    for( var i=0 ; i < incomeArray.length; i++ ) 
    {
        if(IsNumeric(incomeArray[i].value))
        {
            inTotal = inTotal + parseInt(incomeArray[i].value);
        } 
    }

    for( var i=0 ; i < expenditureArray.length; i++ ) 
    {
        if(IsNumeric(expenditureArray[i].value))
        {
            exTotal = exTotal + parseInt(expenditureArray[i].value);
        }
    }

    incomeTotal.value = inTotal;        
    expenditureTotal.value = exTotal;
    
    if (add == 'true')
    {
        surplusText.value = inTotal + exTotal;
    }
    else
    {
        surplusText.value = inTotal - exTotal;
    }
}

function sumBalanceTotal(plusText1, plusText2, totalText)
{
    var num1 = 0;
    var num2 = 0;

	if(IsNumeric(plusText1.value))
	{
		num1 = plusText1.value;
	}
	
	if(IsNumeric(plusText2.value))
	{
		num2 = plusText2.value;
	}
	
	totalText.value = parseInt(num1) + parseInt(num2);
}

function saveValue(subText, totalText) 
{
	var num = subText.value;

	//First check to see if it's a valid number
	if(IsNumeric(num))
	{
		oldValue = parseInt(subText.value);
	}
	else
	{
	    oldValue = 0;
	}
}

function IsNumeric(sText)
{
   var ValidChars = "-0123456789.";
   var IsNumber=true;
   var Char;
 
   if (sText == null || sText.length == 0) 
   {
    IsNumber = false; 
   } else {
       for (i = 0; i < sText.length && IsNumber == true; i++) 
          { 
          Char = sText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
             {
             IsNumber = false;
             }
          }
    }
   return IsNumber;   
}

function initNoCForm1()
{
	toggleStreetAddress();
}

function initNoCForm2() {
  if(document.forms[0].ctl00_contentPalaceHolder1_radRulesChanged_0.checked)
  {
		showSection('sctRulesChanged');
		showSection('sctRulesSupportDoc');
	}
	
  toggleOther('ctl00_contentPalaceHolder1_chkOtherSectors', 'ctl00_contentPalaceHolder1_txtSectorsOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherActivities', 'ctl00_contentPalaceHolder1_txtActivitiesOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherBeneficiaries', 'ctl00_contentPalaceHolder1_txtBeneficiariesOther');
}

function initNoCForm3()
{

}

function initGroupForm4() {
 
  if(document.forms[0].ctl00_contentPalaceHolder1_radGroupRulesUploadNow.checked)
    showSection('sctUploadRules');

  toggleOther('ctl00_contentPalaceHolder1_chkOtherSectors', 'ctl00_contentPalaceHolder1_txtSectorsOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherActivities', 'ctl00_contentPalaceHolder1_txtActivitiesOther');
  toggleOther('ctl00_contentPalaceHolder1_chkOtherBeneficiaries', 'ctl00_contentPalaceHolder1_txtBeneficiariesOther');
}

function initNoCForm5()
{

}

function initNoCForm6()
{

}

function initNoCForm7()
{

}

function clickButton(e, buttonid)
{ 
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt)
      { 
          if (evt.keyCode == 13)
          { 
                bt.click(); 
                return false; 
          } 
      } 
}

function getFilePostfix(fileName)
{
	for(s = fileName.length; s > -1 && fileName != ""; s--)
	{
		if(fileName.charAt(s) == ".")
		{
			return fileName.substring(s + 1, fileName.length);
		}
	}
}

function checkDuplicateFileName(gridId, fileId)
{
	var grid = document.getElementById(gridId);
	var gridLength = grid.rows.length;
	var file = document.getElementById(fileId);
	var filePath = file.value;
	var fileName = "";									
	var filePostfix = "";
	var srcPostfix = "";			
	
	filePath = filePath.replace(/\\/g,"/");

	for(x = filePath.length; x > -1 && fileName == "" ; x--)
	{
		if(filePath.charAt(x) == "/")
		fileName = filePath.substring(x + 1, filePath.length);
	}

	// loop through each row, retrieve
	for(i = 0; i < gridLength; i++)
	{	
		var cellElements = grid.rows[i].cells[1].getElementsByTagName('a');
		var maxj = cellElements.length;
		
		for(j = 0; j < maxj; j++)
		{			
			srcPostfix = getFilePostfix(cellElements[0].getAttribute('title')).toLowerCase();
			filePostfix = getFilePostfix(fileName).toLowerCase();
		
			if(srcPostfix == "pdf" &&
					(filePostfix == "doc" || filePostfix == "docm" || filePostfix == "docx" ||
					filePostfix == "dot" || filePostfix == "dotx" || filePostfix == "htm" ||
					filePostfix == "html" || filePostfix == "rtf" || filePostfix == "txt" ||
					filePostfix == "xml" || filePostfix == "xps"))
				{
					if(cellElements[0].getAttribute('title').replace(srcPostfix, "") == fileName.replace(filePostfix, ""))
					{
						if(confirm("This folder already contains a file named '" + 
							fileName.replace(filePostfix, "pdf") + "'.\r\nWould you like to replace the existing file?")==true)
							return true;
						else
							return false;
					}
				}
			else if(cellElements[0].getAttribute('title') == fileName)
			{
				if(confirm("This folder already contains a file named '" + 
					fileName + "'.\r\nWould you like to replace the existing file?")==true)
					return true;
				else
					return false;
			}
		}
	}
}

function checkDuplicateFileName_SummaryPage(gridId, fileId, folderId)
{
	var grid = document.getElementById(gridId);
	var gridLength = grid.rows.length;
	var file = document.getElementById(fileId);
	var filePath = file.value;	
	var fileName = "";
	var folder = document.getElementById(folderId);
	var folderName = folder.value;								
	var filePostfix = "";
	var srcPostfix = "";	
		
	filePath = filePath.replace(/\\/g,"/");
	
	for(x = filePath.length; x > -1 && fileName == "" ; x--)
	{
		if(filePath.charAt(x) == "/")
		fileName = filePath.substring(x + 1, filePath.length);
	}

	// loop through each row, retrieve
	for(i = 0; i < gridLength; i++)
	{	
		var cellElements = grid.rows[i].cells[1].getElementsByTagName('a');
		var FileElements = grid.rows[i].cells[2];
		var maxj = cellElements.length;
		
		for(j = 0; j < maxj; j++)
		{					
			srcPostfix = getFilePostfix(cellElements[0].getAttribute('title')).toLowerCase();
			filePostfix = getFilePostfix(fileName).toLowerCase();
						
			if(srcPostfix == "pdf" &&
					(filePostfix == "doc" || filePostfix == "docm" || filePostfix == "docx" ||
					filePostfix == "dot" || filePostfix == "dotx" || filePostfix == "htm" ||
					filePostfix == "html" || filePostfix == "rtf" || filePostfix == "txt" ||
					filePostfix == "xml" || filePostfix == "xps"))
			{
				if(cellElements[0].getAttribute('title').replace(srcPostfix, "") == fileName.replace(filePostfix, ""))
				{
					if(confirm("This folder already contains a file named '" + 
						fileName.replace(filePostfix, "pdf") + "'.\r\nWould you like to replace the existing file?")==true)
						return true;
					else
						return false;
				}
			}
			else if(cellElements[0].getAttribute('title') == fileName && 
						FileElements.innerHTML == folderName)
			{
				if(confirm("This folder already contains a file named '" + 
					fileName + "'.\r\nWould you like to replace the existing file?")==true)
					return true;
				else
					return false;
			}
		}
	}
}

function areCookiesEnabled()
{     
    document.cookie = "TestForThirdPartyCookie=yes";
    if ( document.cookie.indexOf( "TestForThirdPartyCookie=" ) == -1 ) {
        return false;
    }
    else
    {
        document.cookie = "TestForThirdPartyCookie=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
        return true;
    }
   
}
