separator = ".";
calcLanguage = "EN";


DateCells = new Array("DAY", "MONTH", "YEAR", "NULL");


function setLangForDates(){
	var location = window.location.pathname;
	if(location.indexOf("/en/") > -1){
		calcLanguage = "EN";
	}else if(location.indexOf("/fr/") > -1){
		calcLanguage = "FR";
	}else if(location.indexOf("/ja/") > -1){
		calcLanguage = "JA";
		DateCells = new Array("YEAR", "MONTH", "DAY", "NULL");
	}else if(location.indexOf("/ko/") > -1){
		calcLanguage = "KO";
		DateCells = new Array("YEAR", "MONTH", "DAY", "NULL");
	}else if(location.indexOf("/es/") > -1){
		calcLanguage = "ES";
	}else if(location.indexOf("/de/") > -1){
		calcLanguage = "DE";
	}else if(location.indexOf("/pt/") > -1){
		calcLanguage = "PT";
	}else if(location.indexOf("/ar/") > -1){
		calcLanguage = "AR";
	}else if(location.indexOf("/zh/") > -1){
		calcLanguage = "ZH";
		DateCells = new Array("YEAR", "MONTH", "DAY", "NULL");
	}else if(location.indexOf("/ru/") > -1){
		calcLanguage = "RU";
	}
}
setLangForDates();

STARTIndex = 0;
PRIIndex = 1;
ISRIndex = 2;
CHANGEIndex = 3;
IFIndex = 4;
navType = "Tab";

selectedBox = 0;
selectedTab = 0;
answerWidth = 560;
mainTableWidth = 550;

Dates = new Array(new dateObj("Start Date"), new dateObj("Earliest Priority Date"), new dateObj("ISR Transmittal Date"), new dateObj("Priority Date as<BR>Proposed to be Changed"), new dateObj("International Filing Date"));
monthOffset = 0;

FieldNames = new Array("STARTDATE", "PRIDATE", "ISRDATE", "CHANGEDATE", "IFDATE");


function calcTab() {
  this.dateFields = "";
  this.tabName = "";
  this.tabLabels = new Array();
  this.explanations = new Array();
  this.texts = new Array();
  this.headers = new Array();
}

pctCalcTab = new calcTab();
pctCalcTab.dateFields = "0";
pctCalcTab.tabName = "Calculator";

pctDeadlineTab = new calcTab();
pctDeadlineTab.dateFields = "1";
pctDeadlineTab.tabName = "PCTDeadlines";


withdrawalTab = new calcTab();
withdrawalTab.dateFields = "1";
withdrawalTab.tabName = "Withdrawal";

supplementaryTab = new calcTab();
supplementaryTab.dateFields = "1";
supplementaryTab.tabName = "Supplementary";

article19Tab = new calcTab();
article19Tab.dateFields = "12"
article19Tab.tabName = "Article19";


demandTab = new calcTab();
demandTab.dateFields = "12";
demandTab.tabName = "Demand";

recordTab = new calcTab();
recordTab.dateFields = "1";
recordTab.tabName = "Record";

correctTab = new calcTab();
correctTab.dateFields = "134";
correctTab.tabName = "Rule26bis";

tabs = new Array(pctDeadlineTab, correctTab, article19Tab, demandTab, supplementaryTab, withdrawalTab, recordTab, pctCalcTab);

