// JavaScript Document
var  $$ = jQuery.noConflict();

var doc = document;
var tDiv;

var Q =0;
$$(document).ready(function(){
	$$("a.popup").fancybox({
		'titlePosition' 		: 'inside'					   
	});
	
	$$('.page_numbers a').click(function(){
		var overlay=$$('<div id="overlay"></div><div id="overlayLoad"><img style="border:none" src="" width="32" height="32"/></div>');
		overlay.appendTo('#product_grid');
		var pWidth=$$("#product_grid").width()/2;
		var iWidth=25;
		var wresult=pWidth-iWidth;
		var pHeight=700/2;
		var iHeight=25;
		var hresult=pHeight-iHeight;
		$$("#overlayLoad").css("margin-left",wresult+"px");
		$$("#overlayLoad").css("margin-top",hresult+"px");
		$$("#overlayLoad").css("position","absolute");
		$$("#overlayLoad").css("z-index","101");
	});
});
function getXMLObj()
	{
		
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		 		Obj = new XMLHttpRequest();
		  }
		else if (window.ActiveXObject)
		  {
		  	// code for IE6, IE5
		 		Obj = new ActiveXObject("Microsoft.XMLHTTP");
		  }
		else
		  {
		  		alert("Your browser does not support XMLHTTP!");
		  }
		return Obj;
	}
	function closeMessage(itemId){
		$$('#productCheckout_'+ itemId).fadeOut();
	}
	function doCart(tDo, itemId, qty, pgfrm){
		Q = pgfrm;
		if (isNaN(qty) || (qty < 0)){
			//alert('Please enter a valid number');
			//return;
		}
		Obj = getXMLObj();
			if (Obj !== null && Obj !== undefined){
				if (tDo == 'DelItem'){

					doc.getElementById('prod_'+ itemId).style.display = 'none';
				}
				if (tDo == 'AddItem'){
					$$('.productCheckout').not('#productCheckout_'+ itemId).fadeOut();
					$$('.productCheckout').not('#productCheckout_content_'+ itemId).html();
					$$('#productCheckout_'+ itemId).fadeIn();
				}
				
				if($$(".variation_select").is('input:radio')){
					var varId = $$('input:radio[name=variation_radio]:checked').val();
				} else {
					var varId = $$(".variation_select").val();
				}

				var quantity = $$("#quantity").val();
				
				var url = '../modules/shoppingcart/cart/myCart.php?tDo='+ tDo  + '&itemId=' + itemId + '&Qty=' + quantity + '&pgfrm=' + pgfrm + '&sId=' + Math.random()+ '&varId=' + varId;
				if (tDo == 'UpdCart'){
					var itemID = doc.getElementsByName(itemId);
					var theItems ='';
					var Qty = doc.getElementsByName(qty);		
					var theQtys = '';
					
					for(i=0; i<itemID.length; i++){
						theItems = theItems + ',' + itemID[i].value;
						theQtys = theQtys + ',' + Qty[i].value;
					}
					url = '../modules/shoppingcart/cart/myCart.php?tDo='+ tDo  + '&itemId=' + theItems + '&Qty=' + theQtys + '&pgfrm=' + pgfrm + '&sId=' + Math.random()+ '&varId=' + varId;
				}
				Obj.onreadystatechange = doingCart;
				Obj.open('GET', url, true);
				Obj.send(null);
			}
	}
	
	function doingCart(){
	//var h = document.getElementById('Cart_Container').scrollHeight;
		//var w = document.getElementById('Cart_Container').scrollWidth;
		
		if (Obj.readyState == 4){
			$$('.productCheckout #theOverlay').hide();
			$$('.productCheckout #productMessage').show();
			$$('#IBKCart_Container #theOverlay').hide();
			$$('#shoppingcart_container').show();
			doc.getElementById('shoppingcart_container').innerHTML = Obj.responseText;
			if (window.location.href.indexOf("/bestelling-plaatsen") > 0){
				window.location.reload();
			}
		}
		if (Obj.readyState < 4){
				var id = (Q == 'ChkOut') ? 'Cart_Container_DetailView' : 'shoppingcart_container';
				$$('.productCheckout #theOverlay').show();
				$$('.productCheckout #productMessage').hide();
				$$('#IBKCart_Container #theOverlay').show();
				doc.getElementById(id).style.display = 'none';	
		}
	}
	
	
	
	//Gotten Directly from the internet...Credit not taken for this at all..I dnt even understnad the this particular one that much..
	function isNumberKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
     }
	function validateform(form){
		var cont;
		cont = true;
		$$(':input.required').each(function() {
		//alert($$(this).val());
		var singleValues = $$(this).val();
		
		if(singleValues == ''){
			$$(this).css('border','1px solid red');
			cont = false;
		} else {
			$$(this).css('border','1px solid #CCCCCC');
		}
		});
		
	  if(cont == true){
		  $$('.requiredMessage').html();
	  	return true;
	  } else {
		 $$('.requiredMessage').html( '* zijn verplichte velden'); 
		return false;  
	  }
	}
