
var _W = window;
var _P = parent;
var _D = document;

var is,xmlhttp,popupEls=[],nrSteps=15,dragObj,dragX,dragY,dragZ=1,close_n=new Image(13,13),close_h=new Image(13,13);

_D.onmouseup=function(){dragObj=_D.onselectstart=_D.onmousemove=null};

close_h.src = '/pics/close_hi.gif';
close_n.src = '/pics/close.gif';

function sniff(){
	var t=this,T=true,F=false,n=navigator,ua=n.userAgent.toLowerCase();
	t.ver=parseInt(n.appVersion);
	t.mac=ua.indexOf('mac')!=-1;
	t.win=!t.mac&&ua.indexOf('wind')!=-1||ua.indexOf('16bit')!=-1;
	t.safari=ua.indexOf('safari')!=-1&&t.mac;
	t.konq=ua.indexOf('konqueror')!=-1;
	t.khtml=t.safari||t.konq;
	t.opera=_W.opera?T:F;
	t.opera5=ua.indexOf('opera 5')!=-1||ua.indexOf('opera/5')!=-1;
	t.opera6=ua.indexOf('opera 6')!=-1||ua.indexOf('opera/6')!=-1;
	t.opera7=t.opera&&_D.createComment?T:F;
	t.ie5=t.ie55=t.ie6=F;
	t.ie=_D.all&&!t.opera&&ua.indexOf('msie')!=-1?T:F;
	if(t.ie){
		t.ieBox=_D.compatMode!='CSS1Compat';
		t.ie5=!_D.fireEvent?T:F;
		t.ie55=_D.fireEvent&&!_D.createComment?T:F;
		t.ie6=_D.fireEvent&&_D.createComment?T:F
	}
	t.gecko=!t.khtml&&!_D.all&&!t.opera&&_D.getElementById?T:F;
	t.ns4=n.appName=='Netscape'&&t.ver==4
}
is=new sniff();
op=is.opera;
function gE(e){if(_D.getElementById)return _D.getElementById(e);if(_D.all)return _D.all[e]}
function sE(e){e.style.visibility='visible'}
function hE(e){e.style.visibility='hidden'}
function SE(e){e.style.display=''}
function HE(e){e.style.display='none'}
function sX(e,x){op?e.style.pixelLeft=x:e.style.left=x+'px'}
function sY(e,y){op?e.style.pixelTop=y:e.style.top=y+'px'}
function sZ(e,z){e.style.zIndex=z}
function sW(e,w){op?e.style.pixelWidth=w:e.style.width=w+'px'}
function sH(e,h){op?e.style.pixelHeight=h:e.style.height=h+'px'}
function wH(e,h){e.innerHTML=h}

function getWinSize(what) {
	if (what == 'height')
		return (is.ie) ? (is.ieBox) ? _D.body.clientHeight : _D.documentElement.clientHeight : _W.innerHeight;
	if (what == 'width')
		return (is.ie) ? (is.ieBox) ? _D.body.clientWidth : _D.documentElement.clientWidth : _W.innerWidth;
}

function setEvent(el,evt,state,fn) {
	if (state) {
		if (el.addEventListener)
			el.addEventListener(evt,fn,false);
		if (el.attachEvent)
			el.attachEvent('on'+evt,fn);
	}
	else {
		if (el.removeEventListener)
			el.removeEventListener(evt,fn,false);
		if (el.detachEvent)
			el.detachEvent('on'+evt,fn);
	}
}

function makePopup(e, name, width, height, title, bgcolor, bordercolor, url) {
	if (popupEls[name]) killPopup(name,1);

	height += 30;
	var wh = getWinSize('height'), ww = getWinSize('width'), st, sl;
	if (!e) {
		e = new Object();
		e.clientX = ww/2-width/2;
		e.clientY = wh/2-height/2;
	}
	var top = e.clientY, left = e.clientX;
	if (is.ie) {
		sl = (is.ieBox) ? _D.body.scrollLeft : _D.documentElement.scrollLeft;
		st = (is.ieBox) ? _D.body.scrollTop  : _D.documentElement.scrollTop;
	}
	else {
		sl = _W.pageXOffset;
		st = _W.pageYOffset;
	}
	if (e.clientX + width > ww)
		left = ww-width-5;
	if (e.clientY + height > wh)
		top = wh-height-5;

	var popupEl = _D.createElement('div');
	popupEl.onmousedown=function(event){dragStart(event,this)};
	popupEl.className = 'popup';
	popupEl.style.border = '1px solid ' + bordercolor;
	sZ(popupEl, dragZ++);
	_D.body.appendChild(popupEl);
	
	popupEls[name] = popupEl;
	sizePopup(name,title,url,left+sl,top+st,width,height,bgcolor,nrSteps);
}

