﻿// JScript File

// ========= Return element id =============================
function getEl(sId) {
	return (document.all ? document.all(sId) : (document.getElementById ? document.getElementById(sId) : null));
}




function AddToBasket(sForm) {
	//alert (document[ sForm ]);
	//document[ sForm ].action = "add_to_basket.aspx"
	document[sForm].ArtPrice.value = document[sForm].txtTotal.value;
	if (document[sForm].ArtUpload.value == 'True') {
		sFile = document[sForm].file1.value;
		if ((sFile != '') && (sFile.substring(sFile.length - 3, sFile.length) == 'jpg')) {

			document[sForm].ArtDesc.value = '[' + sFile.substring(sFile.lastIndexOf("\\") + 1, sFile.length) + ']';
			document[sForm].submit();
		}
		else {
			alert(aFormError[0]);
		}
	}
	else {
		document[sForm].submit();
	}
}

function CalculateAttributes(sForm, sTitle, sValue) {
	document[sForm].txtTotal.value = format_float(sValue);
	document[sForm].ArtDesc.value = '(' + sTitle + ')';
}

function CalculateCustom1(sForm, sValue) {

	nBasicPrice = parseFloat(document[sForm].txtBasicPrice.value);
	nSinglePrice = parseFloat(document[sForm].txtSinglePrice.value);
	nStartCount = parseFloat(document[sForm].txtStartCount.value);

	document[sForm].txtTotalPieces.value = nStartCount + parseFloat(sValue);
	document[sForm].txtTotal.value = format_float(nBasicPrice + (nSinglePrice * parseFloat(sValue)));

	document[sForm].ArtDesc.value = document[sForm].txtTotalPieces.value;

}


function brojkef(myfield, e) {
	var key, keychar;
	if (window.event) {
		key = window.event.keyCode;
		if (key == ','.charCodeAt(0)) window.event.keyCode = key = '.'.charCodeAt(0);
	} else if (e) {
		key = e.which;
	} else {
		return true;
	};
	keychar = String.fromCharCode(key);
	return ((("0123456789").indexOf(keychar) > -1) || (key == 0))
}


function format_float(x) {
	var ostatak = Math.round(100 * (x - Math.round(x - 0.5)));
	while ((ostatak + '').length < 2) { ostatak += '0'; }
	var cjelobrojno = Math.round(x - 0.5) + '';
	var broj = '';
	var nCount = 0;
	for (var i = cjelobrojno.length - 1; i >= 0; i--) {
		broj = cjelobrojno.substr(i, 1) + broj;
		++nCount;
		if ((nCount == 3) && (i > 0)) {
			broj = '.' + broj;
			nCount = 0;
		}
	}
	ostatak = (ostatak != '00') ? ostatak : '00';
	return broj + ',' + ostatak;
}


