var clicktarget=true;
var win;
var hasFB=false;
var fbUserName='';
var url="http://"+location.host;
function getElById(id){
	return(document.getElementById==null) ?
   (document.all==null ? null : document.all[id]) : document.getElementById(id);
}





function getInputData(id){
	if(getElById(id)){
		return getElById(id).value;
	}
	
}
function getData(id){
	if(getElById(id)){
		return getElById(id).value;
	}
	
}
function getIntData(id){
	if(getElById(id)){
		return parseInt(getElById(id).value);
	}
	
}
function setData(id,val){
	if(getElById(id)){
		return getElById(id).value=val;
	}	
	
}
function setIntData(id,val){
	if(getElById(id)){
		return getElById(id).value=parseInt(val);
	}	
	
}

function formatZahl(zahl, k, fix,dec_point,thousands_sep) {

    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt(zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einfügen
    if(fix)    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }

	var sign = zahl < 0;
	if(sign) zahl = zahl.substring(1);
    idx = zahl.indexOf('.');
	// Nachkommastellen ermittlen
    if( idx == -1) idx = zahl.length;
    else neu = dec_point + zahl.substr(idx + 1, k);
    while(idx > 0)    {
        if(idx - 3 > 0)
        neu = thousands_sep + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    return (sign ? '-' : '') + neu;
}

function IsIntegerString(string){
  return string==parseInt(string)?true:false;
}

function IsFloatString(string){
 string=string.replace(/,/,'.');
 return string==parseFloat(string)?true:false;
}

function setFormAction(action,field,form){
	getElById(field).value=action;
	document.forms[form].submit();
	return false;
}


function resetForm(name){
	document.forms[name].reset();
}



function AddPCTTag(id){ 
	var dd = new Date();
	var ord = Math.round(Math.abs(Math.sin(dd.getTime()))*1000000000)%10000000;
	var fd_pct_src = new String("<scr"+"ipt src=\"http://adsfac.eu/pct_mx.asp?L="+id+"&source=js&ord="+ord+"\" type=\"text/javascript\"></scr"+"ipt>");
	document.write(fd_pct_src); 

}








function ValidateRegisterData(){
	HiddeData('loginmessages');
	error=false;
	var txt='';
	var first=getData('person_first');
	var last=getData('person_last');	
	var street=getData('street');
	var zip=getData('zip');	
	var city=getData('city');
	var country=getData('country');
	var gebdate=getData('date');	
	var username=getData('username');

	var pass1=getData('pass1');
	var pass2=getData('pass2');
  if(getElById('person_female').checked==false && getElById('person_male').checked==false){
  	error=true;
  	txt+='<li>Wählen Sie eine Anrede aus!</li>';
  }
	if(first==''){
  	error=true;
  	txt+='<li>Geben Sie Ihren Vornamen ein!</li>';		
	}
	if(last==''){
  	error=true;
  	txt+='<li>Geben Sie Ihren Nachnamen ein!</li>';		
	}
	if(street==''){
  	error=true;
  	txt+='<li>Geben Sie die Stassenbezeichnung ein!</li>';		
	}
	if(zip==''){
  	error=true;
  	txt+='<li>Geben Sie die PLZ ein!</li>';		
	}
	if(city==''){
  	error=true;
  	txt+='<li>Geben Sie den Ort ein!</li>';		
	}	
	if(country==''){
  	error=true;
  	txt+='<li>Wählen Sie ein Land aus!</li>';		
	}		
  if(!string2date(gebdate)){
  	error=true;
  	txt+='<li>Geben Sie ein gültiges Geburtsdatum ein!</li>';	  	
  }
	

	if(!validateEmail(username)){
  	error=true;
  	txt+='<li>Geben Sie eine gültige E-Mail Adresse ein!</li>';		
	}
	if(pass1=='' || pass2==''){
  	error=true;
  	txt+='<li>Geben Sie ein Passwort 2 x ein!</li>';				
	}
  if(pass1!='' && pass2!='' && pass1!=pass2){
  	error=true;
  	txt+='<li>Die Passwortwiederholung ist falsch!</li>';	  	
  }
  if(getElById('subscribe').checked==false){
  	error=true;
  	txt+='<li>Sie müssen die Teilnahmebedingungen akzeptieren um am Gewinnspiel teilnehmen zu können!</li>';  	
  	
  }

	if(error){
		setGlobaMessage('error-msg',txt,'loginmessages');
		
		return false;
	}  

  setData('registeraction','doregister');
	return true;
}

function ValidateAccountData(){
	HiddeData('loginmessages');
	error=false;
	var txt='';
	var first=getData('person_first');
	var last=getData('person_last');	
	var street=getData('street');
	var zip=getData('zip');	
	var city=getData('city');
	var country=getData('country');
	var gebdate=getData('date');	

	var email=getData('username');	

  if(getElById('person_female').checked==false && getElById('person_male').checked==false){
  	error=true;
  	txt+='<li>Wählen Sie eine Anrede aus!</li>';
  }
	if(first==''){
  	error=true;
  	txt+='<li>Geben Sie Ihren Vornamen ein!</li>';		
	}
	if(last==''){
  	error=true;
  	txt+='<li>Geben Sie Ihren Nachnamen ein!</li>';		
	}
	if(street==''){
  	error=true;
  	txt+='<li>Geben Sie die Stassenbezeichnung ein!</li>';		
	}
	if(zip==''){
  	error=true;
  	txt+='<li>Geben Sie die PLZ ein!</li>';		
	}
	if(city==''){
  	error=true;
  	txt+='<li>Geben Sie den Ort ein!</li>';		
	}	
	if(country==''){
  	error=true;
  	txt+='<li>Wählen Sie ein Land aus!</li>';		
	}		
  if(!string2date(gebdate)){
  	error=true;
  	txt+='<li>Geben Sie ein gültiges Geburtsdatum ein!</li>';	  	
  }
	

	if(!validateEmail(email)){
  	error=true;
  	txt+='<li>Geben Sie eine gültige E-Mail Adresse ein!</li>';		
	}



	if(error){
		setGlobaMessage('error-msg',txt,'loginmessages');
		
		return false;
	}  

  setData('registeraction','applyaccount');
	return true;
}


function ValidateUpload(){
	HiddeData('smallpromotionmessages');
	error=false;		
	var txt='';
	var title=getData('title');
	var image=getData('image');	
	if(title==''){
  	error=true;
  	txt+='<li>Geben Sie einen Titel ein!</li>';
  	getElById('title').focus();		
	}	
	if(image==''){
  	error=true;
  	txt+='<li>Wählen Sie ein Bild aus!</li>';
  	getElById('image').focus();		
	}	
	if(error){
		setGlobaMessage('error-msg',txt,'donemessages');
		return false;
	} 
	setData('registeraction','applyfile');	
	return true;
}


function ValidateForward(){
	HiddeData('smallpromotionmessages');
	error=false;	
	var txt='';
	var email=getData('email');
	var person=getData('person');
	if(person==''){
  	error=true;
  	txt+='<li>Geben Sie bitte einen Empfängernamen ein!</li>';
  	getElById('person').focus();		
	}	
	if(!validateEmail(email)){
  	error=true;
  	txt+='<li>Geben Sie bitte eine gültige E-Mail Adresse ein!</li>';	
  	getElById('email').focus();	
	}
	if(error){
		setSmallGlobaMessage('error-msg',txt)	;
		return false;
	}  	
  setFormAction('forwardpage','forwardaction','promotionforwardform');
	return true;
	
	
}

function CheckLogIn(){
	
	HiddeData('loginmessages');
	var uid=getData('userid');
	var passwd=getData('passwd');
	error=false;
	var txt='';
	if(uid==''){
		error=true;
		txt+='<li>Geben Sie Ihren Benutzernamen ein!</li>';
	}
	if(passwd==''){
		error=true;
		txt+='<li>Geben Sie Ihr Passwort ein!</li>';		
	}
	
	if(error){
		setGlobaMessage('error-msg',txt,'loginmessages');
		return false;
	}
	setData('loginaction','login');
	return true;
}

function DoLogOut(){
	
	setData('loginaction','logout');
	return true;	

	
	
		
}

function SetDatePicerDefault(obj,mode,defaultvalue){
	
	if(!mode){
		if(obj.value==defaultvalue){
			obj.value='';
		}
	}
	else{
		if(obj.value==''){
			obj.value=defaultvalue;
		}
	}
}



function checkTextLength(obj,len,lay){

	
	if(obj.value.length>len){
		obj.value=obj.value.substr(0,len)
	}
	d=getElById(lay);
	if(d){
		out=len-obj.value.length;
		d.innerHTML=out;
		
	}
	
}










function setGlobaMessage(mode,text,id){
	if(d=getElById(id)){
   	d.innerHTML='<ul class="'+mode+'">'+text+'</ul>';
		showData(id);
		
	}
	
}




function string2date(string) {

    if(!string) return false;
		string += '';
		string = string.replace(/[-\/]/g, '.'); // Trennzeichen normalisieren
		string = string.replace(/[^0-9.: ]/g, ''); // ungültige Zeichen entfernen
		string = string.replace(/ +/g, ' '); // doppelte Leerzeichen entfernen
		var uhr = string.split(" "); // Uhrzeit abtrennen
		var split = uhr[0].split(".");
		var day = parseInt(split[0], 10);
		var month = parseInt(split[1] || 0, 10);
		var year = parseInt(split[2] || 0, 10);
		if(isNaN(year)) year = getFullYear();
    var check = new Date(year, month - 1, day);
    if(uhr[1]) {
       var uhr = uhr[1].split(':');
       check.setHours( uhr[0] || 0 );
       check.setMinutes( uhr[1] || 0 );
       check.setSeconds( uhr[2] || 0 );

    }
    var day2 = check.getDate();
    var year2 = getFullYear(check);
    var month2 = check.getMonth() + 1;
    return ( year2 == year && month == month2 && day == day2 ) ? check    : false;

}




function getFullYear(date){
    if(!date) date = new Date();
    if(typeof date.getFullYear != 'undefined') return date.getFullYear();
    var year = date.getYear();
    if(year < 1000) year += 2000;
    return year;
}

function validateEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

   if(reg.test(address) == false) {
     
      return false;
   }
   return true;
}



