
//general_funcs sind von der datei weinrot.js getrennt zu halten, da general_funcs im gegensatz dazu ohne mootools arbeiten


/****************************************************************************
	Globale Hilfsvariablen
*****************************************************************************/

g_strFieldsMissingDE = "Nicht alle Pflichtfelder wurden ausgefüllt! \nPflichtfelder sind fett gekennzeichnet";
g_strFieldsMissingIT = "Non tutti i campi obbligatori sono stati compilati!\nI campi in grasseto devono essere compilati";
g_strFieldsMissingEN = "Not all mandatory fields are filled in!\nBold fields must be filled in";
g_strEmailWrongDE = "Bitte geben Sie eine gülige eMail-Adresse ein!";
g_strEmailWrongIT = "Prego inserisca un indirizzo eMail valido!";
g_strEmailWrongEN = "Please insert a valid email address!";
g_strPrivacyMissingDE  = "Bitte lesen und akzeptieren Sie die Datenschutzbestimmungen!";
g_strPrivacyMissingIT  = "Prego legga ed approvi l'informativa riguardante il trattamento dei dati personali!";
g_strPrivacyMissingEN  = "Please accept the data protection rules!";

/****************************************************************************
 Formularfelder überprüfen per Name nicht id
	Übergabe
		strLang			Gewünschte Sprache
	Rückgabe:
		true wenn alle Pflichtfelder ausgefüllt oder false => alert.
*****************************************************************************/

function CheckContactForm( strLang )
{
	// Vorhandensein alle Pflichtfelder prüfen
	bOK = true;
	bPrivacy = true;
	bEmail = true;
	// Feld auf das Focus gesetzt werden soll
	var strFocusFieldID = '';
	
	// Pflicht-Felder anlegen
	aDutyFields = new Array( 'Name', 'eMail', 'Anfrage' );
	
	for( var j = 0; j < aDutyFields.length; ++j ){
		if( document.getElementsByName( aDutyFields[ j ] )[0].value == '' ){
			// Feld fehlt!
			bOK = false;
			
			// Feld auf das Focus gesetzt werden soll
			if( strFocusFieldID == '' && document.getElementsByName( aDutyFields[ j ] )[0]){
				strFocusFieldID = aDutyFields[ j ];
			}
		}
	}
	
	// Checkbox prüfen
	if( bOK && document.getElementsByName( 'Privacy' )[0].checked != true ){
		bOK = false;
		bPrivacy = false;
		// Feld auf das Focus gesetzt werden soll
		strFocusFieldID = 'Privacy';
	}
	
	// Ggf. eMail-Adresse prüfen
	if( bOK ){
		var nPosAt = document.getElementsByName( 'eMail' )[0].value.indexOf( '@' );
		var nPosLastDot = document.getElementsByName( 'eMail' )[0].value.lastIndexOf( '.' );
		if(( document.getElementsByName( 'eMail' )[0].value != "" && ( nPosAt == -1 || 
			( nPosAt != -1 && nPosLastDot <= nPosAt + 1 ))) ||
			( document.getElementsByName( 'eMail' )[0].value=="" )){
			bOK = false;
			bEmail = false;
			// Feld auf das Focus gesetzt werden soll
			strFocusFieldID = 'eMail';
		}
	}

	if( !bOK ){
	
		// Passende Hinweise holen
		switch( strLang ){
		case 'it':
			if( !bPrivacy ){
				strAlert = g_strPrivacyMissingIT;
			}
			else if( !bEmail ){
				strAlert = g_strEmailWrongIT;
			}
			else{
				strAlert = g_strFieldsMissingIT;
			}
			break;
		case 'en':
			if( !bPrivacy ){
				strAlert = g_strPrivacyMissingEN;
			}
			else if( !bEmail ){
				strAlert = g_strEmailWrongEN;
			}
			else{
				strAlert = g_strFieldsMissingEN;
			}
			break;
		case 'es':
			if( !bPrivacy ){
				strAlert = g_strPrivacyMissingES;
			}
			else if( !bEmail ){
				strAlert = g_strEmailWrongES;
			}
			else{
				strAlert = g_strFieldsMissingES;
			}
			break;
		default:
			if( !bPrivacy ){
				strAlert = g_strPrivacyMissingDE;
			}
			else if( !bEmail ){
				strAlert = g_strEmailWrongDE;
			}
			else{
				strAlert = g_strFieldsMissingDE;
			}
			break;
		}
		
		// Hinweis ausgeben
		alert( strAlert );
		
		// Fokus auf fehlerhaftes Feld setzen
		if( strFocusFieldID != '' ){
			document.getElementsByName( strFocusFieldID )[0].focus( );
		}
		// Fehler
		return false;
	}
	else {
		// Alles OK
		return true;
	}
};

//########## EMailadresse verstecken. Defaultwert: info@weinrot.com

