// To do foutje opsporen bij CID in changesetting

var ZoekResultaatHidden = 0;

var PIDSelectie = new Array();
var FormGevalideerd = new Array();

var Fade;
var Show
var ID;
var MenuStatus;
var ProductSelectorStatus;

var URLPostfixOud;
var URLPostfixKortOud;

var Kolom1Volgorde = 'ASC';
var Kolom5Volgorde = 'ASC';

var FilterVeldenURL = new Array();

var FilterVisible = 'TRUE';

var CookieVars = document.cookie.split(';');


var NieuwsTeller;
NieuwsTeller = 0;



var Fouten				= new Array();
var PrognoseTeksten 	= new Array();

function FlashDiv(el, count) 
{
	if (count >= 4)
	{
		return;
	}
	else if (isInt(count/2))
	{
		el.style.display = "none";
		window.setTimeout(function() {FlashDiv(el, count+1);}, 200);
	}
	else
	{
		el.style.display = "";
		window.setTimeout(function() {FlashDiv(el, count+1);}, 200);
	}
}

function FormSubmit()
{
	var Verzenden = true;

	for (var strCurrentKey in Fouten) 
	{
		if(Fouten[strCurrentKey])
		{
			var Foutmelding			= document.getElementById('Foutmelding' + strCurrentKey.replace('Input', ''));
			FlashDiv(Foutmelding, 0);

			var Verzenden = false;
		}
	}
	
	if (Verzenden)
	{
		parent.document.Voorkeuren.submit();
	}
}

function isInt(x)
{ 
	var y=parseInt(x); 
	if (isNaN(y)) {return false;} else { return x==y && x.toString()==y.toString();}
}

function Afronden(ID, y, Min, Max, PosX, PosY)
{
	var el 		= document.getElementById(ID); 

	var Waarde 	= el.value;

	
	if (!isInt(Waarde) || Waarde < Min || Waarde > Max || !isInt(Waarde / y))
	{
		Fouten[ID]			= true;

		var Foutmelding			= document.getElementById('Foutmelding' + ID.replace('Input', ''));
		Foutmelding.style.top 		= PosY + 'px';
		Foutmelding.style.left 		= PosX + 'px';
		Foutmelding.style.display	= "";
	}
	else
	{
		Fouten[ID]			= false;

		var Foutmelding 		= document.getElementById('Foutmelding' + ID.replace('Input', ''));
		Foutmelding.style.display	= "none";

		// el.value 	= Math.round(el.value / y) * y;
	}
}


function CheckInput(ID, Min, Max, Veelvoud, PosX, PosY)
{
	var el = document.getElementById(ID); 

	// Check of het een getal is
	
	var Waarde = el.value;

	
	if (Waarde)
	{
		if (!isInt(Waarde) || Waarde < Min || Waarde > Max)
		{
			Fouten[ID]			= true;
			var Foutmelding			= document.getElementById('Foutmelding' + ID.replace('Input', ''));
			Foutmelding.style.top 		= PosY + 'px';
			Foutmelding.style.left 		= PosX + 'px';
			Foutmelding.style.display	= "";
		}
		else
		{
			Fouten[ID]			= false;

			var Foutmelding 		= document.getElementById('Foutmelding' + ID.replace('Input', ''));
			Foutmelding.style.display	= "none";
		}
	}
	
}




function FilterResultaat()
{
	document.Filteren.submit();
}