function HiddeData(id){
	if(getElById(id)){
		getElById(id).style.display='none';
	}
}
function showData(id){
	if(getElById(id)){
		getElById(id).style.display='block';
	}	
}










function SetToDefaultRecipe(id){
	user=getData('loginuserid');
  if(window.XMLHttpRequest){
  	req=new XMLHttpRequest();
  }
  else if(window.ActiveXObject){
  	req=new ActiveXObject("Microsoft.XMLHTTP");
  }	

	var jetzt = new Date();


	path="SetPlanData.php?mode=setDefaultData&object="+id+'&user='+user+'&ser='+(jetzt.getTime());
  if(req){
   
    req.open("GET",path,true);
    req.send(null);
  }			
	
}



function PrintRecipe(url){
	if(win){
		win.close();
	}

 	url+='&promotionprint=true';
	win=window.open("/"+url,'NOEMfasten',"top=50, left=100, width=1000,height=600,scrollbars=yes,menubar=yes");
	if(win){
		win.focus();
	}
	return false;	
	
}

function HiddeData(id){
	if(getElById(id)){
		getElById(id).style.display='none';
	}
}
function showData(id){
	if(getElById(id)){
		getElById(id).style.display='block';
	}	
}


function getProductDetail(id,group,ret){ 
  
  
	switch(group){
		case '342908':
			HiddeData('group_11931788');
			showData('group_342908');
			getElById('group_11931788').className='molke';
			getElById('group_342908').className='original';
			getElById('group_12343803').className='classic disabled';
			getElById('group_11828651').className='vanilie disabled';			
		break;
		case '11931788':
			HiddeData('group_342908');
			showData('group_11931788');
			getElById('group_11931788').className='molke';
			getElById('group_342908').className='original';
			getElById('group_12343803').className='classic disabled';
			getElById('group_11828651').className='vanilie disabled';				
		break; 	
		case '11828651':
			HiddeData('group_12343803');
			showData('group_11828651'); 
			getElById('group_11931788').className='molke disabled';
			getElById('group_342908').className='original disabled';
			getElById('group_12343803').className='classic';
			getElById('group_11828651').className='vanilie';				 	 
		break;
		case '12343803':
			HiddeData('group_11828651');
			showData('group_12343803');
			getElById('group_11931788').className='molke disabled';
			getElById('group_342908').className='original disabled';
			getElById('group_12343803').className='classic';
			getElById('group_11828651').className='vanilie';				 	 
		break;
	}
  if(ret){
  	return;
  }
  showData('ajax');
  if(window.XMLHttpRequest){
  	req=new XMLHttpRequest();
  }
  else if(window.ActiveXObject){
  	req=new ActiveXObject("Microsoft.XMLHTTP");
  }	

	var jetzt = new Date();
   

	path='product?id='+id+'&AJAXREQUEST=true&ser='+(jetzt.getTime());
  if(req){
    req.onreadystatechange=renderProduct;
	  req.open("GET",path,true);
    req.send(null);
  }		
}

function renderProduct(){
	if(req.readyState==4){
    if(req.status==200){
    	text=req.responseText;
    	HiddeData('ajax');
    	if(text!='' && getElById('ingredient')){
    		getElById('ingredient').innerHTML=text;
    	}

    }
	}
	
}