function checkOrder() {
	var sMissing = "";
	var theForm = document.formBuy;
	if (theForm.txtSenderIme.value == "") sMissing += "\n- " + aFormError[1];
	if (theForm.txtSenderPrezime.value == "") sMissing += "\n- " + aFormError[2];
	//if ( theForm.txtSenderEmail.value == "" ) sMissing += "\n- E-mail naručitelja";

	var x = theForm.txtSenderEmail.value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (!filter.test(x)) { sMissing += "\n- " + aFormError[3]; }

	if (theForm.txtSenderPhone.value == "") sMissing += "\n- " + aFormError[4];

	if (theForm.txtSenderAdress.value == "") sMissing += "\n- " + aFormError[5];
	if (theForm.txtSenderZIP.value == "") sMissing += "\n- " + aFormError[6];
	if (theForm.txtSenderCity.value == "") sMissing += "\n- " + aFormError[7];
	if (theForm.txtSenderCountry.value == "") sMissing += "\n- " + aFormError[8];

	if (theForm.txtRecName.value == "") sMissing += "\n- " + aFormError[9];
	if (theForm.txtRecAdress.value == "") sMissing += "\n- " + aFormError[10];

	if (isDate(theForm.txtShipmentDate.value, "d.M.yyyy")) {

		if ((compareDates(theForm.txtShipmentDate.value, "d.M.yyyy", theForm.txtAvailableDelivery.value, "d.M.yyyy") == 1) || ((compareDatesCustom1(theForm.txtShipmentDate.value, theForm.txtAvailableDeliveryTill.value) == 0) && (compareDates(theForm.txtShipmentDate.value, "d.M.yyyy", theForm.txtAvailableDeliveryTill.value, "d.M.yyyy") == 0))) {
			sMissing += "\n-----\n- " + aFormError[11] + " " + theForm.txtShipmentDate.value + ".";
		}
		else {
			if (checkValidDate(theForm.txtShipmentDate.value) == false) {
				sMissing += "\n-----\n- " + aFormError[11] + " " + theForm.txtShipmentDate.value + ".";
			}
		}

	}
	else {
		sMissing += "\n-----\n- " + aFormError[12];
	}


	if (theForm.txtShipmentFrom.value == "" || theForm.txtShipmentTill.value == "") {
		sMissing += "\n- " + aFormError[13];
	}
	else {

//		sToday = new Date();
//		sNewDate = sToday.getDate() + '.' + (sToday.getMonth() + 1) + '.' + sToday.getFullYear();
//		if (compareDatesCustom1(theForm.txtShipmentDate.value, sNewDate) == -1) {
//			MinValue = parseInt(theForm.txtAvailableDeliveryTime.value);
//		}
//		else {
//			MinValue = 10;
//		}

		// time delivery
		if (!checkExceptionDate()) {
			MinValue = parseInt(theForm.txtAvailableDeliveryTime.value);
			MaxValue = 20;
			TmpFirstValue = parseInt(theForm.txtShipmentFrom.value);
			TmpSecondValue = parseInt(theForm.txtShipmentTill.value);

// ---------------------------------------
// ZH - 03.02.2012.
//			if (!(((TmpFirstValue >= MinValue) && (TmpFirstValue <= MaxValue)) && ((TmpSecondValue >= TmpFirstValue) && (TmpSecondValue <= MaxValue))) || (TmpFirstValue == TmpSecondValue)) {
//				sMissing += "\n- " + aFormError[14] + " " + TmpFirstValue + " - " + TmpSecondValue + " " + aFormError[15] + ". (" + aFormError[17] + ")";
//			}
			if ((!((TmpFirstValue >= MinValue) && (TmpFirstValue <= MaxValue)) && ((TmpSecondValue >= TmpFirstValue) && (TmpSecondValue <= MaxValue))) || (TmpFirstValue > TmpSecondValue)) {
				sMissing += "\n- " + aFormError[14] + " " + TmpFirstValue + " - " + TmpSecondValue + " " + aFormError[15];
			}
			if (TmpFirstValue == TmpSecondValue) {
				sMissing += "\n- " + aFormError[17];
			}
// ---------------------------------------
		}
	}

	if (sMissing != "") {
		alert(aFormError[16] + '\n' + sMissing + '\n');
	}
	else {

		if (theForm.ddlPayment.value == '1') {

			theForm.action = '/order.aspx';
			theForm.target = '_self';
			theForm.submit();

		} else {

			theForm.action = '/order_virman.aspx';
			theForm.target = '_blank';
			theForm.submit();

		}

	}
}

function checkPayment() {
	var theForm = document.formBuy;
	if (theForm.ddlPayment.value == '2') {
		alert('Ukoliko plaćate virmanom potrebno je obavezno faksirati nam potvrdu o plaćanju\nNAJKASNIJE 4 SATA PRIJE TRAŽENOG PERIODA ISPORUKE!');
	}
}


function checkValidDate(dDate) {
	var bValid = true;

	var theForm = document.formBuy;
	var sFreeDays = theForm.txtFreeDays.value;
	var aDates = sFreeDays.split(", ");
	for (i = 0; i < aDates.length; i++) {
		if (compareDatesCustom1(dDate, aDates[i]) == -1) {
			bValid = false
		}
	}

	return (bValid);
	//alert(aDates.length);
}



function printOrder() {
	var theForm = document.forms['aspnetForm'];
	theForm.action = '/order_print.aspx';
	theForm.target = '_blank';
	theForm.submit();

	/*var winURL = '/order_print.aspx';
	var winIme = 'ImgZoom'
	var winAtributi =
	'width=670'       +
	',height=550'     +
	',toolbar=yes'     +
	',location=no'    +
	',directories=no' +
	',status=no'      +
	',menubar=no'     +
	',scrollbars=yes'  +
	',resizable=no'   ;

	var win = eval("window.open(winURL, winIme, winAtributi)");
	win.focus();*/
}



// ========= Check date exceptions =============================

// dates for exceptions
var aExceptionDates = new Array('14.2', '14.02');