function sizePopup(name,title,url,left,top,width,height,bgcolor,step) {
	// New and improved: with slomogrow effect!
	var html, mouse, popupEl = popupEls[name];
	if (step < 0) {
		if (is.ie) {
			sW(popupEl, width+2);
			sH(popupEl, height+2);
		}

		title = '<img src="/pics/pics.php?txt='+escape(title)+'&amp;bg='+escape(bgcolor.substring(1,7))+'" />';
		mouse = (is.ie) ? 'onmouseover="this.src=close_h.src" onmouseout="this.src=close_n.src"' : '';
		html  = '<table class="popupCaption" style="background:'+bgcolor+'" cellpadding="0" cellspacing="0"><tr>';
		html += '<td>'+title+'</td>';
		html += '<td align="right" valign="middle" style="padding:3px"><img src="/pics/close.gif" class="ptr" width="13" height="13" alt="Close" onclick="killPopup(\''+name+'\',0)" '+mouse+' /></td>';
		html += '</tr></table>';
		wH(popupEl, html+'<iframe id="'+name+'_frm" frameborder="0" src="'+url+'" style="width:'+width+'px;height:'+(height-30)+'px"></iframe>');
		return;
	}
	sX(popupEl, left + step*width/nrSteps/2);
	sY(popupEl, top + step*height/nrSteps/2);
	sW(popupEl, width - step*width/nrSteps);
	sH(popupEl, height - step*height/nrSteps + (is.safari?2:0));
	setTimeout("sizePopup('"+name+"','"+title+"','"+url+"',"+left+","+top+","+width+","+height+",'"+bgcolor+"',"+(step-1)+")",0);	
}

var activePopupEl = '';
function killPopup(name,now) {
	activePopupEl = popupEls[name];
	if (activePopupEl) {
		try{gE(name+'_frm').contentWindow.hideWebcam()}catch(e){};
		var code = '_D.body.removeChild(activePopupEl);activePopupEl=\'\';popupEls[\''+name+'\']=null';
		if (now)
			eval(code);
		else
			setTimeout(code,10);
	}
}

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp=new ActiveXObject('Msxml2.XMLHTTP')
	} catch(e) {
		try {
			xmlhttp=new ActiveXObject('Microsoft.XMLHTTP')
		} catch(e) {
			xmlhttp=false
		}
	}
@else
	xmlhttp=false
@end @*/
if (!xmlhttp && document.createElement) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch(e) {
		xmlhttp=false
	}
}

function dragStart(e,el) {
	if (!e)e=_W.event;
	dragObj = el;
	dragObj.style.zIndex = dragZ++;
	dragX = e.clientX - dragObj.offsetLeft;
	dragY = e.clientY - dragObj.offsetTop;
	_D.onmousemove=dragMove;
	_D.onselectstart=function(){return false};
}

function dragMove(e) {
	if (!e)e=_W.event;
	if(dragObj) {
		sX(dragObj,e.clientX - dragX);
		sY(dragObj,e.clientY - dragY);
	}
}


function toggleZoeken(bChange, bKeyword) {
	var lyr = gE('zoekDiv');
	var ct = gE('contenttable');
	var pb = gE('product_breed_id');
	var pg = gE('productgroep_id');
	
	gE('top_submit').disabled = false;
	
	if(bKeyword == false) {
		gE('keyword_product').value = '';
		eSelect = document.forms[0]['product_breed_id'];
		eSelect.options[0].selected = true;
	}
	
	SE(lyr);
	pg.parentNode.parentNode.style.display = (bKeyword == false) ? '' : 'none';
	pb.parentNode.parentNode.style.display = (bKeyword == true) ? '' : 'none';
	if(bKeyword == false) {
		pg.options[0].selected = true;
	} else if(bKeyword == true && _D.forms[0]['keyword_product'].value == '') {
		pb.options[0].selected = true;
	}
		
	ct.style.height = ((document.body.scrollHeight) - 200) + 'px';	
	if(lyr.style.display != 'none') {
		if(bChange == true) {
			HE(lyr);
			ct.style.height = (is.ie) ? ((document.body.clientHeight)) + 'px' : 'auto';
		}
	}
} // End of function


