/* Javascript Functies
 * Gemaakt door Edwin Mooijaart
 * 18-06-07
 */

function StartUp()
{
	ajax_loadContent('marktplaats','Defaults.php?type=marktplaats');				
	ajax_loadContent('marktplaza','Defaults.php?type=marktplaza');				
	ajax_loadContent('tweedehands','Defaults.php?type=tweedehands');				
	ajax_loadContent('aanbodpagina','Defaults.php?type=aanbodpagina');				
	ajax_loadContent('marktnet','Defaults.php?type=marktnet');				
	ajax_loadContent('i2dehands','Defaults.php?type=i2dehands');				
	ajax_loadContent('speurders','Defaults.php?type=speurders');				
}

function ValidEmail(email) 
{
	if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function StopSearch()
{
	var trefwoord=document.search_form.q.value;
	window.location.href="?q="+trefwoord;	
}


function CheckRegisterFormValue(form,field,div)
{
	var value	=document.forms[form].elements[field].value;
	ajax_loadContent(div,'modules/Personal/CheckRegisterFormValue.php?field='+field+'&formname='+form+'&value='+value);	
}


function SearchForm()
{
	//Checken of Checkbox Object bestaat
	var action	= document.search_form.action.value;
	var error	= 0;	
	if (action == 'search')
	{
		if(typeof document.search_form.marktplaats == 'object') {} else { error = 1;  }
		if(typeof document.search_form.tweedehands == 'object') {} else  { error = 1; }
		
		if (error == 0)
		{
			var trefwoord=document.search_form.q.value;
			trefwoord = trefwoord.replace(/^\s+|\s+$/,'');//trimmen
			if (trefwoord.length  < 3) 
			{
				window.alert('Het trefwoord moet uit minimaal 3 tekens bestaan.');
				document.search_form.q.focus();
				return false;
				error = 1;
			}
		
			if (document.search_form.q.value =="" || document.search_form.q.value =="Trefwoord" ) 
			{
				window.alert('Vul een Trefwoord in.');
				document.search_form.q.focus();
				return false;
				error = 1;
			}
	
			if (error == 0)
			{
				var adv = document.search_form.adv.value;
				var marktplaats = 0;
				if (document.search_form.marktplaats.checked) 
				{
					marktplaats = 1;
				}
				
				var marktplaza = 0;
				if (document.search_form.marktplaza.checked) 
				{
					marktplaza = 1;
				}
	
				var tweedehands = 0;
				if (document.search_form.tweedehands.checked) 
				{
					tweedehands = 1;
				}
	
				var aanbodpagina = 0;
				if (document.search_form.aanbodpagina.checked) 
				{
					aanbodpagina = 1;
				}
				
				var marktnet = 0;
				if (document.search_form.marktnet.checked) 
				{
					marktnet = 1;
				}

				var i2dehands = 0;
				if (document.search_form.i2dehands.checked) 
				{
					i2dehands = 1;
				}
				
				var speurders = 0;
				if (document.search_form.speurders.checked) 
				{
					speurders = 1;
				}
				
				//Save selected checkboxes
				SetCookie("marktplaats", marktplaats, 3000);
				SetCookie("marktplaza", marktplaza, 3000);
				SetCookie("tweedehands", tweedehands, 3000);
				SetCookie("aanbodpagina", aanbodpagina, 3000);
				SetCookie("marktnet", marktnet, 3000);
				SetCookie("i2dehands", i2dehands, 3000);
				SetCookie("speurders", speurders, 3000);

				
				if (marktplaats + marktplaza + tweedehands  + aanbodpagina + marktnet + i2dehands + speurders == 0)
				{
					alert('Selecteer ten minste één Advertentie site');
				}
				else
				{
					if (adv== 1)
					{
						var ts = 0;
						if (document.search_form.ts.checked) 
						{
							ts = 1;
						}
						var url = '&ts='+ts;
						var prijs_type = document.search_form.prijs_type.value;
						url += '&prijs_type='+prijs_type;
						if (prijs_type == 1) 
						{
							var prijs_min = document.search_form.prijs_min.value;
							var prijs_max = document.search_form.prijs_max.value;
							url += '&prijs_min='+prijs_min+'&prijs_max='+prijs_max;
						}
						var distance = document.search_form.distance.value;
						url += '&distance='+distance;
						if (distance != 0) 
						{
							var zipcode = document.search_form.zipcode.value;
							url += '&zipcode='+zipcode;
						}
						var type_id = document.search_form.type_id.value;
						var cat_id = document.search_form.cat_id.value;
						var results = document.search_form.results.value;			
						url += '&type_id='+type_id+'&cat_id='+cat_id+'&results='+results;
						trefwoord = encode64(trefwoord);
						url = 'Searchers/advanced.php?q='+trefwoord+'&mp='+marktplaats+'&mpz='+marktplaza+'&twee='+tweedehands+'&abp='+aanbodpagina+'&mn='+marktnet+'&i2de='+i2dehands+'&sp='+speurders+'&adv='+adv+url
						ajax_loadContent('searching',url);
					}
					else
					{
						trefwoord = encode64(trefwoord);
						ajax_loadContent('searching','Defaults.php?type=searching&q='+trefwoord);
						var url = 'q='+trefwoord+'&mp='+marktplaats+'&mpz='+marktplaza+'&twee='+tweedehands+'&abp='+aanbodpagina+'&mn='+marktnet+'&i2de='+i2dehands+'&sp='+speurders+'&adv='+adv
						if (marktplaats == 1)
						{
							ajax_loadContent('marktplaats','Searchers/marktplaats.php?'+url);
						}
						else if (marktplaza == 1)
						{
							ajax_loadContent('marktplaza','Searchers/marktplaza.php?'+url);
						}
						else if (tweedehands == 1)
						{
							ajax_loadContent('tweedehands','Searchers/tweedehands.php?'+url);
						}
						else if (aanbodpagina == 1)
						{
							ajax_loadContent('aanbodpagina','Searchers/aanbodpagina.php?'+url);
						}
						else if (marktnet == 1)
						{
							ajax_loadContent('marktnet','Searchers/marktnet.php?'+url);
						}
						else if (i2dehands == 1)
						{
							ajax_loadContent('i2dehands','Searchers/2dehands.php?'+url);
						}
						else if (speurders == 1)
						{
							ajax_loadContent('speurders','Searchers/speurders.php?'+url);
						}
					}
				}		
			}
		}
		else
		{
			alert('Zoekers niet gevonden\nOpen de Tab \'Zoeken op Omschrijving\''); 	
		}
	}
}

/*
BEGIN CONTROLE SEARCH2MAIL FORMULIER
----------------------------------------------------------------------------------------------------------------
*/

//Dynamic content DMV AJAX
function CheckSearch2MailForm(field)
{
	if (field =='prijs_type')
	{
		prijs_type = document.Search2MailForm.prijs_type.value;
		ajax_loadContent('Prijs','modules/Search2Mail/CheckSearch2MailForm.php?field=prijs_type&value='+prijs_type);	
	}

	if (field =='payment')
	{
		payment = document.Search2MailForm.payment.value;
		ajax_loadContent('payment_div','modules/Search2Mail/CheckSearch2MailForm.php?field=payment&value='+payment);	
	}
}

function LoadRegisterForm()
{
	document.search_form.action.value='submit_aanmelden';
	ajax_loadContent('TabInhoud','modules/Personal/register.php');	
}

//Velden controleren DMV AJAX
function CheckSearch2MailFormValue(form,field,div)
{
	var value	=document.forms[form].elements[field].value;
	ajax_loadContent(div,'modules/Search2Mail/CheckSearch2MailFormValue.php?field='+field+'&formname='+form+'&value='+value);	
}


//Velden controleren, en SUBMIT enabelen indien geen fouten
function CheckSearch2MailFormSubmit(ShowError)
{
	var q 			= document.Search2MailForm.q.value;
	var prijs_type 	= document.Search2MailForm.prijs_type.value;
	var distance 	= document.Search2MailForm.distance.value;
	var type_id 	= document.Search2MailForm.type_id.value;
	var email 		= document.Search2MailForm.email.value;
	var payment 	= document.Search2MailForm.payment.value;
	
	var ErrorMSG 	= "Het formulier bevat de volgende fouten:\n";
	var errors = 0;
	if (q.length  < 4) 
	{
		ErrorMSG 	+= "\t* Het trefwoord is niet minimaal 4 tekens\n";
		errors ++;
	}
	
	if (prijs_type == "-")
	{
		ErrorMSG 	+= "\t* Selecteer een Prijsstelling\n";
		errors ++;
	}

	if (prijs_type == 1)
	{
		var prijs_min 	= document.Search2MailForm.prijs_min.value;
		var prijs_max 	= document.Search2MailForm.prijs_max.value;

		out = ","; // replace this
		add = "."; // with this
		temp = "" + prijs_min; // temporary holder

		while (temp.indexOf(out)>-1) 
		{
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add + 
			temp.substring((pos + out.length), temp.length));
			document.Search2MailForm.prijs_min.value = temp
			prijs_min	= temp;
		}
		if(isNaN(prijs_min)) 
			{ 
			window.alert('Dit is geen getal!');
			document.Search2MailForm.prijs_min.focus();
			ErrorMSG 	+= "\t* De minumumprijs is geen getal\n";
			errors ++;
		}

		out = ","; // replace this
		add = "."; // with this
		temp = "" + prijs_max; // temporary holder

		while (temp.indexOf(out)>-1) 
		{
			pos= temp.indexOf(out);
			temp = "" + (temp.substring(0, pos) + add + 
			temp.substring((pos + out.length), temp.length));
			document.Search2MailForm.prijs_max.value = temp
			prijs_max	= temp;
		}
		if(isNaN(prijs_max)) 
			{ 
			window.alert('Dit is geen getal!');
			document.Search2MailForm.prijs_max.focus();
			ErrorMSG 	+= "\t* De maximum is geen getal\n";
			errors ++;
		}
		
		if (prijs_max == '' || prijs_min == '' )
		{
			ErrorMSG 	+= "\t* De prijs is niet goed ingevuld\n";
			errors ++; 		
		}

		if (errors == 0)
		{
			if (prijs_min > prijs_max )
			{
			document.Search2MailForm.prijs_max.value = prijs_min
			document.Search2MailForm.prijs_min.value = prijs_max
			} 
		}
	}

	if (distance == "-")
	{
		ErrorMSG 	+= "\t* Selecteer een afstand\n";
		errors ++;
	}

	if (type_id == "-")
	{
		ErrorMSG 	+= "\t* Selecteer een Advertentie Type\n";
		errors ++;
	}

	if (!ValidEmail(email))
	{
		ErrorMSG 	+= "\t* Het email adres is ongeldig\n";
		errors ++;
	}

	document.Search2MailForm.errors.value = errors
	
	if (errors == 0)
	{
		document.Search2MailForm.Submit.disabled=false
	}
	else
	{
		document.Search2MailForm.Submit.disabled=true
		if (ShowError == 1)
		{
		alert(ErrorMSG);	
		}
	}
	
}

