// GEN basis
// GEN basis
function popup(url,winName,W,H,X,Y) {
	if(W == undefined) W = 400;	//default width
	if(H == undefined) H = 300; //default height
	if(X == undefined) X = screen.width/2-W/2; //default Xpos (is now centered)
	if(Y == undefined) Y = screen.height/2-H/2; //default Ypos (is now centered)
	X = 10;
	Y = 10;
	winprops = 'height='+H+',width='+W+',top='+X+',left='+Y+',scrollbars=yes, resizable=yes';

	win = window.open(url, winName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}
function stocks(id){
	for(var i = 1; i <5; i++)
	{
		document.getElementById('tab_'+i).style.display = 'none';
		document.getElementById('stocklink'+i).className =  '';
	}
	
	document.getElementById('tab_'+id).style.display = 'block';
	document.getElementById('stocklink'+id).className =  'active';
}
function ignoreenter(myfield,e)
		{
			var keycode;
			if (window.event) keycode = window.event.keyCode;
			else if (e) keycode = e.which;
			else return true;
			
			if (keycode == 13)
			   {
				 
				  
				   return false;
			   }
			else
				 return true;
		}
function $rgb(id)
{

	return document.getElementById(id);	
}
function hide(id)
{
		
	$rgb(id).style.display = 'none';
}
function show(id)
{
		
	$rgb(id).style.display = 'block';
}
function isHidden(id)
{
	if($rgb(id).style.display == 'none')
		return true;
	else
		return false;
}
function switchCurrency(currency)
{
	setCookie("currency", currency, 30, '/', '', '');	
	location.reload();
	
}
function V_switch(id)
{
	if(isHidden(id))
		show(id);
	else
		hide(id);
}
function switchPaymentMethod(id)
{
	for(var i=1; i<=3 ; i++)
	{
		if( $rgb('payment_method_'+i) != null)
		hide('payment_method_'+i);
	}
	if(id != -1)
	show('payment_method_'+id);
}
function bankPay()
{
	setCookie("basket", '', 30, '/', '', '');	
	
	$rgb('bankpayform').submit();
}
function molliePay()
{
	setCookie("basket", '', 30, '/', '', '');	
	
	$rgb('molliepayform').submit();
}
function paypalPay()
{
	setCookie("basket", '', 30, '/', '', '');	
	
	$rgb('paypalpayform').submit();
}
// GEN basis
// AJAX basis
	
var parsing_xml = false;
var lang = "";
function loadXMLFromText(xmlData)
{
	if (window.ActiveXObject) {
			//for IE
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(xmlData);
			return xmlDoc;
		} else if (document.implementation && document.implementation.createDocument) {
			//for Mozila
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(xmlData,"text/xml");
			return xmlDoc;
		}

}
function moveto(url)
{
	window.location = url;	
}
function replacewww(text) { 
	var start = (text.indexOf("www"))-7; 
	var match = text.substr(start,7); 
	if(match != "http://" || match != "ttps://") 
	{ 
	text = text.replace("www", "http://www"); 
	} 
	
	 
	return text; 
}
function replaceURLWithHTMLLinks(text) { 
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; 
	return text.replace(exp,"<a href='$1' >$1</a>"); 
}


function processXmlTwitter(response)
{
	parsing_xml =false; 
	xmlDoc=loadXMLFromText( response );
	var str = xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue; 
	 
	str = replacewww(str); 
	str = replaceURLWithHTMLLinks(str); 
	$rgb('twittertxt').innerHTML=  "<p>"+str+"</p>";
	show('twitter');
	
	
}
														
function processData(responseText, responseStatus)
	 { 
		
		//document.getElementById('loadingbar').style.display ='none';
		if (responseStatus==200) // succes
		{
			if(responseText == 'error')
							alert('Error updating data!'+responseText);
			else
			{
				if(parsing_xml)
					processXmlTwitter(responseText);
				else
				{
					
					eval(responseText);
				}
				
			}
		 }
		else if(responseStatus == 0)
		{
		
		}
		else 
		{ 
		   alert(responseStatus + ' -- Error Processing Request');
		}
	  }
function ajaxObject(url) {
		  var that=this;      
		   this.updating = false;
			  this.abort = function() {
					that.AJAX.onreadystatechange = function () {}
				  that.updating=false;
				  that.AJAX.abort();
				  that.AJAX=null;
				
			  }
		  this.update = function(form,postMethod) { 
			if(that.updating) that.abort();
			 that.AJAX = null;                      
			 var passData = getFormValues(form,"validate");
			 
			 
					
			if (window.XMLHttpRequest) {              
			  that.AJAX=new XMLHttpRequest();              
			} else {                                  
			  that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
			}                                             
			if (that.AJAX==null) {                             
			  return false;                               
			} else {
			
				
				  that.AJAX.onreadystatechange = function() {  
					if (that.AJAX.readyState==4) {             
					  that.updating=false;                
					  that.callback(that.AJAX.responseText,that.AJAX.status);        
					  that.AJAX=null;                                         
					} 
					}
																
			  that.updating = new Date();                              
			  if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
				//that.AJAX.setRequestHeader("Content-Length", passData.length);
				that.AJAX.send(passData);
			  } else {
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
				that.AJAX.open("GET", uri, true);                             
				that.AJAX.send(null);                                         
			  }              
			  return true;                                             
			}                                                                           
		  }
		  var urlCall = url;        
		  this.callback = processData;
		  }
		  
		
	