var tel_toggled = false;
function toggleTel( bid, uid ) {
	if( !tel_toggled && typeof bid == 'number') {
		tel_toggled = true;
		var tel1 = gE('telnr1');
		var tel2 = gE('telnr2');

		// get the phone number(s) of the company with the given identifier (bid)
		xmlhttp.open('GET', '/phoundry/custom/Inputtypes/gettelephone.php?bid=' + escape(bid) + '&' + Math.random(), false);
		xmlhttp.send(null);
		var response = xmlhttp.responseText;
		var opts = response.split("|");

		if(opts.length >= 2) {
			tel1.style.display = 'block';
			tel1.childNodes[1].innerHTML = opts[0];

			tel2.style.display = 'block';
			tel2.childNodes[1].innerHTML = opts[1];

			// Send an e-mail to the company with the given identifier (bid)
			// that a request for its' tel. number is done by the user with the
			// given identifier (uid)
			xmlhttp.open('GET', '/phoundry/custom/Inputtypes/sendtelephone.php?bid=' + escape(bid) + '&' + 'uid=' + escape(uid) + '&' + Math.random(), false);
			xmlhttp.send(null);
		}
	}
}


function searchFeatures(bChange) {
	var lyr = gE('zoekDiv');
	var ct = gE('contenttable');
	var pb = gE('product_breed_id');
	var p = gE('product_id');
	
	SE(lyr);
	p.parentNode.parentNode.style.display = 'none';
	pb.parentNode.parentNode.style.display = 'none';
	ct.style.height = ((document.body.scrollHeight) - 110) + 'px';	
	if(lyr.style.display != 'none') {
		if(bChange == true) {
			HE(lyr);
			ct.style.height = (is.ie) ? ((document.body.clientHeight) + 10) + 'px' : 'auto';
		}
	}
}



function changeFormat(elem) {
	var sCode = elem.options[elem.selectedIndex].alt
	if(sCode != '') {
		format(sCode);
	}
} // End of function


function format(format) {
	var fields = new Array('tel_landcode', 'mobiel_landcode', 'fax_landcode');
	for(var i=0; i < fields.length; i++) {	
		var elem = document.getElementById(fields[i]);
		elem.value = format;
	}
} // End of function



function resetProduct(productgroup_id) {
	var F = _D.forms['searchform'], el = F['product_id'], idx = 1;
	F['product_breed_id'].selectedIndex=F['product_id'].selectedIndex=0;
	if(productgroup_id != '') {
		F['product_breed_id'].disabled=F['product_id'].disabled=true;
		while(el.options[idx]) {
			el.options[idx] = null;
		}
	} else {
		F['product_breed_id'].disabled=F['product_id'].disabled=false;
	}
} // End of function

function disableProductGroup(product_id) {
	var F = _D.forms['searchform'];
	if(product_id != '') {
		F['productgroep_id'].selectedIndex = 0;
		F['productgroep_id'].disabled = true;
	} else {
		F['productgroep_id'].disabled = false;
	}
} // End of function



