// JavaScript Document

function init() {	
	ClearInputEvents('newsletter_name');
	ClearInputEvents('newsletter_email');
    swfobject.embedSWF("swf/flashdemo.swf", "swf", "940", "600", "9.0.0");
}


function change_text(text1) {
  //document.getElementById('title-article').innerHTML = text1;
  document.getElementById('seizoen').innerHTML = text1; 
} 

// make_ACTIVE | make_INACTIVE functions
function makeInActive() {
    document.getElementById('frm_biladress').className = 'inactive';
    document.getElementById('billing').checked = false;
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_billing) { f[i].disabled = true; }
    }
}

function makeActive() {
    document.getElementById('frm_biladress').className = 'active';
    document.getElementById('billing').checked = true;
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_billing) { f[i].disabled = false; }
    }
}

function makeEcardInActive() {
    /*document.getElementById('frm_ecard').className = 'inactive_ecard';
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if (i == 22 | i == 23) { f[i].disabled = true; }
    }*/
}

function makeEcardActive() {
    /*document.getElementById('frm_ecard').className = 'active_ecard';
    f = document.getElementById('frm_ecard');
    for(i=0 ; i < f.length; i++) { 
      if (i == 22 | i == 23) { f[i].disabled = false; }
    }*/
}

function makeSchoolInActive() {
    document.getElementById('frm_school').className = 'inactive_school';
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_school) f[i].disabled = true;
    }
}

function makeSchoolActive() {
    document.getElementById('frm_school').className = 'active_school';
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_school) f[i].disabled = false;
    }
}

function makePaymentInActive() {
    document.getElementById('subpayment_if').className = 'inactive_payment';
    f = document.getElementById('frm_subpayment');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_subpayment) f[i].disabled = true;
    }
}

function makePaymentActive() {
    document.getElementById('subpayment_if').className = 'active_payment';
    f = document.getElementById('frm_subpayment');
    for(i=0 ; i < f.length; i++) { 
      if (f[i].showhide_subpayment) f[i].disabled = false;
    }
}

function makeCurrentInActive() {
    document.getElementById('frm_current').className = 'start inactive_current';
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if ( f[i].showhide_current ) { f[i].disabled = true; }
    }
    document.getElementById('start1').disabled = true;
    document.getElementById('start2').disabled = true;
}

function makeCurrentActive() {
    document.getElementById('frm_current').className = 'start active_current';
    f = document.getElementById('frm_adress');
    for(i=0 ; i < f.length; i++) { 
      if ( f[i].showhide_current ) { f[i].disabled = false; }
    }
    document.getElementById('start1').disabled = false;
    document.getElementById('start2').disabled = false;
}

// showhide functions
function showhidePayment() {
  if(document.getElementById('payment2').checked) {
    makePaymentActive();
  } else {
    makePaymentInActive();
  }
}

function showhideBilling() {
  if(document.getElementById('billing').checked) {
    makeActive();
  } else {
    makeInActive();
  }
}

/*
Bepaald de onload, functie toevoegen aan body onload d.m.v.: window.onload.actions.push(function_naam);
*/
window.onload = function () {
    for (var i=0;arguments.callee.actions.length>i;i++)
        arguments.callee.actions[i]();
};
window.onload.actions = [];

function init_logo(){
	if(window.logo_ie6) logo_ie6('<?=$logo_bgcolor?>', '<?=$logo_color?>');
}

var page = 0;
var limit = 4;

function init_products() {
	if(document.getElementById('form_subscrip')) {
		update_back_issues();
		update_product_cart();
	}
}

function next_allowed(){
	var rbs = $('form_cart').shipment;
	for(var i =0; i < rbs.length; i++){
		if(rbs[i].checked){
			return true;
		}
	}
	alert('Please select shipment to');
	return false;
}

function update_product_cart() {
	var post_body = Form.serialize($('form_subscrip'));
	new Ajax.Updater('product_cart', '/ajax/cart.ajax.php', { method:'post', postBody: post_body, asynchronous:true, evalScripts:true, onComplete: cart_check_as_radio} );
}

function update_aantal_per_product(product_id, plus_min) {
	post_body = 'mag_id='+product_id+'&plus_min='+plus_min;
	//product_id;
	//plus_min;
	new Ajax.Updater('product_cart', '/ajax/cart.ajax.php', { method:'post', postBody: post_body, asynchronous:true, evalScripts:true, onComplete: cart_check_as_radio } );
}

function update_aantal_per_offer(offer_id, plus_min) {
	
	post_body = 'aanbieding[id]='+offer_id+'&aanbieding[aantal]='+plus_min;
	new Ajax.Updater('product_cart', '/ajax/cart.ajax.php', { 
		method:'post', 
		postBody: post_body, 
		asynchronous:true, 
		evalScripts:true, 
		onComplete: cart_check_as_radio 
	});
}

function update_shipment(shiment_to_id) {
	new Ajax.Updater('product_cart', '/ajax/cart.ajax.php', { 
		method:'post', 
		postBody: $('form_cart').serialize(), 
		asynchronous:true, 
		evalScripts:true, 
		onComplete: cart_check_as_radio
	});
}

function update_back_issues(dir, total) {
	if(dir=='up') page++;
	if(dir=='down') page --;

	if(page<0) { page=0; return false; }

	if((page + limit) >= total) { page--; return false; }

	var post_body = Form.serialize($('form_subscrip'));
	post_body += '&pagina='+page+'&limit='+limit;
	
	update_products(post_body);
	
}

