// currency format

function resize(image_seq) {
  var i=0;
  var add_size = 50;
  if (navigator.appName == 'Netscape') i=40;
	//if(document.images[1].width < 270) add_size=300;
  if (document.images[image_seq]) window.resizeTo(document.images[image_seq].width +add_size, document.images[image_seq].height+add_size);
  self.focus();
}

function formatCurrency(prefix, suffix, k_sep, c_sep, num) {
/*
	var prefix = '';
	var suffix = '&nbsp;&euro;';
	var k_sep = ',';
	var c_sep = '.';
*/
	var num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();

	if(cents < 10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length - ( 1 + i )) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + k_sep + num.substring(num.length - (4 * i + 3));

	return (((sign)? '' : '-') + prefix + num + c_sep + cents + suffix);
}

// form validation functions -- Chee Leong 21/09/2004
function is_empty(value) {
	if(value == '')
		return true;
	else
		return false;
}


function trim(str) {
	return str.replace(/^\s+/g, '');
}

// form validation functions -- yploo 23/09/2004
// Required Field
// - td id for showing the errormsg should be <element's name>_err
// - the element only considered as required field while <element's name>_err appear in that form
//  Confirmation
// - the function will validate the confirmation field while 'confirm' word appear in the element name and 
//   the remaining word for that element name is an another element's name in the form.
//   eg. if confirmemail and email is two element's name in the form, then function will check the confirmation
// Email
// - if the element's name content the 'email' word, then function will validate the email format
// Tel
// - if the element's name content the 'tel' word, then function will validate the tel format

function validateform(docform,docall) {
	var d = docform;
	var pass =true;
	var i=0;
	var errorColor = '#E1CCE1';
 
	for(i=0; i<d.elements.length; i++) {
		//check if it have td which id is <inputname>_err
		if(docall[d.elements[i].name+'_err']){
			if(d.elements[i].value==''){
				docall[d.elements[i].name+'_err'].style.display = 'inline';
				docall[d.elements[i].name].style.background = errorColor;
				pass =  false;
			}
			else{
				docall[d.elements[i].name+'_err'].style.display = 'none';
				docall[d.elements[i].name].style.background = '#FFFFFF';
				//check confirmation
				if(d.elements[i].name.search(/confirm/gi)!=-1){
					if((d[d.elements[i].name.replace(/confirm/gi,'')])&&d[d.elements[i].name.replace(/confirm/gi,'')].value!=''){
						if(d[d.elements[i].name.replace(/confirm/gi,'')].value!=d.elements[i].value){
							docall[d.elements[i].name+'_err'].style.display = 'inline';
							docall[d.elements[i].name].style.background = errorColor;
							pass = false;
						}
					}
				}
				//validate for tel input
				if(d.elements[i].name.search(/tel/gi)!=-1){
					
					//alert('tel');
				}
				
				//validate numeric input
				
				if(d.elements[i].name.search(/postcode/gi)!=-1){
					
					if(!is_number(d.elements[i].value)){
						docall[d.elements[i].name+'_err'].style.display = 'inline';
						docall[d.elements[i].name].style.background = errorColor;
						pass = false;
						
					}
				}
				
				//validate email input
				if(d.elements[i].name.search(/email/gi)!=-1){
					if(!is_email(d.elements[i].value)){
						docall[d.elements[i].name+'_err'].style.display = 'inline';
						docall[d.elements[i].name].style.background = errorColor;
						pass = false;
						
					}
				}
			}
			
		}
		
	}
	return pass;
	
}

function is_email(email) {
	var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;
	var flag = pattern.test(email);
	if(!flag)
		return false;
	else
		return true;
}

// check numeric
function is_number(value){
	var checkOK = "0123456789";
	var i;
	var j;
	
	for (i = 0 ; value.length > i ; i++) {
		ch = value.charAt(i);
		for (j = 0 ; checkOK.length > j ; j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)				
			return false;
	}
	return true;
}