function getFormValues(fobj,valFunc, url) 			
	{ 
	
	   var str = ""; 
	
	   var valueArr = null; 
	
	   var val = ""; 
	
	   var cmd = ""; 
	
	   for(var i = 0;i < fobj.elements.length;i++) 
	
	   { 
	
		   switch(fobj.elements[i].type) 
	
		   { 
			case "checkbox":
					var myval = fobj.elements[i].checked;
					if(myval) 
						myval = 1;
					else
						myval = 0;								   
				   str += fobj.elements[i].name + 
	
					"=" + myval + "&"; 
	
					   break; 
			  case "textarea":
				 str += fobj.elements[i].name + 
	
						 "=" + escape(fobj.elements[i].value) + "&"; 
						 break; 
		   
				case "password":
					str += fobj.elements[i].name + 
	
						 "=" + escape(fobj.elements[i].value) + "&"; 
						  break; 
				case "hidden":
					str += fobj.elements[i].name + 

					 "=" + (fobj.elements[i].value) + "&"; 
					  break; 
			   case "text": 
	
							
					str += fobj.elements[i].name + 
	
					 "=" + escape(fobj.elements[i].value) + "&"; 
	
					 break; 
	
			   case "select-one": 
	
					str += fobj.elements[i].name + 
	
					"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&"; 
	
					break; 
	
		   } 
	
	   } 
							
				
					 
					 
	   str = str.substr(0,(str.length - 1)); 
	
	   return str; 
	
	}


	

// productfiles magic
var myRequestPF = new ajaxObject( 'http://www.terrazza.be/'+'ajax/fetchfiles.php');
function fetchFiles(id)
{
	$rgb('ajax_product_id').value = id;
	myRequestPF.update( $rgb('ajax_fetchproduct_form'), 'POST');

}

// account magic
var myRequestAccount = new ajaxObject( 'http://www.terrazza.be/'+'ajax/account.php');

function changeInfo()
{
	myRequestAccount.update( $rgb('account_form'), 'POST');	
}

// contact magic
var myRequestContact = new ajaxObject( 'http://www.terrazza.be/'+'ajax/contact.php');

function Contact()
{
	myRequestContact.update($rgb('contactform'), 'POST');
}
function contact_gt()
{
	window.location = "http://www.terrazza.be/"+lang+"/contact-bedankt";
}