function HideEA( EMail, EMailText, StyleCSS, DomainText )
{
	var Domain;
	var EMail;
	if( DomainText=="" || DomainText==undefined )
		Domain="enovit.it";
	else
		Domain=DomainText;
	if( EMail=="" || EMail==undefined )
		EMail="info";
	if( StyleCSS=="" ){
		if( EMailText=="" )
			document.write( '<a href="mailto:'+EMail+'@'+Domain+'">'+EMail+'@'+Domain+'</a>');// -->
		else
			document.write( '<a href="mailto:'+EMail+'@'+Domain+'">'+EMailText+'</a>');// -->
	}
	else {
		if( EMailText=="" )
			document.write( '<a href="mailto:'+EMail+'@'+Domain+'"class="'+StyleCSS+'">'+EMail+'@'+Domain+'</a>');// -->
		else
			document.write( '<a href="mailto:'+EMail+'@'+Domain+'"class="'+StyleCSS+'">'+EMailText+'</a>');// -->
	}
}
//########## EMailadresse verstecken ENDE


/****************************************************************************
 Button deaktivieren, damit er nicht mehrmals geklickt werden kann.
	Übergabe
		strId			Id des Buttons.
		strImgSrc		Dateiname des Bildes, das angezeigt werden soll (ohne Pfad. Pfad bleibt gleich.)
	Rückgabe:
		nichts.
*****************************************************************************/

function DisableSubmitBtn ( strId, strImgSrc )
{
	document.getElementById(strId).disabled=true;
	img = document.getElementById(strId).src;
	
	nPos = img.lastIndexOf("/");
	
	img2 = img.substr(0,(nPos+1))+strImgSrc; 
	
	document.getElementById(strId).src=img2;
}

if(function_exists( 'addEvent' )){
	window.addEvent('domready', function(){
	if($('ts')){ // nur tester
	 var ConfirmBtn = $('checkout_confirmation').getElementById('send_btn');
	 ConfirmBtn.addEvent("click", function(e){
		new Event(e).stop();
		DisableSubmitBtn( 'send_btn', 'button_confirm_order_wait.gif' );
		var osCsid=document.getElementsByName("osCsid");
		osCsid=osCsid[0].value;
		
		new Request.JSON({
			url: "ajax/add_session.php?osCsid="+osCsid,
			method: 'post',
			data: { data : 'Auf Bestellbutton geklickt.'},
			noCache : true,
			//onSuccess: function(response)
			onComplete: function(response)
			{
				$( 'checkout_confirmation' ).submit();
			},
			onFailure: function( xhr ){
				alert('Error: '+xhr.status);
			}
		}).send();
		
		return false;
	 });
	} // nur tester
	});

}

function function_exists (func_name) {
    // Checks if the function exists  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/function_exists    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Steve Clay
    // +   improved by: Legaev Andrey
        // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: function_exists('isFinite');    // *     returns 1: true
    if (typeof func_name === 'string') {
        func_name = this.window[func_name];
    }
    return typeof func_name === 'function';
}

/****************************************************************************
	Popup-Fenster.
*****************************************************************************/

function ShowWindow( strURL, strName, strWndData )
{
	// Fenster öffnen
	if( strWndData == "" || strWndData == undefined ){
		Win = window.open( strURL, strName );
	}
	else {
		Win = window.open( strURL, strName, strWndData );
	}
	
	// Fokus setzen
	Win.focus( );
	
	return false;
};


/*///////////////////////////POPUPFENSTER///////////////////////////*/


//don't touch
function popImage(imageURL, imageTitle) {

PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

var AutoClose = true;	

    var imgWin = window.open("", "_popImage", "scrollbars=no,resizable=1,width=" + defaultWidth + ",height=" + defaultHeight + ",left=" + PositionX + ",top=" + PositionY);
    if (!imgWin) {
        return true;
    }
    imgWin.document.write("<html><head><title>" + imageTitle + "</title><script type=\"text/javascript\">\r\n" + "function resizeWinTo() {\r\n" + "if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }" + "var oH = document.images[0].height+30, oW = document.images[0].width+30;\r\n" + "if( !oH || window.doneAlready ) { return; }\r\n" + "window.doneAlready = true;\r\n" + "var x = window; x.resizeTo( oW + 200, oH + 200 );\r\n" + "var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\r\n" + "if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\r\n" + "else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\r\n" + "else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\r\n" + "if( window.opera && !document.childNodes ) { myW += 16; }\r\n" + "x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\r\n" + "var scW = screen.availWidth ? screen.availWidth : screen.width;\r\n" + "var scH = screen.availHeight ? screen.availHeight : screen.height;\r\n" + "if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\r\n" + "}\r\n" + "</script>" + "</head><body style=\"margin: 0px;\" onload=\"resizeWinTo();\"" + (AutoClose ? " onblur=\"self.close();\"" : "") + ">" + (document.layers ? "<layer left=\"0\" top=\"0\">" : "<div style=\"position:absolute; margin-top: 15px; margin-left: 15px; margin-right: 15px;\">") + "<a href=\"#\"  onclick=\"self.close();\"><img src=" + imageURL + " border=\"0\" alt=\"Loading image ...\" title=\"\" onload=\"resizeWinTo();\"></a>" + (document.layers ? "</layer>" : "</div>") + "</body></html>");
    imgWin.document.close();
    if (imgWin.focus) {
        imgWin.focus();
    }
    return false;
}

/*///////////////////////////POPUPFENSTER ENDE///////////////////////////*/