function new_window(mypage, myname, w, h, scroll) {

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize'
	win = window.open(mypage, myname, winprops);

	if (parseInt(navigator.appVersion) >= 4) {
		win.focus();
	}
}
// This function strip all passed-in text of any html tags, you can pass in as many argument as you like.
function stripHTML(text){
	var re= /<\S[^><]*>/g
	str = text;
	return str.replace(re,"");
}

// This function convert all special chars to its html entities correspondent eg < to &lt;
function html2entities(text){
	var re=/[(<>"'&]/g
	//for (i=0; i<arguments.length; i++)
	//arguments[i].value=arguments[i].value.replace(re, function(m){return replacechar(m)})
	str = text;
	return str.replace(re, function(m){return replacechar(m)});
	
}

function replacechar(match){
	if (match=="<")
	return "&lt;"
	else if (match==">")
	return "&gt;"
	else if (match=="\"")
	return "&quot;"
	else if (match=="'")
	return "&#039;"
	else if (match=="&")
	return "&amp;"
}
function findPosX(obj, left)
{
	var curleft = left;
		if(obj.offsetParent)
			while(1) 
			{
		  		curleft += obj.offsetLeft;
		 		 if(!obj.offsetParent)
				 break;
		        obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft += obj.x;
			
	return curleft;
}

function findPosY(obj, top)
{
	var curtop = top;
		if(obj.offsetParent)
			while(1)
			{
				  curtop += obj.offsetTop;
				  if(!obj.offsetParent)
					break;
				  obj = obj.offsetParent;
			}
		else if(obj.y)
			curtop += obj.y;
			
	return curtop;
}


function showPriceDetail(recordPosition, magPic, magPriceBe, magPriceCh){

	document.getElementById('magazinePriceBe').innerHTML  = 'BEL : ' + magPriceBe + ' €';
	document.getElementById('magazinePriceCh').innerHTML  = 'CH : ' + magPriceCh + ' fs';
	intY = findPosY(document.getElementById('imgPrice' + recordPosition), 15); /* set the color palatte position */

	//show all at top
	//if(intY >= 500){
		intY = intY-152;
	//}

	document.getElementById('rolloverBox').style.top  	 = intY;		
	document.getElementById('rolloverBox').style.left  	 = findPosX(document.getElementById('imgPrice' + recordPosition), 2);
	document.getElementById('rolloverBox').style.display ='inline';
	document.getElementById('picLogo').src  = magPic;
}

function closePriceDetail(){
	document.getElementById('rolloverBox').style.display ='none';
}

// new function to adjust pop up
var timer = null;
var pic   = new Image();

function AdjustPopupPicture(imagesource, imagearea) {
	pic.src = imagesource;
	timer = setInterval("IsPictureLoaded(imagearea)", 1);
}

function IsPictureLoaded(imagearea) {
	if (pic.complete) {
		clearInterval(timer);
		window.moveTo(0, 0);
		GetPicSize();
		
		if(picwidth > 550){
			picheight = 550 / picwidth * picheight;
			picwidth  = 550;
		}
		
		FitPic();
		MoveWindow();
		document.getElementById(imagearea).innerHTML = '<img src="' + pic.src + '" alt="" width="' + picwidth +'">';
	}
}

function GetPicSize() {
	picheight = parseInt(pic.height);
	picwidth  = parseInt(pic.width);		
}

function FitPic() {

	if (window.innerWidth){
		iWidth  = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth  = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	
	iWidth  = picwidth - iWidth + 30;
	iHeight = picheight - iHeight + 146;
	
	window.resizeBy(iWidth, iHeight);
}

function MoveWindow() {
	var winl = (screen.width - picwidth) / 2;
	var wint = (screen.height - (picheight + 150)) / 2;

	window.moveTo(winl, wint);
}

function GoToUrl(url) {
	window.location.href = url;
}
