﻿function sendApplication(applicationId) {
	if (window.confirm("Ali ste prepričani, da želite potrditi prijavni list? Potrditev pomeni, da se strinjate z izjavami na prijavnem listu! S pritiskom na »V redu« bodo podatki poslani na Sklad. V primeru, da še niste popolnoma zaključili s pripravo prijavnega lista pritisnite »Prekliči«.")) { 
		window.location = "?acx=Application.Send&aid=" + applicationId;
	}
}
function copyApplication(applicationId) {
	if (window.confirm("Ali res želite kopirati prijavni list?!")) { 
		window.location = "?acx=Application.Copy&aid=" + applicationId;
	}
}
function deleteApplication(applicationId) {
	if (window.confirm("Ali res želite izbrisati prijavni list?")) { 
		window.location = "?acx=Application.Delete&aid=" + applicationId;
	}
}
function deleteApplicationCost(applicationId, costId) {
	if (window.confirm("Ali res želite izbrisati strošek?")) { 
		window.location = "?acx=Application.DeleteCost&cid=" + costId + "&aid=" + applicationId;
	}
}
function deleteClaim(claimId) {
	if (window.confirm("Ali res želite izbrisati zahtevek?")) { 
		window.location = "?acx=Claim.Delete&cid=" + claimId;
	}
}
function deleteInvoice(invoiceId) {
	if (window.confirm("Ali res želite izbrisati račun?")) { 
		window.location = "?acx=Invoice.Delete&cid=" + invoiceId;
	}
}
function deletePayment(paymentId) {
	if (window.confirm("Ali res želite izbrisati plačilo?")) { 
		window.location = "?acx=Payment.Delete&cid=" + paymentId;
	}
}
function deleteCompany(companyId) {
	if (window.confirm("Ali res želite izbrisati podjetje in prijavne liste podjetja?!")) { 
		window.location = "?acx=DeleteCompanyAction&oid=" + companyId;
	}
}