/*
EINDE CONTROLE SEARCH2MAIL FORMULIER
----------------------------------------------------------------------------------------------------------------
*/
function LoadTab(tab)
{
	ajax_loadContent('TabInhoud','TabInhoud.php?tab='+tab);	
	if (tab == 1)
	{
		document.search_form.action.value='search';
	}
	if (tab == 2)
	{
		document.search_form.action.value='postcode';
	}
	if (tab == 3)
	{
		document.search_form.action.value='login';
	}
}

function AdvSeach()
{
	var adv 	= document.search_form.adv.value;
	if (adv == 0)
	{
		document.search_form.adv.value = 1;
		ajax_loadContent('searching','Defaults.php?type=ADVSearch');
		this.obj('aanbodpagina').style.textDecoration="line-through";
		this.obj('aanbodpagina').style.fontStyle="italic";
		this.obj('i2dehands').style.textDecoration="line-through";
		this.obj('i2dehands').style.fontStyle="italic";
		this.obj('speurders').style.textDecoration="line-through";
		this.obj('speurders').style.fontStyle="italic";

	}
	else
	{
		document.search_form.adv.value = 0;
		ajax_loadContent('searching','Defaults.php');
		this.obj('aanbodpagina').style.textDecoration="none";
		this.obj('aanbodpagina').style.fontStyle="normal";
		this.obj('i2dehands').style.textDecoration="none";
		this.obj('i2dehands').style.fontStyle="normal";
		this.obj('speurders').style.textDecoration="none";
		this.obj('speurders').style.fontStyle="normal";
	}
}