// search magic
function search()
{
 $rgb('searchform').submit();
}
function searchFocus(obj)
{
	if(obj.value == "typ hier uw tekst")
		obj.value =  "";
	if(obj.value == "type here")
		obj.value =  "";
}
function searchBlur(obj)
{
	if(lang == "nl")
	{
		if(obj.value == "")
			obj.value = "typ hier uw tekst";
	}
	else
	{
		if(obj.value == "")
			obj.value = "type here";
	}
	
}
// login magic
var myRequestLogin = new ajaxObject( 'http://www.terrazza.be/'+'ajax/login.php');
var myRequestPW = new ajaxObject( 'http://www.terrazza.be/'+'ajax/pwreset.php');

function moveToUrl(url)
{
	window.location = url;
}

function doPwReset()
{	 
 
	myRequestPW.update($rgb("pwresetform") , "POST" );	
}
// SHOPPING magic

function emptyBasketShop()
{
	setCookie("basket", '', 30, '/', '', '');	
	
	window.location = "http://www.terrazza.be/"+lang+"/shop";		
}
//  SHOP basis

/* cart functions */
var baseurl = "http://www.terrazza.be/";
var stricturl = "http://www.terrazza.be/";

//////////// SHOPPING CART start
function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );


if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
function setCookie2 (name, value, lifespan, access_path) {
      
  var cookietext = name + "=" + escape(value)  
    if (lifespan != null) {  
      var today=new Date()     
      var expiredate = new Date()      
      expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan)
      cookietext += "; expires=" + expiredate.toGMTString()
    }
    if (access_path != null) { 
      cookietext += "; PATH="+access_path 
    }
   document.cookie = cookietext 
   return null  
}
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
	return ( unescape ( results[1] ) );
  else
	return '';
}	

function add(new_item){

	var old_amount = deleteOrderReturnAmount(new_item);
	basket_string = get_cookie("basket");
	
	var p_fid = ''+( parseInt($rgb('aantal').options[$rgb('aantal').selectedIndex].value) + parseInt(old_amount));
	

	if(basket_string == '' || basket_string == null)
		basket_string = new_item + 'fff' + p_fid;
	else
		basket_string = basket_string + "xxx" + new_item + 'fff' + p_fid;
	
	setCookie("basket", basket_string, 30, '/', '', '');
	
	window.location = 'http://www.terrazza.be/'+lang+'/winkelwagentje';
}
function addinstant(new_item){
		
	// failsafe check: do not allow multiple subscription items..
	// find product id
	new_item = new_item.toString();
	var temp = new_item.split('_');
	
	if( temp.length == 2)
	{
		// product with options
		var product_id = temp[0];
		basket_string = get_cookie("basket");
	
		if(  basket_string.search(product_id+"_") != -1)
		{
			alert("U kan dit product niet toevoegen aan het mandje omdat u al een gelijkaardige subscriptie in uw mandje heeft. Gelieve deze eerst te verwijderen.");
			return;
		}
	}	
	// 
	
	var old_amount = deleteOrderReturnAmount(new_item);
	basket_string = get_cookie("basket");
	
	var p_fid = ''+( 1 + parseInt(old_amount));
	

	if(basket_string == '' || basket_string == null)
		basket_string = new_item + 'fff' + p_fid;
	else
		basket_string = basket_string + "xxx" + new_item + 'fff' + p_fid;
	
	setCookie("basket", basket_string, 30, '/', '', '');
	
	window.location = 'http://www.terrazza.be/'+lang+'/winkelwagentje';
}
function buyNow(id)
{
	add_str = id;
	// get optional options ..
	if( $rgb('getoptions_'+id) != null)
	{
		var t = $rgb('getoptions_'+id);
		add_str = add_str +'_'+ t.options[t.selectedIndex].value;
		if( t.options[t.selectedIndex].value == '-1')
		{	
			alert('Gelieve een optie te selecteren.');
			return;
		}
	}
	addinstant(add_str);
}
function confirmDeleteOrder(id)
{

	
	  if (confirm("Bent u zeker dat u dit product wilt verwijderen uit het winkelwagentje?")) {
		deleteOrder(id);
	  }
	
}
function deleteOrder(id)
{
	// special -> only delete one amount from this item
	basketstr = get_cookie("basket");
	new_basketstr = "";
	
	if(basketstr == "")return;
	
	var myarray = basketstr.split("xxx");
	var found = false;

	for(var i = 0; i<myarray.length;i++)
	{
		var temparray = myarray[i].split("=");
		var tmp = temparray[0].split("fff");
		
		if(id != tmp[0] || found)
		{
			
			if(i > 0) new_basketstr += "xxx";
			new_basketstr += temparray[0];			
		}else
		{found = true;
			if(tmp[1] > 1)
			{
				if(i > 0) new_basketstr += "xxx";
				new_basketstr += tmp[0]+'fff'+(tmp[1]-1);	
			}
		
		}
	}	
	
	//alert(new_basketstr);
	setCookie("basket", new_basketstr, 30, '/', '', '');	
	
	
	
	window.location = 'http://www.terrazza.be/'+lang+'/winkelwagentje';
}
function emptyBasketShop()
{
	setCookie("basket", '', 30, '/', '', '');	
	
	window.location = "http://www.terrazza.be/'+lang+'/shop";		
}
function deleteOrderReturnAmount(id)
{
	basketstr = get_cookie("basket");
	
	new_basketstr = "";
	var found_amount = 0;
		if(basketstr == "")return found_amount;
	
	var myarray = basketstr.split("xxx");
	var found = false;

	for(var i = 0; i<myarray.length;i++)
	{
		var temparray = myarray[i].split("=");
		
		var tmp = temparray[0].split("fff");
		if(id != tmp[0] || found)
		{
			
			if(i > 0) new_basketstr += "xxx";
			new_basketstr += temparray[0];			
		}else
		{
			found = true;
			found_amount = tmp[1];
			
			
		}
	}	
	
	
	setCookie("basket", new_basketstr, 30, '/', '', '');	
	
	return found_amount;
}