function sumarizeInvestmentCosts() {
	
	var investmentCostsA = (document.getElementById('application.investmentCostsA')!=null)?getFieldValue(document.getElementById('application.investmentCostsA').value):0;
	var investmentCostsB = (document.getElementById('application.investmentCostsB')!=null)?getFieldValue(document.getElementById('application.investmentCostsB').value):0;
	var investmentCostsC = (document.getElementById('application.investmentCostsC')!=null)?getFieldValue(document.getElementById('application.investmentCostsC').value):0;
	var investmentCostsD = (document.getElementById('application.investmentCostsD')!=null)?getFieldValue(document.getElementById('application.investmentCostsD').value):0;
	var investmentCostsE = (document.getElementById('application.investmentCostsE')!=null)?getFieldValue(document.getElementById('application.investmentCostsE').value):0;
	var investmentCostsF = (document.getElementById('application.investmentCostsF')!=null)?getFieldValue(document.getElementById('application.investmentCostsF').value):0;
	var investmentCostsG = (document.getElementById('application.investmentCostsG')!=null)?getFieldValue(document.getElementById('application.investmentCostsG').value):0;
	var investmentCostsH = (document.getElementById('application.investmentCostsH')!=null)?getFieldValue(document.getElementById('application.investmentCostsH').value):0;
	var investmentCostsI = (document.getElementById('application.investmentCostsI')!=null)?getFieldValue(document.getElementById('application.investmentCostsI').value):0;
												
	var sum = 
		  parseFloat(investmentCostsA)
		+ parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)
		+ parseFloat(investmentCostsF)
		+ parseFloat(investmentCostsG)
		+ parseFloat(investmentCostsH)
		+ parseFloat(investmentCostsI);
	document.getElementById('application.investmentCostsTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}
function sumarizeFinancialConstruction() {
	var financialConstructionNoNationalFunds = getFieldValue(document.getElementById('application.financialConstructionNoNationalFunds').value);
	var financialConstructionSubvention = getFieldValue(document.getElementById('application.financialConstructionSubvention').value);
	var sum = 
		parseFloat(financialConstructionNoNationalFunds) + parseFloat(financialConstructionSubvention);
	document.getElementById('application.financialConstructionTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}


// ------------------------------------------------- P1

function sumarizeInvestmentCostsP1() {
	
	var investmentCostsA = getFieldValue(document.getElementById('application.investmentCostsA').value);
	var investmentCostsB = getFieldValue(document.getElementById('application.investmentCostsB').value);
	var investmentCostsC = getFieldValue(document.getElementById('application.investmentCostsC').value);
	var investmentCostsD = getFieldValue(document.getElementById('application.investmentCostsD').value);
	var investmentCostsE = (document.getElementById('application.investmentCostsE')!=null)?getFieldValue(document.getElementById('application.investmentCostsE').value):0;
	var investmentCostsF = (document.getElementById('application.investmentCostsE')!=null)?getFieldValue(document.getElementById('application.investmentCostsF').value):0;
	var sum = 
		  parseFloat(investmentCostsA)
		+ parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)
		+ parseFloat(investmentCostsF)
	document.getElementById('application.investmentCostsTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}
function enabledisableInvestmentCostFields(type) {
	if (type=='A.1' || type=='B.1') {
		document.getElementById('application.investmentCostsA').disabled = false;
		document.getElementById('application.investmentCostsB').disabled = false;
		document.getElementById('application.investmentCostsC').disabled = false;
		document.getElementById('application.investmentCostsD').disabled = false;
		if (document.getElementById('application.investmentCostsE')!=null) document.getElementById('application.investmentCostsE').disabled = false;
		document.getElementById('application.investmentCostsTotal').disabled = false;		
	} else if (type=='A.2' || type=='B.2') {
		document.getElementById('application.investmentCostsA').disabled = true;
		document.getElementById('application.investmentCostsA').value = 0;
		document.getElementById('application.investmentCostsB').disabled = true;
		document.getElementById('application.investmentCostsB').value = 0;
		document.getElementById('application.investmentCostsC').disabled = true;
		document.getElementById('application.investmentCostsC').value = 0;
		document.getElementById('application.investmentCostsD').disabled = true;
		document.getElementById('application.investmentCostsD').value = 0;
		if (document.getElementById('application.investmentCostsE')!=null) document.getElementById('application.investmentCostsE').disabled = true;
		if (document.getElementById('application.investmentCostsE')!=null) document.getElementById('application.investmentCostsE').value = 0;
		document.getElementById('application.investmentCostsTotal').disabled = true;
		document.getElementById('application.investmentCostsTotal').value = 0;
		sumarizeInvestmentCostsP1092010();
	} 
}
function sumarizeInvestmentCostsP1092010() {
	var companySize = '';
	if (document.getElementById('companySize').checked) companySize = 'MIKRO'; 
	else if (document.getElementById('companySize2').checked) companySize = 'MALO';
	else if (document.getElementById('companySize3').checked) companySize = 'SREDNJE';
	
	
	var lines = document.getElementsByName("application.guaranteeCreditLine");
	
	var selectedGuaranteeLine = '';
	
	for (i=0; i<lines.length; i++) {
		if (lines[i].checked) {
	    	selectedGuaranteeLine = lines[i].value; 
	    }
	}
	
	var investmentCostsA = getFieldValue(document.getElementById('application.investmentCostsA').value);
	var investmentCostsB = getFieldValue(document.getElementById('application.investmentCostsB').value);
	var investmentCostsC = getFieldValue(document.getElementById('application.investmentCostsC').value);
	var investmentCostsD = getFieldValue(document.getElementById('application.investmentCostsD').value);
	var investmentCostsE = (document.getElementById('application.investmentCostsE')!=null)? getFieldValue(document.getElementById('application.investmentCostsE').value):0;
	var investmentCostsF = (document.getElementById('application.investmentCostsF')!=null)? getFieldValue(document.getElementById('application.investmentCostsF').value):0;
	
	var sumA = 
		(parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)) / 9;
	var sum = 
		  parseFloat(investmentCostsA)
		+ parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)
	var sumTurnover = sum + parseFloat(investmentCostsF);
	
	var container = document.getElementById('maxValue');
	container.innerHTML = "max: " + (""+Math.round(sumA*100)/100).replace('.', ',') + " €";
	
	document.getElementById('application.investmentCostsTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
	document.getElementById('application.turnOverResourcesCostsTotal').value = (""+Math.round(sumTurnover*100)/100).replace('.', ',');
	
	var turnOverResourcesCostsTotal = getFieldValue(document.getElementById('application.turnOverResourcesCostsTotal').value);
	var investmentCostsTotal = getFieldValue(document.getElementById('application.investmentCostsTotal').value);
	if (sumA < investmentCostsA) alert('Znesek vrednosti zemljišča je previsok.');
	
	var financialConstructionBankCredit = getFieldValue(document.getElementById('application.financialConstructionBankCredit').value);
		
	if (selectedGuaranteeLine == 'B.1') {
		if (financialConstructionBankCredit > 1500000) {
			alert('Znesek kredita je previsok. Največji možni znesek je omejen na 1.500.000,00 EUR');
		}
	}
	
	if (selectedGuaranteeLine == 'A.1' || selectedGuaranteeLine == 'B.1') {
		if (sumA>0 && (investmentCostsF / investmentCostsTotal) > 0.3 || investmentCostsF > 200000) {
			alert('Znesek kredita je previsok. Največji možni znesek obratnih sredstev je omejen na 30% vrednosti kredita oziroma maksimalno 200.000,00 EUR');
		}
	} 
	
	if (selectedGuaranteeLine == 'A.2' || selectedGuaranteeLine == 'B.2') {
		if (companySize == 'MIKRO' || companySize == 'MALO') {
			if (financialConstructionBankCredit > 100000) {
				alert('Znesek kredita je previsok. Največji možni znesek je omejen na 100.000,00 EUR');
			}
		} else {
			if (financialConstructionBankCredit > 200000) {
				alert('Znesek kredita je previsok. Največji možni znesek je omejen na 200.000,00 EUR');
			}
		} 
	}
}

function sumarizeInvestmentCostsP1TIP() {
	var companySize = '';
	if (document.getElementById('companySize').checked) companySize = 'MIKRO'; 
	else if (document.getElementById('companySize2').checked) companySize = 'MALO';
	else if (document.getElementById('companySize3').checked) companySize = 'SREDNJE';
	
	
	var lines = document.getElementsByName("application.guaranteeCreditLine");
	
	var selectedGuaranteeLine = '';
	
	for (i=0; i<lines.length; i++) {
		if (lines[i].checked) {
	    	selectedGuaranteeLine = lines[i].value; 
	    }
	}
	
	var investmentCostsA = getFieldValue(document.getElementById('application.investmentCostsA').value);
	var investmentCostsB = getFieldValue(document.getElementById('application.investmentCostsB').value);
	var investmentCostsC = getFieldValue(document.getElementById('application.investmentCostsC').value);
	var investmentCostsD = getFieldValue(document.getElementById('application.investmentCostsD').value);
	var investmentCostsE = (document.getElementById('application.investmentCostsE')!=null)? getFieldValue(document.getElementById('application.investmentCostsE').value):0;
	var investmentCostsF = (document.getElementById('application.investmentCostsF')!=null)? getFieldValue(document.getElementById('application.investmentCostsF').value):0;
	
	var sumA = 
		(parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)) / 9;
	var sum = 
		  parseFloat(investmentCostsA)
		+ parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsC)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)
	var sumTurnover = sum + parseFloat(investmentCostsF);
	
	var container = document.getElementById('maxValue');
	container.innerHTML = "max: " + (""+Math.round(sumA*100)/100).replace('.', ',') + " €";
	
	document.getElementById('application.investmentCostsTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
	document.getElementById('application.turnOverResourcesCostsTotal').value = (""+Math.round(sumTurnover*100)/100).replace('.', ',');
	
	var turnOverResourcesCostsTotal = getFieldValue(document.getElementById('application.turnOverResourcesCostsTotal').value);
	var investmentCostsTotal = getFieldValue(document.getElementById('application.investmentCostsTotal').value);
	if (sumA < investmentCostsA) alert('Znesek vrednosti zemljišča je previsok.');
	
	var financialConstructionBankCredit = getFieldValue(document.getElementById('application.financialConstructionBankCredit').value);
		
	if (selectedGuaranteeLine == 'A.1') {
		if (financialConstructionBankCredit > 1500000) {
			alert('Znesek kredita je previsok. Največji možni znesek je omejen na 1.500.000,00 EUR');
		}
	}
	
	if (selectedGuaranteeLine == 'A.1') {
		if (sumA>0 && (investmentCostsF / investmentCostsTotal) > 0.3 || investmentCostsF > 200000) {
			alert('Znesek kredita je previsok. Največji možni znesek obratnih sredstev je omejen na 30% vrednosti kredita oziroma maksimalno 200.000,00 EUR');
		}
	} 
	
	if (selectedGuaranteeLine == 'A.2') {
		if (financialConstructionBankCredit > 200000) {
			alert('Znesek kredita je previsok. Največji možni znesek je omejen na 200.000,00 EUR');
		} 
	}
}


function sumarizeFinancialConstructionP1() {
	var financialConstructionOwnResources = getFieldValue(document.getElementById('application.financialConstructionOwnResources').value);
	var financialConstructionBankCredit = getFieldValue(document.getElementById('application.financialConstructionBankCredit').value);
	var financialConstructionOtherResources = getFieldValue(document.getElementById('application.financialConstructionOtherResources').value);
	var sum = 
		parseFloat(financialConstructionOwnResources) + parseFloat(financialConstructionBankCredit) + parseFloat(financialConstructionOtherResources);
	document.getElementById('application.financialConstructionTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}

function getFieldValue(value) {
	return (value!=null && value!='')?
				(value.replace('.','')).replace(',','.'):
				0;
}

function AJAXInteraction(url, callback) {

    var req = init();
    req.onreadystatechange = processRequest;
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () {
      // readyState of 4 signifies request is complete
      if (req.readyState == 4) {
	// status of 200 signifies sucessful HTTP call
        if (req.status == 200) {
          if (callback) callback(req.responseXML);
        }
      }
    }

    this.doGet = function() {
      // make a HTTP GET request to the URL asynchronously
      req.open("GET", url, true);
      req.send(null);
    }
}

function validateVatId() { 
    var vatId = document.getElementById("company.vatId"); 
    var url = "Controller?acx=Company.ValidateVatId&vatId=" + encodeURIComponent(vatId.value); 
    var ajax = new AJAXInteraction(url, validateCallbackVatId); 
    ajax.doGet(); 
}
function validateCallbackVatId(responseXML) {
    var mdiv = document.getElementById("company.vatIdNotice");
	var submitBtn = document.getElementById("register");
	
	var msg = responseXML.getElementsByTagName("valid")[0].firstChild.nodeValue;
	
    if (msg == "false") {
	   // set the style on the div to invalid
	   mdiv.className = "bp_invalid";
       mdiv.innerHTML = "Neznana davčna številka. Z vnosom lahko nemoteno nadaljujete.";
    } else {
	   // set the style on the div to valid
	   mdiv.className = "bp_valid";
       mdiv.innerHTML = "Veljavna davčna številka";
    }
}

function validateCompanyId() { 
    var vatId = document.getElementById("company.companyId"); 
    var url = "Controller?acx=Company.ValidateCompanyId&companyId=" + encodeURIComponent(vatId.value); 
    var ajax = new AJAXInteraction(url, validateCallbackCompanyId); 
    ajax.doGet(); 
}
function validateCallbackCompanyId(responseXML) {
	var mdiv = document.getElementById("company.companyIdNotice");
	var submitBtn = document.getElementById("register");
	
	var msg = responseXML.getElementsByTagName("valid")[0].firstChild.nodeValue;
	
    if (msg == "false") {
	   // set the style on the div to invalid
	   mdiv.className = "bp_invalid";
       mdiv.innerHTML = "Neznana matična številka";
    } else {
	   // set the style on the div to valid
	   mdiv.className = "bp_valid";
       mdiv.innerHTML = "Veljavna matična številka";
    }
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function sumarizeInvestmentCostsP4() {
	
	var investmentCostsA = getFieldValue(document.getElementById('application.investmentCostsA').value);
	var investmentCostsB = getFieldValue(document.getElementById('application.investmentCostsB').value);
	var investmentCostsC = getFieldValue(document.getElementById('application.investmentCostsC').value);
	document.getElementById('application.investmentCostsC').value = (""+Math.round((parseFloat(investmentCostsA) + parseFloat(investmentCostsB))*100)/100).replace('.', ',');
	
	var investmentCostsD = getFieldValue(document.getElementById('application.investmentCostsD').value);
	var investmentCostsE = getFieldValue(document.getElementById('application.investmentCostsE').value);
	document.getElementById('application.investmentCostsF').value = (""+Math.round((parseFloat(investmentCostsD) + parseFloat(investmentCostsE))*100)/100).replace('.', ',');
	var sum = 
		  parseFloat(investmentCostsA)
		+ parseFloat(investmentCostsB)
		+ parseFloat(investmentCostsD)
		+ parseFloat(investmentCostsE)
	document.getElementById('application.investmentCostsTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}

function sumarizeFinancialConstructionP4() {
	
	var financialConstructionOtherResources = getFieldValue(document.getElementById('application.financialConstructionOtherResources').value);
	var financialConstructionSubvention = getFieldValue(document.getElementById('application.financialConstructionSubvention').value);
	document.getElementById('application.financialConstructionJustifiedCostsTotal').value = (""+Math.round((parseFloat(financialConstructionSubvention) + parseFloat(financialConstructionOtherResources))*100)/100).replace('.', ',');
	
	var financialConstructionUnjustifiedCostsTotal = getFieldValue(document.getElementById('application.financialConstructionUnjustifiedCostsTotal').value);
	var sum = 
		  parseFloat(financialConstructionOtherResources)
		+ parseFloat(financialConstructionSubvention)
		+ parseFloat(financialConstructionUnjustifiedCostsTotal)
	document.getElementById('application.financialConstructionTotal').value = (""+Math.round(sum*100)/100).replace('.', ',');
}

function calculateAvgExportValue() {
	var directSalesOtherCountriesM1 = getFieldValue(document.getElementById('application.directSalesOtherCountriesM1').value);
	var directSalesOtherCountriesP3 = getFieldValue(document.getElementById('application.directSalesOtherCountriesP3').value);
	var sum = 
		  (parseFloat(directSalesOtherCountriesM1)
		+ parseFloat(directSalesOtherCountriesP3)) / 2
	document.getElementById('application.directSalesOtherCountriesAverageM1P3').value = (""+Math.round(sum*100)/100).replace('.', ',');
}

function calculateAvgAddedValueEmployees() {
	
	var addedValueEmployeesM1 = getFieldValue(document.getElementById('application.addedValueEmployeesM1').value);
	var addedValueEmployeesP3 = getFieldValue(document.getElementById('application.addedValueEmployeesP3').value);
	var addedValueM1 = getFieldValue(document.getElementById('application.addedValueM1').value);
	var addedValueP3 = getFieldValue(document.getElementById('application.addedValueP3').value);
	var percentage = ((parseFloat(addedValueM1)/parseFloat(addedValueEmployeesM1)) + (parseFloat(addedValueP3)/parseFloat(addedValueEmployeesP3))) / 2;
	
	document.getElementById('application.addedValueAverageM1P3').value = (""+Math.round(percentage*100)/100).replace('.', ',');
}

function calculateAvgAddedValue() {
	
	var addedValueEmployeesM1 = getFieldValue(document.getElementById('application.addedValueEmployeesM1').value);
	var addedValueEmployeesP3 = getFieldValue(document.getElementById('application.addedValueEmployeesP3').value);
	var addedValueM1 = getFieldValue(document.getElementById('application.addedValueM1').value);
	var addedValueP3 = getFieldValue(document.getElementById('application.addedValueP3').value);
	var sum = 
		  ((parseFloat(addedValueP3)/parseFloat(addedValueEmployeesP3)) - (parseFloat(addedValueM1)/parseFloat(addedValueEmployeesM1))) / Math.abs(parseFloat(addedValueM1)/parseFloat(addedValueEmployeesM1)) * 100
		
	document.getElementById('application.addedValueValueIncrease').value = (""+Math.round(sum*100)/100).replace('.', ',');
}

function check1000signs(fieldName) {
	if (document.getElementById(fieldName).value.length > 999) {
		alert('Preseženo je bilo maksimalno število znakov');
	}
}

function enableDisable(checkbox) {
		if (document.getElementById('application.marketOrientationB').checked && document.getElementById('application.marketOrientationC').checked) {
			alert('Izbrana je lahko le ena izmed vrednosti');
			document.getElementById('application.marketOrientationB').checked = false;
			document.getElementById('application.marketOrientationC').checked = false;
		}
}