function ADVSearchPrijs()
{
	var prijs_type	= document.search_form.prijs_type.value;

	if (prijs_type == 5 || prijs_type == 6 || prijs_type == 8 || prijs_type == 9)
	{
		this.obj('marktplaza').style.textDecoration="line-through";
		this.obj('marktplaza').style.fontStyle="italic";
	}
	else
	{
		this.obj('marktplaza').style.textDecoration="none";
		this.obj('marktplaza').style.fontStyle="normal";
	}
	
	
	if (prijs_type == 2 || prijs_type == 6 || prijs_type == 7 || prijs_type == 8 || prijs_type == 9 || prijs_type == 10)
	{
		this.obj('tweedehands').style.textDecoration="line-through";
		this.obj('tweedehands').style.fontStyle="italic";
	}
	else
	{
		this.obj('tweedehands').style.textDecoration="none";
		this.obj('tweedehands').style.fontStyle="normal";
	}

	if (prijs_type > 1)
	{
		this.obj('aanbodpagina').style.textDecoration="line-through";
		this.obj('aanbodpagina').style.fontStyle="italic";
	}
	else
	{
		this.obj('aanbodpagina').style.textDecoration="none";
		this.obj('aanbodpagina').style.fontStyle="normal";
	}
	
	if (prijs_type > 1)
	{
		this.obj('marktnet').style.textDecoration="line-through";
		this.obj('marktnet').style.fontStyle="italic";
	}
	else
	{
		this.obj('marktnet').style.textDecoration="none";
		this.obj('marktnet').style.fontStyle="normal";
	}
	
	if (prijs_type == 2 || prijs_type == 3 || prijs_type == 8 || prijs_type == 9)
	{
		this.obj('tweedehands').style.textDecoration="line-through";
		this.obj('tweedehands').style.fontStyle="italic";
	}
	else
	{
		this.obj('tweedehands').style.textDecoration="none";
		this.obj('tweedehands').style.fontStyle="normal";
	}

	ajax_loadContent('ADVSearchPrijs','Defaults.php?type=ADVSearchPrijs&prijs_type='+prijs_type);
}