function ToonFlash(SWFURL, URL, Breedte, ID, Hoogte)
{
	document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='" + Breedte + "' height='280' id='Rectangle' align='middle'>");
	document.write("<param name='allowScriptAccess' value='sameDomain' />");
	document.write("<param name='movie' value='" + SWFURL + "' /><param name='loop' value='true' />");
	document.write("<param name='menu' value='false' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='bgcolor' value='#ffffff' />");
	document.write("<param name='url' value='" + URL + "' />");
	document.write("<param name='wmode' value='transparent' />");
	document.write("<embed src='" + SWFURL + "' loop='true' menu='false' quality='high' wmode='transparent' bgcolor='#ffffff' width='" + Breedte + "' height='280' name='Rectangle' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
	document.write("</object>");


	var el = document.getElementById('Rectangle' + ID);
	// Hoogte instellen ivm afwijkende rectangles
	el.style.height = Hoogte + 'px';



}

function EmbedVideo(Params)
{
	document.write("<embed src='http://www.portablegear.nl/mediaplayer.swf' width='480'height='260' allowscriptaccess='always' allowfullscreen='true' flashvars='callback=analytics&height=300&width=480&shuffle=false&autostart=True&file=http://www.portablegear.nl/videoplaylist.htm" + Params + "&displayheight=240&displaywidth=320'>");
//	document.write("<embed src='http://www.portablegear.nl/mediaplayer.swf' width='480'height='260' allowscriptaccess='always' allowfullscreen='true' flashvars='height=300&width=480&shuffle=false&autostart=false&file=http://www.portablegear.nl/videoplaylist.htm" + Params + "&displayheight=240&displaywidth=320'>");
}
	



function ShowSubmenuTimer(ID)
{
	clearTimeout(Fade);
	Show = setTimeout('ShowSubmenu('+ID+')', 200);
}

function ShowSubmenu(ID)
{
	for (i = 0; i < 9; i++)
	{

		var el = document.getElementById('PulldownMenu' + i);

		if (i == ID)
		{
			if (el) {el.style.display = '';}
		}
		else
		{
			if (el) {el.style.display = 'none';}
		}
	}
}


function ShowProductSelector()
{
	var el = document.getElementById('PulldownMenuProducten');

	if (ProductSelectorStatus == 'Show')
	{
		ProductSelectorStatus = 'False';
		if (el) {el.style.display = 'none';}
	}
	else
	{
		ProductSelectorStatus = 'Show';
		if (el) {el.style.display = '';}
	}
}



function FadeMenu()
{
	clearTimeout(Show);
	Fade = setTimeout('ShowSubmenu(-1)', 1000);
}






function LaadPopUp(URL, Hoogte, Breedte)
{
	window.open(URL, 'PopUp', 'scrollbars=no,width=' + Breedte + ',height= ' + Hoogte + ',resizable=no,status=0,toolbar=0,top=100,left=100');
}


function ShowPulldown(Sectie, posX, posY)
{
	for(var SectieOverig = 1; SectieOverig < 10; SectieOverig++)
	{
		var el =  document.getElementById("DivPulldown" + SectieOverig);
		if (el) {el.style.display='none';}
	}



	var el =  document.getElementById("DivPulldown" + Sectie);

	el.style.display = "";
	el.style.left = posX + "px";
	el.style.top = posY + "px";
}


/* Auto-aanvullend zoekformulier */

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject()
{
	if(window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else 
	{
		// Ietsjes meer user-friendly mag wel
		alert("Data kon niet geladen worden. Browser issue.");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq 	= getXmlHttpRequestObject();
var WinkelID 	= 0;
var OpenDiv		= new Array();

function ToonWinkels(ID, AboID, PID, Looptijd, PrijsLos, PrijsTotaal) 
{
	WinkelID = ID;
	
	if (OpenDiv[ID] == 'open')
	{
		var el 				= document.getElementById('PrognoseWinkels' + ID);
		el.style.display 	= 'none';
		OpenDiv[ID] 		= 'dicht';
		return;
	}
	else
	if (OpenDiv[ID] == 'dicht')
	{
		var el 				= document.getElementById('PrognoseWinkels' + ID);
		el.style.display 	= '';
		OpenDiv[ID] 		= 'open';
		return;
	}		
	
	// Bijhouden welke ID's al eerder geopend zijn
	OpenDiv[ID] = 'open';
	
	if(searchReq.readyState == 4 || searchReq.readyState == 0)
	{
		var str = "";
		searchReq.open("GET", 'http://www.portablegear.nl/@test/winkels.htm?AboID=' + AboID + '&PID=' + PID + '&Looptijd=' + Looptijd / 12 + '&PrijsLos=' + PrijsLos + '&PrijsTotaal=' + PrijsTotaal, true);
		

		
		searchReq.onreadystatechange = ToonWinkelsAfhandelen; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function ToonWinkelsAfhandelen()
{
	if(searchReq.readyState == 4) 
	{
		var ss 	= document.getElementById('Winkels' + WinkelID);
		ss.innerHTML = '';
		var str = searchReq.responseText;
		var Delen = str.split("<dl");

		var Hoogte = (Delen.length - 1) * 25;

		ss.innerHTML = str;
		
		ss.style.height 		= Hoogte+"px";
		// ss.style.borderBottom 	= "1px solid #ff8703";
	}
}

//Click function
function setSearch(value) 
{
	document.getElementById('ZoekSuggesties').innerHTML = '';
}




function BannerLaden(Type)
{
	var Timestamp 	= Math.round(new Date().getTime() / 1000);
	URL 			= "http://www.portablegear.nl/@test/advertenties.htm?Type=" + Type + "&Timestamp=" + Timestamp;
	
	if (window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (req != undefined) 
	{
		req.onreadystatechange = function() {HTMLAfbeelden(URL, Type);};
		req.open("GET", URL, true);
		req.send("");
	}
}  


function HTMLAfbeelden(URL, IDContent) 
{
	if (req.readyState == 4) 
	{ 
		if (req.status == 200) 
		{
			// Hier nog een regel die de rectangle laat verdwijnen als er geen responseText is
			document.getElementById(IDContent).innerHTML = req.responseText;
		} 
		else 
		{
			document.getElementById(IDContent).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
		}
	}
}


function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function checkEmailadres(id)
{
		
	var email = $('#Emailadres' + id).val();
		
	if(email != 0)
	{	
		if(isValidEmailAddress(email))
		{
			$('#EmailadresCheck' + id).css({ 'background-image': "url('/media/layout/pictogram-gevalideerd.png')" });
			FormGevalideerd[id] = 1;
		} 
		else 
		{
			$('#EmailadresCheck' + id).css({ 'background-image': "url('/media/layout/pictogram-niet-gevalideerd.png')" });
			FormGevalideerd[id] = 0;
		}
	} 
	else 
	{
		$('#EmailadresCheck' + id).css({ 'background-image': 'none' });
		FormGevalideerd[id] = 0;
	}
	
}

function SerializeFieldset(fieldsetName)
{    
	//Add a new form and hide it.    
	$('body').append('<form id="form-to-submit" style="visibility:hidden;"></form>');       
	
	//Clone the fieldset into the new form.    
	$('#form-to-submit').html($(fieldsetName).clone());        
	
	//Serialize the data    
	var data = $('#form-to-submit').serialize();        
	
	//Remove the form    
	$('#form-to-submit').remove();        
	
	//Alert to see the data, this is where you would do your ajaxy stuff :)    
	return data;
}

function FormulierVerzenden(formId, targetId, sourceFormId, baseUrl)
{
	// alert(sourceFormId);
	
	var url = baseUrl + '?' + SerializeFieldset('#' + sourceFormId);
	
//	$('#Faded').show();
//	$('#GebruikerPopup').show();
//	$('#Feedback').load(url);
	
	if ((FormGevalideerd[formId]) == 1)
	{
		$('#Faded').show();
		$('#GebruikerPopup').show();
		$('#Feedback').load(url);
		

	}
}

function TakeoverRedirect()
{
	var VensterBreedte = $(window).width();
		
	if (VensterBreedte < 984)
	{
		return false;
	}
	else
	{
		if (posX < (VensterBreedte - 984) / 2)
		{
			window.open('http://shop.telfort.nl/?utm_source=Portablegear&utm_medium=display&utm_campaign=N97TO');
			return false;	
		}
		else if (posX > (VensterBreedte - 984) / 2 + 984)
		{
			window.open('http://shop.telfort.nl/telefoons/Nokia/Nokia-X6-16GB.html?utm_source=Porablegear&utm_medium=300x250&utm_campaign=X6TO');
			return false;		
		}
	}
}