function CheckValentinovo() {
	var theForm = document.formBuy;
	if (isDate(theForm.txtShipmentDate.value, "d.M.yyyy")) {

		// if date in exception list - chenge time period
		if (checkExceptionDate()) {
			getEl('periodStandard').style.display = 'none';
			getEl('periodValentinovo').style.display = 'block';
			setExceptionPeriods(); // ZH - 03.02.2012.
			fillTimePeriod();
		}
		else {
			getEl('periodStandard').style.display = 'block';
			getEl('periodValentinovo').style.display = 'none';
		}

// ---------------------------------------
// ZH - 03.02.2012.
		var minDeliveryTime = document.getElementById("earliestTime");
		var txtAvailableDeliveryTime = document.getElementById("txtAvailableDeliveryTime");
		//var txtShipmentFrom = document.getElementById("txtShipmentFrom");
		var today = new Date();
		var earliestTime = today.getHours() + 4;
		var date = new Date(getDateFromFormat(theForm.txtShipmentDate.value, 'd.M.yyyy'));
		today.setHours(0, 0, 0, 0);
		date.setHours(0, 0, 0, 0);

		if (today.getTime() == date.getTime()) {
			minDeliveryTime.innerHTML = (earliestTime + ":00");
			txtAvailableDeliveryTime.value = earliestTime;
		//	txtShipmentFrom.value = earliestTime;
		} else {
			minDeliveryTime.innerHTML = "10:00";
			txtAvailableDeliveryTime.value = 10;
		//	txtShipmentFrom.value = "10";
		}
// ---------------------------------------
	}
}


function checkExceptionDate(sNewDate) {
	if (sNewDate) {
		sNewDate = new Date(sNewDate);
		var selectedDateCheck = sNewDate.getDate() + '.' + (sNewDate.getMonth() + 1);
	}
	else {
		var theForm = document.formBuy;
		var selectedDate = theForm.txtShipmentDate.value;
		var selectedDateCheck = selectedDate.substring(0, selectedDate.lastIndexOf("."));
	}
	var dateIsException = false;

	// check for date exception
	for (i = 0; i < aExceptionDates.length; i++) {
		if (selectedDateCheck == aExceptionDates[i]) {
			dateIsException = true;
		}
	}
	return (dateIsException);
}


// ---------------------------------------
// ZH - 03.02.2012.
function setExceptionPeriods() {
	var theForm = document.formBuy;
	var today = new Date();
	var date = new Date(getDateFromFormat(theForm.txtShipmentDate.value, 'd.M.yyyy'));
	date.setHours(today.getHours(), today.getMinutes(), today.getSeconds(), today.getMilliseconds());
	var isToday = (today.getTime() == date.getTime());

	var ddlShipmentFromTill = document.getElementById("ddlShipmentFromTill");
	ddlShipmentFromTill.innerHTML = '';

	if ((today.getHours() < 8) || !isToday) {
		var option1 = document.createElement("OPTION");
		ddlShipmentFromTill.options.add(option1);
		option1.value = '8##12';
		option1.text = '8:00 - 12:00';
	}
	if ((today.getHours() < 12) || !isToday) {
		var option2 = document.createElement("OPTION");
		ddlShipmentFromTill.options.add(option2);
		option2.value = '12##16';
		option2.text = '12:00 - 16:00';
	}
	if ((today.getHours() < 15) || !isToday) {
		var option3 = document.createElement("OPTION");
		ddlShipmentFromTill.options.add(option3);
		option3.value = '16##20';
		option3.text = '16:00 - 20:00';
	}
	if ((today.getHours() >= 15) && isToday) {
		var option4 = document.createElement("OPTION");
		ddlShipmentFromTill.options.add(option4);
		option4.value = '';
		option4.text = '-';
	}
}
// ---------------------------------------

function fillTimePeriod() {
	var theForm = document.formBuy;
	var selectedTime = theForm.ddlShipmentFromTill.value;
// ---------------------------------------
// ZH - 03.02.2012.
//	var sToday = new Date();
//	if (checkExceptionDate(sToday)) {
//		if (sToday.getHours() > 7) // working hours
//		{
//			//if ( sToday.getHours() < 12  ) theForm.ddlShipmentFromTill.options[0] = null;
//			theForm.ddlShipmentFromTill.options[0] = null;
//			if ((sToday.getHours() > 11) && (sToday.getHours() < 16)) {
//				theForm.ddlShipmentFromTill.options[0] = null;
//				theForm.ddlShipmentFromTill.options[0] = null;
//			}
//		}
//	}
// ---------------------------------------
	theForm.txtShipmentFrom.value = selectedTime.substring(0, selectedTime.indexOf('##'));
	theForm.txtShipmentTill.value = selectedTime.substring(selectedTime.indexOf('##') + 2);
}
