/* Inhalt der EingabeFelder bei Selbsteintragung prÃ¼fen, Pflichtfelder dÃ¼rfen nicht leer sein, Paare PLZ Ort mÃ¼ssen beide vorhanden sein */
 function isEmpty(field, label, cssclass)
 {
    var retMessage = "";
    if (document.getElementById(field).value == "") {
       document.getElementById(label).className=cssclass;
       document.getElementById(field).focus();
       retMessage = " Fehler in Feld " + field;
    }
    return retMessage;
 }
 function bothEmpty(field1, field2, label1, label2, cssclass)
 {
    var retMessage = "";
    var field1AndField2 = document.getElementById(field1).value + document.getElementById(field2).value;
    if (field1AndField2 != "") {
      if (document.getElementById(field1).value == "") {
         document.getElementById(label1).className=cssclass;
         document.getElementById(field1).focus();
         retMessage = " Fehler in Feld " + field1;
      }
      if (document.getElementById(field2).value == "") {
         document.getElementById(label2).className=cssclass;
         document.getElementById(field2).focus();
         retMessage = " Fehler in Feld " + field2;
      }
    }
    return retMessage;
 }
 function isCheckSelected(field, label, cssclass)
 {
    var retMessage = "";
    if (!document.getElementById(field).checked) {
       document.getElementById(label).className=cssclass;
       document.getElementById(field).focus();
       retMessage = " Fehler in Feld " + field;
    }
    return retMessage;
 }
 function isSelected(field, label, value, cssclass)
 {
    var retMessage = "";
    if (document.getElementById(field).selectedIndex == value) {
       document.getElementById(label).className=cssclass;
       document.getElementById(field).focus();
       retMessage = " Fehler in Feld " + field;
    }
    return retMessage;
 }


 function checkForm()
 {
    var retValue = true;
    var retMessage = "";
    retMessage += isEmpty("Firma", "FirmaLabel", "fehlt");
    retMessage += isEmpty("Strasse", "StrasseLabel", "fehlt");
    retMessage += isEmpty("Nr", "NrLabel", "Etikett2schmal fehlt");
    retMessage += isEmpty("PLZ", "PLZLabel", "fehlt");
    retMessage += isEmpty("Ort", "OrtLabel", "Etikett2schmal fehlt");
    retMessage += isEmpty("Vorwahl1", "Vorwahl1Label", "fehlt");
    retMessage += isEmpty("Durchwahl1", "Durchwahl1Label", "Etikett2schmal fehlt");
    /*
    retMessage += bothEmpty("ZEPLZ01", "ZEOrt01", "ZEPLZ01Label", "ZEOrt01Label", "fehlt");
    retMessage += bothEmpty("ZEPLZ02", "ZEOrt02", "ZEPLZ02Label", "ZEOrt02Label", "fehlt");
    retMessage += bothEmpty("ZEPLZ03", "ZEOrt03", "ZEPLZ03Label", "ZEOrt03Label", "fehlt");
    retMessage += bothEmpty("ZEPLZ04", "ZEOrt04", "ZEPLZ04Label", "ZEOrt04Label", "fehlt");
    retMessage += bothEmpty("ZEPLZ05", "ZEOrt05", "ZEPLZ05Label", "ZEOrt05Label", "fehlt");
    */
    retMessage += isSelected("Anrede", "AnredeLabel", 0, "fehlt");
    retMessage += isEmpty("Vorname", "VornameLabel", "fehlt");
    retMessage += isEmpty("Nachname", "NachnameLabel", "fehlt");
    retMessage += isEmpty("EMail2", "EMail2Label", "fehlt");

    retMessage += isCheckSelected("AGBs", "AGBLabel", "fehlt");


    if (retMessage != "") {
       document.getElementById('error').style.display="block";
       retValue = false;
    }

    //alert("return: " + retValue + retMessage);
    return retValue;
 }

/* Klick auf Auftrag protokollieren */
function writeOrderClick(execname, qid, anztyp, inserent)
{
 var url = execname + "&DIS_" + qid + "_auftraglog3&INSERENT=" + inserent + "&ANZTYP=" + anztyp;
 var fenster = window.open(url,'auftraglog','width=1,height=1');
 fenster.close();
}

/* Inhalt der EingabeFelder prÃ¼fen, mindestens 1 Zeichen muss eingegeben sein */
function checkInput()
{
 var checkstring = "";
 if (document.forms[0].F0 != null)
     checkstring += document.forms[0].F0.value;
 if (document.forms[0].F1 != null)
     checkstring += document.forms[0].F1.value;
 if (document.forms[0].F2 != null)
     checkstring += document.forms[0].F2.value;
 if (document.forms[0].F3 != null)
     checkstring += document.forms[0].F3.value;
 if (document.forms[0].F4 != null)
     checkstring += document.forms[0].F4.value;
 if (document.forms[0].F5 != null)
     checkstring += document.forms[0].F5.value;
 if (document.forms[0].F6 != null)
     checkstring += document.forms[0].F6.value;
 if (checkstring.length < 2)
 {
    alert ("Bitte geben Sie mindestens 2 Zeichen ein!");
    return false;
 }
}