function ADVSearchZipcode()
{
	var distance	= document.search_form.distance.value;
	ajax_loadContent('ADVSearchZipcode','Defaults.php?type=ADVSearchZipcode&distance='+distance);
}

function DeleteItem(q,type,type_id,sort_id,sort_type,url)
{
	var url = 'Defaults.php?type=DeleteItem&q='+q+'&delete='+type+'&type_id='+type_id+'&sort_id='+sort_id+'&sort_type='+sort_type+'&url='+url;
	ajax_loadContent('searching',url);
}

function Add2Favorites(AddID)
{
	var url = 'Defaults.php?type=Add2Favorites&AddID='+AddID;
	ajax_loadContent('searching',url);
}


function Wait(Mili)
	{
	setTimeout("return true",5000);
}

function LoadResult(q)
{
	var url = '/zoek/'+q;
	window.location.href=url;
}

/*
 * Zoeken vanuit checkboxes is klaar
 * Kijken of zoeken wat opgeleverd heeft
 * Bij een resultaat, deze laden via LOADREAULT()
 * Bij geen resultaat dit weergeven
*/
function SearchStop(q)
{
	var url = 'Defaults.php?type=SearchStop&q='+q;
	ajax_loadContent('searching',url);
}



function obj(menu)
	{
	return (navigator.appName == "Microsoft Internet Explorer")?this[menu]:document.getElementById(menu);
}

function HideDiv(treepart,hide)
	{
//	alert(this.obj(treepart).style.visibility+treepart);
	if (hide == 0)
		{
		this.obj(treepart).style.position="";
		this.obj(treepart).style.visibility="";
		}
		else
		{
		this.obj(treepart).style.position="absolute";
		this.obj(treepart).style.visibility="hidden";
	}
}

function togglevisibleNoIMG(treepart)
	{
	if (this.obj(treepart).style.visibility == "hidden")
		{
		this.obj(treepart).style.position="";
		this.obj(treepart).style.visibility="";
		}
		else
		{
		this.obj(treepart).style.position="absolute";
		this.obj(treepart).style.visibility="hidden";
	}
}

function SearchZipcode()
{
	var postcode = document.search_form.postcode.value;
	window.location.href='/postcode/'+postcode;
}