function update_latest_issues(nummer, page, limit) {
	if(page=='') { page=0;}
	if(limit=='') { limit=4;}	
	if(page<0) { page=0;}
	
	var post_body = Form.serialize($('form_subscrip'));
	post_body += '&pagina='+page+'&limit='+limit+'&number='+nummer;
	update_products(post_body);
}

function update_products(post_body) {
	new Ajax.Updater('back_issues', '/ajax/products.ajax.php', { method:'post', postBody: post_body, asynchronous:true, evalScripts:true, onComplete:function(){ cart_check_as_radio(); update_product_cart(); } } );
}

function update_aantal(dir, id) {
	alert(dir + "sdfsdfsdf " +id)
	var aantal;
	var aantal_veld = document.getElementById(id);
	if(aantal_veld.value != "") {
		aantal = aantal_veld.value
	}
	else aantal = 1;
	if(dir=='up') aantal++;
	if(dir=='down') aantal--;
	
	if(aantal<1) { aantal=1; return false; }
	alert("aantal  " +aantal)
	document.getElementById(id).value = aantal;
}

/*
 Voor de Nieuwsbrief Aanmeld form
*/

function change_subscription(what) {
	var link = 'http://mail.copernica.nl/web'+what+'.php';
	var newsletter = document.getElementById('newsletter');
	//alert(link);
	newsletter.action = link;
	newsletter.elements['send'].value = what;
	//alert(newsletter.action);
	
	if (what == 'subscribe') {
		newsletter.elements['redirect'].value = 'http://www.foammagazine.nl/index.php?pageId=10&msg=p10m01';
	}
	if (what == 'unsubscribe') {
		newsletter.elements['redirect'].value = 'http://www.foammagazine.nl/index.php?pageId=10&msg=p10m02';
	}
	
	//alert(newsletter.elements['redirect'].value);
}

function ClearInputEvents(id,changeType) {
	obj = document.getElementById(id);
	
	if (obj) {
		val = obj.value;
		obj.startvalue = val;
		obj.value = val;
		
		if(changeType){
			typ = obj.type;
			obj.starttype = typ;
			obj.type = typ;
		}
		
		obj.onfocus = function() { 
			if(this.value == this.startvalue) { this.value = '';}
			if(changeType){
				if(this.type == this.starttype) { this.type = 'password';}
			}
		}
		obj.onblur =  function() { 
			if(this.value == '') { this.value = this.startvalue; }
			if(changeType){
				if(this.type == 'password' && this.value == this.startvalue) { this.type = 'text'; }
			}
		}	
	}
}

var show_textfield = function(){
	if($('in_contact_gekomen')){
		$('frm_adress').contact_index.value = $('in_contact_gekomen').selectedIndex;
		if ($('in_contact_gekomen').selectedIndex >= 8) {
			$('in_contact_text').style.display = 'block';
			$('in_contact_textarea').name = 'in_contact_gekomen';
		} else {
			$('in_contact_text').style.display = 'none';
			$('in_contact_textarea').name = '';
		}
	}
}

window.onload.actions.push(init);
window.onload.actions.push(init_logo);
window.onload.actions.push(init_products);
window.onload.actions.push(lightwindowInit);
window.onload.actions.push(function(){
	if($('frm_adress')){
		if($('frm_adress').contact_index.value){
			$('in_contact_gekomen').selectedIndex = $('frm_adress').contact_index.value ;
		}
	}
});
window.onload.actions.push(show_textfield);

var check_as_radio = function(collection, current){
	collection.each(function(element){
		if(element != current){
			element.checked = false;
		}
	});
}

var cart_check_as_radio = function(){
	var buttons = $A($$('#form_cart input.radio'));
	buttons.each(function(element){
		var clickEvent = element.onclick;
		element.onclick = function(){};
		element.observe('click', function(){
			check_as_radio(buttons,element);
			clickEvent();
		});
	});
}

window.onload.actions.push(function(){
	var buttons = $A($$('#form_subscrip input.radio'));
	buttons.each(function(element){
		element.observe('click', function(){
			check_as_radio(buttons,element);
		});
	});
});

var tickertimeout;
function tiktik(obj) {
	var div=document.getElementById(obj);
	var span1=div.getElementsByTagName('SPAN')[0];
	var span2=div.getElementsByTagName('SPAN')[1];
	
	if (span1.style.left) var l=parseInt(span1.style.left); else var l=0;
	l=l-1.0;
	
	if(0-l>span1.offsetWidth) l=0;
	
	span1.style.left=l+'px';
	span2.style.left=l+span1.offsetWidth+'px';
	
	tickertimeout=setTimeout("tiktik('"+obj+"')", 18);
}
		
function inittiktik(obj) {
	if (document.getElementById(obj) != null) {
		var div=document.getElementById(obj);
		var span1=div.getElementsByTagName('SPAN')[0];
		span1.innerHTML+='&nbsp;';
		div.innerHTML+=div.innerHTML;
	
		div.onmouseover=function() { clearTimeout(tickertimeout); }
		div.onmouseout=function() { tickertimeout=setTimeout("tiktik('"+obj+"')", 18); }
		
		tiktik(obj);
	}
}

function go_to_url(url) {
	alert('');
	document.location.href = url;
}
