﻿var msg_reset_form = 'Da li želite da izbrišete sve unete podatke?';
var msg_zero_iznos = 'Unesite količinu koju želite da naručite.\n';
var msg_maximalan_iznos = '';
var msg_form_error = 'Unesite podatke:\n\n';


function calcTotal(form) {
	var text = "";
	var total = 0.0;

	for (var i = 0; i < form.length; i++) {
		var el = form[i];
		if (el.nodeName == "INPUT" && el.name.substring(0,4) == "tot_") {
			total += get_field_value(el);
		}
	}
	set_field_value(getElement("ordertotal"), total);
}

function field_onfocus(obj) {
	var name = obj.name; if (!name) return false;
	if (name.substring(0,4) == "tot_") {
		// next focus...
		obj.blur();
	}
}

var firsttime = 1;

function field_onblur(obj) {
	var name = obj.name;
	if (!name) return false;

	if (name.substring(0,5) == "quan_") {
		var index = name.substring(5);
		var quan  = get_field_value(obj);
		var price = get_field_value(getElement("price_" + index));

		if (isNaN(quan) || quan < 0) {
			quan = 0;
			set_field_value(obj, 0);
		}

		set_field_value(getElement("tot_" + index), price * quan);

		calcTotal(obj.form); // recalc total.
	}
}


function field_click(obj) {
	var name = obj.name; if (!name) return false;
	if (name == "reset") {
		var willReset = confirm(msg_reset_form);
		return willReset;
	}
	if (name == "Submit") {
//		var obj;
//		obj = MM_findObj('agreecheck');
//		if (!obj) { return false; }
//		if (!obj.checked) { return false; }
//
		if ((obj = MM_findObj("_tsep")) != null) obj.value = local_decimal_tsep;
		if ((obj = MM_findObj("_comma")) != null) obj.value = local_decimal_comma;

//		var ret = validateform_2();
		var obj = MM_findObj("orderForm");
		var ret = validateForm(obj);
		return ret;
	}
	return false;
}

function form_submit() {
}


function validate_requirefield(objname) {
	var obj = MM_findObj(objname);
	if (obj == null) return false;
	var val = get_field_value(obj);
	if (obj == null) return false;

	if (val == null || val == false || val == "")
		return false;

	return true;
}

function get_locname(objname) {
	var obj = MM_findObj(objname);
	if (obj == null) return '';
	
	// debug_props(obj, 'obj');
	var val = obj.getAttribute('locname');
	if (val == null) return objname;
	else return val;
}

function validateform_2() {                     // Stara funkcija. Nova je validateForm
	var obj, val, errors = '';

	//var tt = get_locname('ime');
	//alert(tt);
	//return false;

	val = get_field_value(MM_findObj('ordertotal'));
	if (val == 0) errors += msg_zero_iznos;
//	if (val < 1000 && val != 0) errors += msg_minimalan_iznos;
//	if (val > 50000) errors += msg_maximalan_iznos;

	if (validate_requirefield('ime') == false) errors += '• ' + get_locname('ime') + '\n';
	if (validate_requirefield('prezime') == false) errors += '• ' + get_locname('prezime') + '\n';
	if (validate_requirefield('ulicaibroj') == false) errors += '• ' + get_locname('ulicaibroj') + '\n';
	// email

/*
	if ((obj = MM_findObj('email')) && (val = get_field_value(obj)) != null) {
		if (val == null || val == false || val == "") errors += '• ' + get_locname('email') + '\n';
		else {
			var p = val.indexOf('@');
        		if (p<1 || p==(val.length-1))
				errors += '• ' + get_locname('email') + '\n';
		}
	}
*/
	if (validate_requirefield('mesto') == false) errors += '• ' + get_locname('mesto') + '\n';
	if (validate_requirefield('postanskibroj') == false) errors += '• ' + get_locname('postanskibroj') + '\n';
	if (validate_requirefield('email') == false) errors += '• ' + get_locname('email') + '\n';

	if (errors != '') alert(msg_form_error + errors);

	return (errors == '');
}



// the validateForm() function is the brain of the form-validation code;
// you'll have to customize it to fit your form-field needs!

function validateForm(form)
  {

  val = get_field_value(MM_findObj('ordertotal'));
  if (val == 0)
    {
    alert("Unesite količinu koju želite da naručite.");
    return false;
    }


  form.ime.value = stripLeadingTrailingBlanks(form.ime.value);
  if (isBlank(form.ime.value))
    {
    alert("Unesite ime.");
    form.ime.focus();
    return false;
    }

  form.prezime.value = stripLeadingTrailingBlanks(form.prezime.value);
  if (isBlank(form.prezime.value))
    {
    alert("Unesite prezime.");
    form.ime.focus();
    return false;
    }

  form.email.value = stripLeadingTrailingBlanks(form.email.value);
  if (!isEmail(form.email.value))
    {
    alert("Unesite svoju e-mail adresu.");
    form.email.focus();
    return false;
    }

  form.ulicaibroj.value = stripLeadingTrailingBlanks(form.ulicaibroj.value);
  if (isBlank(form.ulicaibroj.value))
    {
    alert("Unesite ulicu i broj, gde će pošiljka biti isporučena.");
    form.ulicaibroj.focus();
    return false;
    }

  form.postanskibroj.value = stripLeadingTrailingBlanks(form.postanskibroj.value);
  if (!isInteger(form.postanskibroj.value))
    {
    alert("Unesite poštanski broj.");
    form.postanskibroj.focus();
    return false;
    }

  form.mesto.value = stripLeadingTrailingBlanks(form.mesto.value);
  if (isBlank(form.mesto.value))
    {
    alert("Unesite ime mesta.");
    form.mesto.focus();
    return false;
    }

//  form.telefon.value = stripLeadingTrailingBlanks(form.telefon.value);
//  if (isBlank(form.telefon.value))
//    {
//    alert("Unesite kontakt telefon.");
//    form.telefon.focus();
//    return false;
//    }

//  isOK = false;

//	formEl = form.kolicina40;
//	formEl.value = stripLeadingTrailingBlanks(formEl.value);
//	if (isInteger(formEl.value)) isOK = true;

//  if (!isOK)
//    {
//    alert("Unesite količinu koju želite da naručite.");
//    form.kolicina40.focus();
//    return false;
//    }


  return true;  // form valid - submit to ACTION URL
  }