function checkSearchform() {
	//var re = /^-?[0-9]+([\.,][0-9]+)?$/
	var input = false;
	var sprice = null;
	var eprice = null;
	for(i=0; i < document.searchform.elements.length; i++) {
		var elem = document.searchform.elements[i];
		if(elem.type == 'text') {
			var value = elem.value;
			if(elem.name == 'keyword' && elem.disabled == false)  {
			   	input = (value != '') ? true : input;
			   	return (input) ? true : message();			   	
			} else if(elem.name == 'advertentie_nummer') {
				if(elem.value !== '' && !/^\+|-?[0-9]+$/.test(elem.value)) {
					alert(mesInteger);
					return false;
				}
			} else if(elem.name == 'prijs_vanaf') {
				if(elem.value !== '' && !/^-?[0-9]+([\.,][0-9]+)?$/.test(elem.value)) {
					alert(mesNumbersonly);
					return false;
				} else {
					elem.value = elem.value.replace(/\,/, '.');
					input = (elem.value != '') ? true : input;	
					if(input) { sprice = (elem.value*1); }
				}				
			} else if(elem.name == 'prijs_totmet') {
				if(elem.value !== '' && !/^-?[0-9]+([\.,][0-9]+)?$/.test(elem.value)) {
					alert(mesNumbersonly);
					return false;
				} else {
					elem.value = elem.value.replace(/\,/, '.');
					input = (elem.value != '') ? true : input;	
					if(sprice && sprice >= (elem.value*1) && elem.value != '') { 
						alert(mesMinimunprice);
						return false;
					}
				}
			// Later toegevoegd?	
			} else {
				input = (elem.value != '') ? true : input;
			}
		} else if(elem.type == 'select-one') {
				var value = elem.options[elem.selectedIndex].value;
				input = (value != '') ? true : input;
		} 
	}	
	return (input) ? true : message();
	
	function message() {
		alert(searchmessage);
		return false;
	} // End of function

} // End of function


function rotate(image, color) {
	var imgsrc = image.src;
	if(color !== '') {
		imgsrc += '&cl=' + color;
		imgsrc = imgsrc.replace(/#/, '');
		image.src = imgsrc;
	} else {
		imgsrc = imgsrc.replace(/&cl=\w*/, '');
		image.src = imgsrc;
	}
} // End of function


function hiliterow(row) {
	row.style.cursor = (is.ie) ? 'hand' : 'pointer';
	return false;
}

function defaultrow(row) {
	row.style.cursor = 'default';
	return false;
}


function sortColumn(eLink) {

	var sOrderBy = eLink.href.substr((eLink.href.lastIndexOf('#')) + 1, eLink.href.length);		
	if(sOrderBy != '') {
		eLink.href = '#' + (/\|asc$/.test(sOrderBy) ? sOrderBy.replace('\|asc', '|desc') : sOrderBy.replace('\|desc', '|asc'));
		for(var i = 0; i < eLink.parentNode.parentNode.cells.length; i++) {
			var eCell = eLink.parentNode.parentNode.cells[i];
			if(eCell.getElementsByTagName('A')[0]) {			
				eCell.getElementsByTagName('A')[0].className = (i == eLink.parentNode.cellIndex) ? (/\|asc$/.test(sOrderBy)) ? 'clicked-asc' : 'clicked-desc' : 'sort';
			}
		}
		
		sQuerystr = unescape(document.location.href);
		sQuerystr = sQuerystr.replace(/\?did=[0-9]{1,}/, '?offset=0'); /* stip the diveka advertisement id if set */

		var sQuerystr = sQuerystr.replace(/^.*\?/gi, '');
		sQuerystr = (sQuerystr.match(document.location.href) == null) ? sQuerystr : unescape('offset=0');
			
		sQuerystr = sQuerystr.replace(/offset=[0-9]{1,}/gi, 'offset=0'); /* reset the offset paramater to 0 ( = 1 first page ) */
		sQuerystr = ( (/orderby=.*\|(asc|desc)/.test(sQuerystr)) ) ? sQuerystr.replace(/orderby=.*\|(asc|desc)/gi, 'orderby=' + escape(sOrderBy)) : sQuerystr + '&orderby=' + escape(sOrderBy);			
		var sLocation = document.location.href.replace(/\?.*$/gi, '');		
		document.location.href = sLocation + "?" + sQuerystr;
	}
	return false;
}


function infoWindow(e, title, url) {
	var event = (e) ? e : event;
	title = title.replace(/(<([^>]+)>)/ig,"");
	title = escape(title)	
	makePopup(event, 'info', 300, 150, title, '#ee3224', '#0f3a21', url);	
	return false;
}


function beforeSave(title, url) {
	//title = title.replace(/(<([^>]+)>)/ig,"");
	//title = escape(title)	
	//makePopup(null, 'info', 300, 150, title, '#ee3224', '#0f3a21', url);
	//var eWindow = popupEls['info'];
	//return eWindow;
}