function goToCheckout(logged_in)
{
	
		if(logged_in)
			window.location = 'http://www.terrazza.be/'+lang+'/checkout';
		else
			window.location = 'http://www.terrazza.be/'+lang+'/checkout';
	
}
function update_shipping_option()
{
	var sec = $rgb('shipping_option');
	var value = sec.options[sec.selectedIndex].value;
	if(value == -1) return;
	var price = parseFloat(sec.options[sec.selectedIndex].className);
	
	var normale_prijs = parseFloat( $rgb('normale_prijs').innerHTML );
	$rgb('verzending_amount').innerHTML = price;
	$rgb('totaal_amount').innerHTML = price + normale_prijs;


}
//  SHOP basis

// REGFORM basis
function goRegisterForm(bool)
{
	if(bool == 1 ) show('company_form'); else hide('company_form');	
}
function goRegisterForm2(bool)
{
	if(!bool)hide('facturatie_adres_form');else show('facturatie_adres_form');

}
var myRequestRegister = new ajaxObject( 'http://www.terrazza.be/'+'ajax/registreren.php');

function register()
{
	
	myRequestRegister.update( $rgb('register_ajax_form'), 'POST');	
}


function gotoUrl(mylink)
						{
							window.location = mylink;
						}			
																				
function showAddress(address, title, id) {
	 // if (geocoder) {
	//	geocoder.getLatLng(
	//	  address,
	//	  function(point) {
	//		if (!point) {
	//		  alert(address + " not found");
	//		} else {
			 // map.setCenter(point, 13);
				var point = new GLatLng(52.314674, 5.042939); 
				marker = new GMarker(point);
				GEvent.addListener(marker, "click", function() {    gotoUrl("http://maps.google.be/maps?f=q&source=s_q&hl=nl&q=Frederica+Zeijdelaarweg+23,+1382+Weesp,+Noord-Holland,+Nederland&sll=50.805935,4.432983&sspn=5.597012,16.907959&ie=UTF8&cd=1&geocode=FTJCHgMd_PJMAA&split=0&hq=&hnear=Frederica+Zeijdelaarweg+23,+1382+Weesp,+Noord-Holland,+Nederland&ll=52.314674,5.042939&spn=0.002643,0.008256&z=18") });
		
				 
			  map.addOverlay(marker);
			 // marker.openInfoWindowHtml(title);
			
	}
		 