function submit_purchase(){
	document.forms.ideal_form.submit();
}
function change_order(a){
	url=window.location.href;
	if(url.search(/\?/)==-1){
		separator="?";
	}else{
		separator="&";
	}
	if(a=="name"){
		if(url.search(/sort/)!=-1){
			newurl=url.replace(/id/,"name");
		}else{
			if(url.search(/order/)!=-1){
				var parts = url.split('order=',2);
				newurl=parts[0]+"sort=name";
			}else{
				newurl=url+separator+"sort=name";
			}
		}
	}else{
		if(url.search(/sort/)!=-1){
			newurl=url.replace(/name/,"id");
		}else{
			if(url.search(/order/)!=-1){
				var parts = url.split('order=',2);
				newurl=parts[0]+"sort=id";
			}else{
				newurl=url+separator+"sort=id";
			}
		}
	}
	
	window.location = newurl;
}

function change_price_order(a){
	url=window.location.href;
	if(url.search(/\?/)==-1){
		separator="?";
	}else{
		separator="&";
	}
	if(a=="highest_price"){
		if(url.search(/order/)!=-1){
			newurl=url.replace(/lowest_price/,"highest_price");
		}else{
			if(url.search(/sort/)!=-1){
				var parts = url.split('sort=',2);
				newurl=parts[0]+"order=highest_price";
			}else{
				newurl=url+separator+"order=highest_price";
			}
		}
	}else{
		if(url.search(/order/)!=-1){
			newurl=url.replace(/highest_price/,"lowest_price");
		}else{
			if(url.search(/sort/)!=-1){
				var parts = url.split('sort=',2);
				newurl=parts[0]+"order=lowest_price";
			}else{
				newurl=url+separator+"order=lowest_price";
			}
		}
	}
	
	window.location = newurl;
}
var arrSelected=new Array();

function vergelijkArtikelen(){
	if(arrSelected.length<2){
		alert("Voor het vergelijken van producten dien je er minimaal 2 te selecteren.");
		return false
	}
	return true
}

function CheckGeselecteerdeArtikelen(el){
	var i=0;
	if(el.checked){
		if(parseInt(arrSelected.length)>=3){
			alert('Je kunt maximaal 3 artikelen per keer vergelijken.');
			el.checked=false;
			return false;
		}else{
			var alreadyAdded=false;
			for(i=0;i<arrSelected.length;i++){
				if(parseInt(arrSelected[i])==parseInt(el.value)){
					alreadyAdded=true;break
				}
			}
			if(!alreadyAdded){
				arrSelected[arrSelected.length]=el.value;
			}
		}
	}else{
		for(i=0;i<arrSelected.length;i++){
			if(parseInt(arrSelected[i])==parseInt(el.value)){
				arrSelected.splice(i,1);
				break;
			}
		}
	}
}	

function change_variation(product_id, variation_ids, special,type) {
  value_ids = '';
  special_prefix = "";

    form_id = "product_"+product_id;
	if(type == 1){
		
		value_ids = "&variation[]="+variation_ids;
		
		
	} else {
		
		for(var i in variation_ids) {
		if(!isNaN(parseInt(i))) {
		  variation_name = "variation["+variation_ids[i]+"]";
		  value_ids += "&variation[]="+document.getElementById(form_id).elements[variation_name].value;
			}
		}
	}
	
  if(special == true) {
    var return_price=function(results) { 
      eval(results);
      if(product_id != null) {
        target_id = "single_special_price";
		
				document.getElementById(target_id).firstChild.innerHTML = price;
				if (price.substring(27,price.indexOf("&"))!='')
					document.getElementById(buynow_id).item_price_1.value = price.substring(27,price.indexOf("&"));
				}
      }
	} else {
    var return_price=function(results) {
      //alert(results);
      eval(results);
      if(product_id != null) {
        target_id = "single_special_price";
				
				//document.getElementById(target_id).firstChild.innerHTML = price;			
				if(jQuery("input#"+target_id).attr('type') == 'text') {
				  jQuery("input#"+target_id).val(numeric_price);
				} else {
				  jQuery("."+target_id).html(price);
				}
			}
		}
	}
  ajax.post("index.php",return_price,"ajax=true&get_updated_price=true&product_id="+product_id+value_ids);
}