/* Inhalt der EingabeFelder sichern */
function saveInput()
{
 var searchstring = "&F0=";
 var match = "&S=1";
 if (document.forms[0].F1.value != "")
     searchstring += document.forms[0].F1.value + "+";
 if (document.forms[0].F2.value != "")
     searchstring += document.forms[0].F2.value + "+";
 if (document.forms[0].F3.value != "")
     searchstring += document.forms[0].F3.value + "+";
 if (document.forms[0].F4.value != "")
     searchstring += document.forms[0].F4.value + "+";
 if (document.forms[0].F5.value != "")
     searchstring += document.forms[0].F5.value + "+";
 if (document.forms[0].F6.value != "")
     searchstring += document.forms[0].F6.value + "+";
 for (i=0;i<document.links.length;i++)
 {
 if (document.links[i].search.indexOf(match) != -1)
     document.links[i].search += searchstring;
 }
}
//  Fokus setzen, nicht nach Bestellung von CDROM
function setFocus()
{
 if (document.forms[0] != null)
 {
  if (document.forms[0].F0 != null)
     document.forms[0].F0.focus();
  else
   if (document.forms[0].F1 != null)
       document.forms[0].F1.focus();
 }
}
// F1 bei Ã„hnlichkeitsSuche speichern und wieder herstellen
function saveF1()
{
 if (document.forms[0] != null)
 {
  if (document.forms[0].F1Save != null)
     document.forms[0].F1Save.value = document.forms[0].F1.value;
 }
}
function restoreF1()
{
 if (document.forms[0] != null)
 {
  if (document.forms[0].F1Save != null)
     document.forms[0].F1.value = document.forms[0].F1Save.value;
 }
}


// --- --- --- //
// --- Formularberechnung beim Ausfuellen --- //

c_cart_manager = Class.create({

//	initialize
initialize: function() {
	// settings
	this._form_elmt= $$('form')[0];
	this._searchterm_input_classname= 'cart-searchterm';
	this._city_input_classname= 'cart-city';
	this._zip_input_classname= 'cart-zip';

	this._base_amount= 9.95;
	this._amount_per_additional_city= 9.95;
	this._amount_per_additional_searchterm= 4.95;

	this._base_sum_elmt= $('SummeBasis');
	this._additional_city_sum_elmt= $('Summe1');
	this._additional_searchterm_sum_elmt= $('Summe2');
	this._total_sum_elmt= $('Summe3');
	this._total_sum2_elmt= $('EndsummeBestaetigen');

	// system variables
	this._searchterm_elmts = $$('input.'+this._searchterm_input_classname);
	this._city_elmts = $$('input.'+this._city_input_classname);
	this._zip_elmts = $$('input.'+this._zip_input_classname);
	
	// initialize
	this.set_event_handlers();
	this.show_submit_button();
},

//	set event handlers
set_event_handlers: function() {
	this._searchterm_elmts.each(function(a, b) {
		a.observe('blur', function() { this.update_cart(); }.bind(this));
		this._city_elmts[b].observe('blur', function() { this.update_cart(); }.bind(this));
		this._zip_elmts[b].observe('blur', function() { this.update_cart(); }.bind(this));
	}.bind(this));
	this.update_cart();
},

//	update cart
update_cart: function() {
	this._total_base = 0;		
	this._total_additional_cities = 0;
	this._total_additional_searchterms = 0;
	this._total = 0;
	
	// search terms
	this._searchterm_elmts.each(function(a, b) {
		if(!a.value.blank()) {
			this._total_additional_searchterms += this._amount_per_additional_searchterm;
		}
	}.bind(this));
	
	// cities
	this._city_elmts.each(function(a, b) {
		if(!a.value.blank() || !this._zip_elmts[b].value.blank()) {
			this._total_additional_cities += this._amount_per_additional_city;
		}
	}.bind(this));
	
	this._total_base = this._base_amount;
	this._total = this._total_base + this._total_additional_cities + this._total_additional_searchterms;
	
	this._base_sum_elmt.value = this.format_currency(this._total_base);
	this._additional_city_sum_elmt.value = this.format_currency(this._total_additional_cities);
	this._additional_searchterm_sum_elmt.value = this.format_currency(this._total_additional_searchterms);
	this._total_sum_elmt.value = this.format_currency(this._total);
	this._total_sum2_elmt.update(this._total_sum_elmt.value);
},

//	format currency
format_currency: function(number) {
	i = parseFloat(number);
	if(isNaN(i)) { i = 0.00; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = s.replace(/\./, ',');
	return s;
},

//	show submit button
show_submit_button: function() {
	$('Bestellen').setStyle('display: block !important;');
	$$('p.BitteWarten')[0].hide();	
}

});

//	ON DOM:LOADED //
document.observe('dom:loaded', function() {
	// cart manager
	cart_manager = new c_cart_manager();
});

// --- --- --- //