firstBox = "";
type = tabs[selectedTab].tabName;
function copyForm() {
	for (i = 0; i < FieldNames.length; i++) {
		dateString = "";
		obj = findObj(FieldNames[i]+"_DAY")
		if (obj) {
			dateString += obj.value+".";
		}
		obj = findObj(FieldNames[i]+"_MONTH");
		if (obj) {
			dateString += obj.value+".";
		}
		obj = findObj(FieldNames[i]+"_YEAR");
		if (obj) {
			dateString += obj.value;
		}
		if (dateString.length > 0) {
			Dates[i].setDateString(dateString, separator, "DD.MM.YYYY");
		}
	}
	obj = findObj("MONTH_OFFSET");
	if (obj) {
		monthOffset = parseInt(obj.value);
		if(isNaN(monthOffset )){
			obj.value = "";
			monthOffset = 0;
		}
	}
}
function processForm(output, validate, testType) { 
  if (validate == null) {
	  validate = true;
  }
  if (output == null) {
	  output = true;
  }
  if (testType == null) {
	  testType = type;
  }
  copyForm();
  var inputOk = true;
  var STARTDate = new dateObj();

  STARTDate.copyValues(Dates[STARTIndex]);

  var PRIDate = new dateObj();
  PRIDate.copyValues(Dates[PRIIndex]);

  var ISRDate = new dateObj();
  ISRDate.copyValues(Dates[ISRIndex]);

  var CHANGEDate = new dateObj();
  CHANGEDate.copyValues(Dates[CHANGEIndex]);
  var IFDate = new dateObj();
  IFDate.copyValues(Dates[IFIndex]);
  var answer = "";
  var errorMessage = "";
  var errors = new Array();
  testDate = new dateObj();
  if ((testType == "Calculator") && (STARTDate.isValidDate() == false)) {
	errors[errors.length] = getGUILabel("label-errorStartDate") + "\n";
	inputOk = false;
  }
  if ((testType == "Calculator") && (monthOffset == 0)) {
	errors[errors.length] = getGUILabel('label-CalculatorSummaryText2') + "\n";
	inputOk = false;
  }
  if ((testType != "Calculator") && (PRIDate.isValidDate() == false)) {
	errors[errors.length] = getGUILabel("label-errorPriorityDate") + "\n";
	inputOk = false;
  } 
  if (((testType == "Article19") || (testType == "Demand")) && (ISRDate.isValidDate() == false)) {
	errors[errors.length] = getGUILabel("label-errorISRDate") + "\n";
	inputOk = false;
  }
  if (testType == "Rule26bis") {
	if (CHANGEDate.isValidDate() == false) {
		errors[errors.length] = getGUILabel("label-errorPriorityChangeDate") + "\n";
		inputOk = false;
	}
	if (IFDate.isValidDate() == false) {
		errors[errors.length] = getGUILabel("label-errorIFDate") + "\n";
		inputOk = false;
	}
  }
  if (inputOk == false) {
	if ((validate) && (errors.length > 0)) {
		//errorMessage = "Invalid input in:\n";
		errorMessage = getGUILabel("label-errorPreamble") + "\n";
		for (i = 0; i < errors.length; i++) {
			errorMessage += errors[i];
		}
		
		errorMessage  = errorMessage.replace(/<br>/gi,"\n");
		errorMessage  = errorMessage.replace(/<br\/>/gi,"\n");
		alert(errorMessage);
	}
	return answer;
  }
	

	answer ="<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3 WIDTH='100%' ID='calcAnswerTable' CLASS='plain'><TR><TD VALIGN='TOP'>&nbsp;</TD><TD VALIGN='TOP' ALIGN='RIGHT'><B>"+getGUILabel('label-timeLimit')+"</B></TD></TR>\n";


	if (testType == "PCTDeadlines") {
		testDate.copyValues(PRIDate);
		testDate.addMonths(16);
		answer += "<TR><TD VALIGN='TOP'>"+ getGUILabel('label-PCTDeadlinesSummaryText1') +"</TD><TD NOWRAP VALIGN='TOP' ALIGN='RIGHT'>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
		testDate.copyValues(PRIDate);
		testDate.addMonths(18);
		testDate.goToThursday();
		answer += "<TR><TD VALIGN='TOP'>"+ getGUILabel('label-PCTDeadlinesSummaryText2')+"</TD><TD NOWRAP ALIGN='RIGHT' VALIGN='TOP'>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
		testDate.copyValues(PRIDate);
		testDate.addMonths(20);
		answer += "<TR><TD VALIGN='TOP' COLSPAN=2 ALIGN='LEFT'>"+getGUILabel('label-PCTDeadlinesSummaryText3')+"</TD></TR>\n";
		answer += "<TR><TD VALIGN='TOP' ALIGN='RIGHT' COLSPAN=2><TABLE CLASS='plain' BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD ALIGN='LEFT' VALIGN='TOP'>";
		answer += getGUILabel('label-PCTDeadlinesSummaryText4')+"&nbsp;&nbsp;&nbsp;</TD><TD VALIGN='TOP' NOWRAP ALIGN='RIGHT'>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
		testDate.copyValues(PRIDate);
		testDate.addMonths(30);
		answer += "<TR><TD VALIGN='TOP' ALIGN='LEFT'>"+getGUILabel('label-PCTDeadlinesSummaryText5')+"</TD><TD VALIGN='TOP' NOWRAP ALIGN='RIGHT'>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR></TABLE></TD></TR>\n";
	}

	else if (testType == "Rule26bis") {
		PRIDate.addMonths(16);
		CHANGEDate.addMonths(16);
		IFDate.addMonths(4);
		if (PRIDate.compare(CHANGEDate) == -1) {
			targetDate = PRIDate;
		}
		else {
			targetDate = CHANGEDate;
		}
		if (targetDate.compare(IFDate) == -1) {
		    targetDate = IFDate;
		}
		answer += "<TR><TD VALIGN='TOP'>"+ getGUILabel('label-Rule26bisSummaryText1') +"</TD><TD VALIGN='TOP' NOWRAP ALIGN='RIGHT'>";
		answer += targetDate.displayDate(calcLanguage)+"</TD></TR>\n";
	}

	else if ((testType == "Article19") || (testType == "Demand")) {
		if (testType == "Article19") {
			PRIDate.addMonths(16);
			ISRDate.addMonths(2);
			answer += "<TR><TD VALIGN='TOP' WIDTH='90%' VALIGN='TOP'>"+ getGUILabel('label-Article19SummaryText1') +"</TD>";
		}
		else if (testType == "Demand") {
			PRIDate.addMonths(22);
			ISRDate.addMonths(3);
			answer += "<TR><TD WIDTH='100%' VALIGN='TOP' COLSPAN=2>"+getGUILabel('label-DemandSummaryText1')+"</TD></TR><TR><TD WIDTH='90%' VALIGN='TOP'>"+getGUILabel('label-DemandSummaryText2')+"</TD>";
		}
		answer += "<TD VALIGN='TOP' ALIGN='RIGHT' NOWRAP>";
		targetDate = PRIDate;
		if (ISRDate.compare(PRIDate) == 1) {
			targetDate = ISRDate;
		}
		answer += targetDate.displayDate(calcLanguage);
		if (testType == "Article19") {
			answer += "</TD></TR><TR><TD>&nbsp;</TD><TD ALIGN='LEFT' NOWRAP>"+getGUILabel('label-Article19SummaryText2');
		}
		else if (testType == "Demand") {
			answer += "</TD></TR><TR><TD WIDTH='90%' VALIGN='TOP'>"+getGUILabel('label-DemandSummaryText3')+"</TD><TD ALIGN='RIGHT' NOWRAP VALIGN='TOP'>";
			PRIDate.addMonths(-3);
			answer += PRIDate.displayDate(calcLanguage);
		}
		answer +="</TD></TR>\n";
	}
	else if (testType == "Supplementary") {
		testDate.setDateValues(2007, 6, 1);
		if (PRIDate.compare(testDate) < 0) {
			answer += "<TR><TD COLSPAN=2 VALIGN='TOP'>"+getGUILabel('label-SupplementarySummaryText1')+"</TD></TR>";
      	}	
		else {
			testDate.copyValues(PRIDate);
			testDate.addMonths(19);
			answer += "<TR><TD VALIGN='TOP'>"+getGUILabel('label-SupplementarySummaryText2')+"</TD><TD VALIGN='TOP' ALIGN='RIGHT' NOWRAP>";
			answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
		}
	}
	else if (testType == "Withdrawal") {
		testDate.copyValues(PRIDate);
		testDate.addMonths(30);
		answer += "<TR><TD VALIGN='TOP'>"+getGUILabel('label-WithdrawalSummaryText1') +"</TD><TD VALIGN='TOP' ALIGN='RIGHT' NOWRAP>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
	}

	else if (testType == "Record") {
		testDate.copyValues(PRIDate);
		testDate.addMonths(30);
		answer += "<TR><TD>"+getGUILabel('label-RecordSummaryText1') +"</TD><TD VALIGN='TOP' ALIGN='RIGHT' NOWRAP>";
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
	}
	else if (testType == "Calculator") {
		answer += "<TR><TD VALIGN='TOP' COLSPAN=2 ALIGN='RIGHT' NOWRAP>";
		testDate.copyValues(STARTDate);
		testDate.addMonths(monthOffset);
		answer += testDate.displayDate(calcLanguage)+"</TD></TR>\n";
	}

	answer += "</TABLE>";
	if (output) {
		PopUp('TEXT', '&nbsp;', answer, "TextWindow", answerWidth);
	}
	else {
		return answer;
	}
}
function processSummary() {
	var summary = "";
	var answer;
	var i, j;
	for (i = 0; i < tabs.length; i++) {
		answer = processForm(false, false, tabs[i].tabName);
		if (answer.length > 0) {
			//summary += "<HR><B>"+tabs[i].headers[0]+"</B><BR><TABLE ID='summaryTable' CLASS='plain' BORDER=0 CELLSPACING=0 CELLPADDING=3>";
			summary += "<HR><B>"+getGUILabel('label-' + tabs[i].tabName + 'DateHeaders')+"</B><BR><TABLE ID='summaryTable' CLASS='plain' BORDER=0 CELLSPACING=0 CELLPADDING=3>";
			for (j = 0; j < tabs[i].dateFields.length; j++) {
				field = parseInt(tabs[i].dateFields.substr(j, 1));
				summary += '<TR><TD NOWRAP WIDTH="150">'+ getGUILabel('label-' + FieldNames[field]);
				//  Dates[field].desc);
				summary += '</TD><TD NOWRAP VALIGN="BOTTOM">'+Dates[field].displayDate(calcLanguage)+'</TD></TR>';
				if (tabs[field].tabName == "Calculator") {
					summary += "<TR><TD>&nbsp;</TD><TD> + "+monthOffset+" months</TD></TR>";
				}
			}
			summary += '</TABLE><BR>'+answer;
		}
	}
	if (summary.length > 0) {
		summary = '<P ALIGN="CENTER"><FONT SIZE="+1"><B>'+getGUILabel('label-buttonSummary')+'</B></FONT></P>'+ getGUILabel('label-SummaryText') +summary;
		PopUp('TEXT', '&nbsp', summary, "TextWindow", answerWidth);
	}
	else {
		alert(getGUILabel('label-errorNoDates'));
	}
}
function printSummary() {
processSummary();
window.print();
}
function makeForm(index) {
  type = tabs[index].tabName;
  output = '<FORM ACTION="" onSubmit="processForm();return false;" onReset="resetForm();return true;" name="frm" id="frm">\n';
  output += '<TABLE CELLSPACING=0 BORDER=0 CELLPADDING=1 WIDTH="'+mainTableWidth+'" ID="calcFormTable" CLASS="plain">\n<TR>\n<TD COLSPAN=8>\n';
  output += getGUILabel('label-' + tabs[index].tabName + 'TabExplanations');
  output += '</TD></TR>';
  output += '<TR><TD ROWSPAN=6 WIDTH=38%>&nbsp;</TD><TD COLSPAN=6>&nbsp;</TD><TD ROWSPAN='+(2+tabs[index].dateFields.length)+'WIDTH=15%>&nbsp;</TD></TR>';

  var field;
  output += '<TR><TD>&nbsp;</TD>';
  if(calcLanguage == 'JA' || calcLanguage == 'KO' || calcLanguage == 'ZH' ){  
     output += '<TD ALIGN="CENTER">' + getGUILabel('label-dateYYYY') + '</TD><TD></TD><TD ALIGN="CENTER">' + getGUILabel('label-dateMM') + '</TD><TD></TD><TD ALIGN="CENTER">' + getGUILabel('label-dateDD') + '</TD></TR>\n';
  }else{
     output += '<TD ALIGN="CENTER">' + getGUILabel('label-dateDD') + '</TD><TD></TD><TD ALIGN="CENTER">' + getGUILabel('label-dateMM') + '</TD><TD></TD><TD ALIGN="CENTER">' + getGUILabel('label-dateYYYY') + '</TD></TR>\n';
  }
  top.firstBox = '';
  for (i = 0; i < tabs[index].dateFields.length; i++) {
    field = parseInt(tabs[index].dateFields.substr(i, 1));
    output += '<TR><TD NOWRAP WIDTH=90%>';
    //output += FieldLabels[0][field];
    output += getGUILabel('label-' + FieldNames[field] );
    output += '</TD>';
    output += makeDateCell(field, 0, i)+makeDateCell(field, 1, i)+makeDateCell(field, 2, i);
    output +='</TR>\n';
  }
  if (tabs[index].tabName == "Calculator") {
    //output += '<TR><TD COLSPAN=6 ALIGN="RIGHT">'+ tabs[index].texts[0][1]+': <INPUT TYPE=TEXT SIZE=2 VALUE="';
    output += '<TR><TD COLSPAN=6 ALIGN="RIGHT">'+ getGUILabel('label-' + tabs[index].tabName + 'SummaryText2') +': <INPUT TYPE=TEXT SIZE=2 VALUE="';
    if (monthOffset != 0) {
      output += monthOffset;
    }
    output += '" NAME="MONTH_OFFSET"></TD><TD>&nbsp;</TD></TR>\n';
  }
  output += '<TR><TD VALIGN="TOP" COLSPAN=6 ALIGN="RIGHT" VALIGN="BOTTOM"><BR>\n<INPUT TYPE="SUBMIT" VALUE="'+getGUILabel('label-buttonCalculate')+'"></TD><TD VALIGN="BOTTOM"><INPUT TYPE="RESET" VALUE="'+getGUILabel('label-buttonReset')+'">';
  output += '</TD></TR></TABLE>\n';
  output += '<DIV NAME="TextWindow" ID="TextWindow" CLASS="popUp"><LAYER NAME="TextWindow" ID="TextWindow" pageX=200 pageY=340 WIDTH='+answerWidth+'>&nbsp;</LAYER></DIV>';
  output += '<TABLE CLASS="plain" WIDTH='+mainTableWidth+'><TR><TD ALIGN="LEFT"><A HREF="javascript:processSummary()">'+getGUILabel('label-linkSummary')+'</A> [<A HREF="javascript:printSummary()">'+getGUILabel('label-linkPrint')+'</A>]</TD></TR></TABLE>';
  output += '</FORM>';
  return output;
}
function makeDateCell(field, index, order) {
	output = '<TD NOWRAP VALIGN="BOTTOM"><INPUT TYPE="Text" NAME="';
	name = FieldNames[field]+"_"+DateCells[index];
	nextName = FieldNames[field]+"_"+DateCells[index+1];
	output += name;
	output += '" onKeyUp=\'goToNextWhenFull("'+name+'", "'+nextName+'");\' SIZE=';
	if ((DateCells[index] == "DAY") || (DateCells[index] == "MONTH")) {
		output += '2 MAXLENGTH=2';
	}
	else {
		output += '4 MAXLENGTH=4';
	}
	output += ' VALUE="';
	if ((DateCells[index] == "DAY") && (Dates[field].isValidDay())) {
		output += ""+Dates[field].day;
	}
	else if ((DateCells[index] == "MONTH") && (Dates[field].isValidMonth())) {
		output += ""+Dates[field].month;
	}
	else if ((DateCells[index] == "YEAR") && (Dates[field].isValidYear())) {
		output += ""+Dates[field].year;
	}
	output += '" onChange="PopUp(0);">';
	if (index < DateCells.length-2) {
		output += "</TD><TD><B>.</B>";
	}
	output += '</TD>\n';
	if (top.firstBox.length == 0) {
	  top.firstBox = name;
	}
	return output;
}
function selectBox(index) {
	obj = findObj(tabs[selectedTab].tabName+"Box");
	if (obj) {
		obj.className = "boxOff";
	}
	selectedBox = index;
	type = tabs[index].tabName;
	obj = findObj(type+"Box", self.document);
	if (obj) {
		obj.className = "boxOn";
	}
	PopUp('TEXT', 'NORMAL', makeForm(index), "calculatorContents");
	val = true;
	for (i = 0; i < tabs[index].dateFields.length; i++) {
		field = parseInt(tabs[index].dateFields.substr(i, 1));
		if (Dates[field].isValidDate() == false) {
			val = false;
			break;
		}
	}
	if (val == true) {
		processForm();
	}
	return false;
}