function load() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("gmaps"));
	map.addControl(new GSmallZoomControl());
	geocoder = new GClientGeocoder();
	map.setCenter(new GLatLng(52.314674, 5.042939), 14);
	
	GIcon.transparent ="";
	for(var i = 1; i < adres_str.length; i++)
		showAddress( adres_str[i] , marker_str[i], i);							
  }
}

$(document).ready(function() {
				
			$("a.pic").fancybox({
					'hideOnContentClick': false
				});
			$("a.refs").fancybox({
					'hideOnContentClick': false
				});
			
			$("a.refs2").fancybox({
					'hideOnContentClick': false
				});
			
			$("a.iframe").fancybox({
					'hideOnContentClick': false
				});
				$("a.iframenoscroll").fancybox({
					'hideOnContentClick': false, 
					'scrolling': 'no'
				});
			
			
			});
			
			
function showTab(id)
{
	// hide tabs
	for(var i = 1; i <=4; i++)
	{
		if($rgb('tab'+i) == null)break;
		hide('tab'+i);
		$rgb('tablink'+i).className = "";
	}
	
	$rgb('tab'+id).style.display = "block";
	$rgb('tablink'+id).className = "active";
	
	

}

var quot_busy = false;
function submitQuotation()
{
	
	if( quot_busy ) return;
	
	show('loading_');
	quot_busy = true;
	myRequest3.update($rgb('quotation_form'),'POST');	
	
	
}
var myRequest3 = new ajaxObject('http://www.terrazza.be/'+'ajax/quotation.php');	

function submitDemo()
{
	myRequestDemo.update($rgb('quotation_form'),'POST');	
	
	
}
var myRequestDemo = new ajaxObject('http://www.terrazza.be/'+'ajax/demo.php');	


function submitPartner()
{
	myRequestPartner.update($rgb('quotation_form'),'POST');	
	
	
}
var myRequestPartner = new ajaxObject('http://www.terrazza.be/'+'ajax/partner.php');	


var formsubmitstatus = false;
var lang_submit="";
var lang_wait = "";
function fsm_disable()
{
	if($rgb('formsubmitbutton2'))
		$rgb('formsubmitbutton2').disabled = true;
	//$rgb('formsubmitbutton').value = lang_wait;
	if($rgb('formsubmitbutton') != null) $rgb('formsubmitbutton').disabled = true;
	formsubmitstatus = true;
}
function fsm_enable()
{
	try{
	
	if($rgb('formsubmitbutton2'))
		$rgb('formsubmitbutton2').disabled = false;
	//$rgb('formsubmitbutton').value = lang_submit;
	if($rgb('formsubmitbutton') != null) $rgb('formsubmitbutton').disabled = false;
	
	}catch(err){}
	formsubmitstatus = false;
}

function focusMail(obj)
{
	if(obj.value == "E-mail")
		obj.value = "";
}
function blurMail(obj)
{
	if(obj.value == "")
		obj.value = "E-mail";

}

function submitenter_tmcfrontpageNM(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
	  submitNewsletter();
	   return false;
   }
else
	 return true;
}
var myRequest5 = new ajaxObject('http://www.terrazza.be/'+'ajax/ajaxbuffer.php');	

function submitNewsletter()
{
	myRequest5.update($rgb('index_newsletter_form'),'POST');	
}