function get_stats(guid,site_id)
	{
	referrer = escape(document.referrer); 

	if((referrer == "") || (referrer == "undefined"))
		{
		referrer = ""; 
	} 
	pagina = escape(document.location.href); 
	screens = screen.width + "x" + screen.height; 
	screenc = (navigator.appName.indexOf("Microsoft") > -1) ? screen.colorDepth : screen.pixelDepth; 
	img = "http://www.easy-ringtones.nl/includes/stats/stats.php?guid=" + guid + "&pagina=" + pagina + "&site_id=" + site_id + "&referrer=" + referrer + "&screensize=" + screens + "&screencolors=" + screenc; 
	img = "<img src=\"" + img + "\" />"; 
	document.write(img);
}


//Swap image
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}











function Add2GoogleToolbar() {
	document.search_form.q.focus();
	ajax_loadContent('SearchDetail','/Defaults.php?type=GoogleToolbar');
	document.getElementById("SearchDetail").style.visibility="visible";
	document.getElementById("GoogleFirefox").style.visibility="visible";
	document.getElementById("GoogleFirefox").style.position="";
	document.getElementById("Blur").style.filter="alpha(opacity=30)";
}

function CloseGoogleToolbar() {
	document.search_form.q.focus();
	ajax_loadContent('SearchDetail','/Defaults.php');
	document.getElementById("SearchDetail").style.visibility="visible";
	document.getElementById("GoogleFirefox").style.visibility="hidden";
	document.getElementById("GoogleFirefox").style.position="absolute";
	document.getElementById("Blur").style.filter="alpha(opacity=100)";
}

function Add2GoogleEarth(q) {
	document.search_form.q.focus();
	ajax_loadContent('SearchDetail','/Defaults.php?type=GoogleEarth&q='+q);
	document.getElementById("SearchDetail").style.visibility="visible";
	document.getElementById("GooglePack").style.visibility="visible";
	document.getElementById("GooglePack").style.position="";
	document.getElementById("Blur").style.filter="alpha(opacity=30)";
}

function CloseGoogleEarth() {
	document.search_form.q.focus();
	ajax_loadContent('SearchDetail','/Defaults.php');
	document.getElementById("SearchDetail").style.visibility="visible";
	document.getElementById("GooglePack").style.visibility="hidden";
	document.getElementById("GooglePack").style.position="absolute";
	document.getElementById("Blur").style.filter="alpha(opacity=100)";
}


function ShowSearchDetail(q) {
	document.search_form.q.focus();
	ajax_loadContent('SearchDetail','/Defaults.php?type=LoadKeyword&q='+q);
	document.getElementById("SearchDetail").style.visibility="visible";
	document.getElementById("Blur").style.filter="alpha(opacity=30)";
}

function LoadKeyword(q)
{
	ajax_loadContent('LoadKeyword','/Defaults.php?type=LoadKeyword2&q='+q);
}


function LoadZipCodeFromEnter()
{
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(document.all) 
		{ 
			var e = window.event.keyCode; 
		}
		else
		{ 
			e = e.which; 
		}
		if (e == 13)//keycode 13 = enter
		{
			SearchZipcode()
		}
	}
}

function LoadResultsFromEnter()
{
	if(document.all) 
	{ 
		var e = window.event.keyCode; 
	}
	else
	{ 
		e = e.which; 
	}
	if (e == 13)//keycode 13 = enter
	{
		document.search_form.action.value='search';
		SearchForm();
	}
}

function LoginFromEnter()
{
	if(document.all) 
	{ 
		var e = window.event.keyCode; 
	}
	else
	{ 
		e = e.which; 
	}

	if (e == 13)//keycode 13 = enter
	{
		var email 		= document.search_form.email.value;
		var password 	= document.search_form.password.value;
		var error		= 0;
		if (email == "")
		{
			document.search_form.email.focus();
			error 		= 1;
		}
		if (password == "" && error == 0)
		{
			document.search_form.password.focus();
			error 		= 1;
		}
	
		if (error == 0)
		{
			document.search_form.action.value='login';
			document.search_form.submit();
		}
	}
}

//cookie uitlezen via JS
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function SetCookie() 
{
	if(arguments.length < 2) 
	{
		return; 
	}
	var n = arguments[0];
	var v = arguments[1];
	var d = 0;
	if(arguments.length > 2) 
	{
		d = parseInt(arguments[2]); 
	}
	var exp = '';
	if(d > 0) 
	{
		var now = new Date();
		then = now.getTime() + (d * 24 * 60 * 60 * 1000);
		now.setTime(then);
		exp = '; expires=' + now.toGMTString();
	}
	document.cookie = n + "=" + escape(String(v)) + '; path=/' + exp;
}