function selectTab(index) {
  obj = findObj(tabs[selectedTab].tabName+"Tab");
  if (obj) {
    obj.className = "tabOff";
  }
  selectedTab = index;
  type = tabs[index].tabName;
  obj = findObj(type+"Tab", self.document);
  if (obj) {
    obj.className = "tabOn";
    obj.blur();
  }
  PopUp('TEXT', 'NORMAL', makeForm(index), "calculatorContents");
  selectFirstBox();
  
  val = true;
  for (i = 0; i < tabs[index].dateFields.length; i++) {
    field = parseInt(tabs[index].dateFields.substr(i, 1));
    if (Dates[field].isValidDate() == false) {
      val = false;
      break;
    }
  }
  if (val == true) {
    processForm();
  }
  return false;
}

function resetForm() {
    PopUp(0);
	for (i = 0; i < Dates.length; i++) {
		Dates[i] = new dateObj();
	}
	monthOffset = 0;
	if (navType == "Box") {
		selectBox(selectedBox);
	}
	else {
		selectTab(selectedTab);
	}
}
function highlightTab(index, on) {
  if (index == selectedTab) {
    return;
  }
  obj = findObj(tabs[index].tabName+"Tab");
  if (obj) {
    if (on) {
      obj.className = "tabHighlight";
    }
    else {
      obj.className = "tabOff";
    }
  }
}
function goToNextWhenFull(objName, nextObjName) {
  obj = findObj(objName);
  if ((obj) && (obj.value)) {
    if (obj.value.length >= obj.maxLength) {
      obj = findObj(nextObjName);
      if (obj) {
	obj.focus();
	if (obj.value.length > 0) {
	  obj.value = '';
	  obj.select();
	}
      }
    }
  }
}
function selectFirstBox() {
  obj = findObj(firstBox);
  if (obj) {
    obj.focus();
    //obj.select();
  }
}
// window.onload=selectFirstBox;
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 }
}
addEvent(window, 'load', selectFirstBox);
