/*
 * $Id: common.js 1794 2009-04-09 11:24:34Z joris $
 */

function newWindow(mypage,myname,w,h,features) {
	if(screen.width){
		var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
	}
	else{winl = 0;wint =0;}
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;

	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

if ( typeof window.eId != 'function' ) {
	function eId() {
        var elements = new Array();

        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string') { element = document.getElementById(element); }

            if (arguments.length == 1) { return element; }

            elements.push(element);
        }

        return elements;
	};
}
function copyAddress(frm) {
	var elems = new Array('adres','huisnummer','postcode','plaats','land');
	while (elem = elems.pop()) {
		frm.elements['shop[' + elem + '2]'].value = frm.elements['shop[' + elem + ']'].value;
	}
}


function getObj(name) {
  if (document.getElementById) {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  }
  else if (document.all) {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers) {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}

// Start actionattacher on window load
var actionAttacher = pDomApi.getActionAttacher();
//pDomApi.addEvent(window, 'domload', function() { actionAttacher.attach(); });
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('a', 'printPage', 'click', function(e) {
	window.print();
	pDomApi.preventDefault(e);
}));
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('a', 'external', 'click', function(e) {
    if (!e) { var e = window.event; } targ = e.target ? e.target : e.srcElement;
    while (targ.nodeType != 1 || targ.tagName.toLowerCase() != 'a') {
    	targ = targ.parentNode;
    }
    window.open(targ.href);
    pDomApi.preventDefault(e);
}));

actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('select','langSelect', 'change', function(e) {

	var event = e || window.event;
	var target = event.target ? event.target : event.srcElement;

	href = location.protocol + '//' + location.hostname + location.pathname;

	var lang = target.value;
	var searchStr = location.search;
	if ( location.search.search(/lang=[a-z]{2}/) != -1 ) {
		searchStr = location.search.replace(/lang=[a-z]{2}/,'lang=' + lang);
	} else {
		searchStr = ((searchStr == '') ? '?lang=' : searchStr + '&lang=') + lang;
	}
	window.location = href + searchStr;
}));
// make a submit button from a link
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('a','submit', 'click', function(e) {

	pDomApi.preventDefault(e);

	var rel = this.getAttribute('rel');
	if (rel.length > 0) {
		eId(rel).submit();
	}
}));

// Password field in user menu
var textStore = {};

function wipeTextOnFocus(input) {

    textStore[input.getAttribute('id')] = input.value;

    pDomApi.addEvent(input, 'focus', function(e) {
        if (input.value == textStore[input.getAttribute('id')]) {
            input.value = '';
        }
    });

    pDomApi.addEvent(input, 'blur', function(e) {
         if(input.value == '') {
            input.value = textStore[input.getAttribute('id')];
        }
    });
}

function replacePasswordBox(passwd) {
    var text = document.createElement('input'); // create new input element
    text.setAttribute('type', 'text'); // type = text
    text.setAttribute('value', passwd.getAttribute('value'));
    text.setAttribute('tabIndex', passwd.getAttribute('tabIndex'));
    text.className = passwd.className;
    text.setAttribute('readonly', 'readonly');
    passwd.setAttribute('value', '');
    //passwd.setAttribute('class', 'password');
    passwd.parentNode.insertBefore(text, passwd);
    passwd.parentNode.removeChild(passwd);

    pDomApi.addEvent(text, 'focus', function (e) {
        text.parentNode.insertBefore(passwd, text);
        passwd.parentNode.removeChild(text);
        passwd.focus();
    });

    pDomApi.addEvent(passwd, 'blur', function(e) {
        if (passwd.value == '') {
            passwd.parentNode.insertBefore(text, passwd);
            passwd.parentNode.removeChild(passwd);
        }
    });
}


var stdEmailContent = '';
function newsletterFocus() {
	elem			= document.forms.newsletterBlock.email;
	stdEmailContent	= document.forms.newsletterBlock.email.value;

	pDomApi.addEvent(elem, 'focus', function(e) {
		if (!e) { var e = window.event; } targ = e.target ? e.target : e.srcElement;

		if (targ.value == stdEmailContent) {
			targ.value = '';
		}
	});

	pDomApi.addEvent(elem, 'blur', function(e){
		if (!e) { var e = window.event; } targ = e.target ? e.target : e.srcElement;

		if (targ.value == '') {
			targ.value = stdEmailContent;
		}
	});


}

// Password field in user menu
pDomApi.addEvent(window, 'domload', function() {
    var passwd, email;

    if (passwd = eId('userMenuPass')) {
        replacePasswordBox(passwd);
    }
    if (email = eId('userMenuEmail')) {
        wipeTextOnFocus(email);
    }

	if (document.forms.newsletterBlock) {
		newsletterFocus();
	}

	init();


	// Password field in user menu
	pDomApi.addEvent('userSelect', 'change', function(e) {
		if (!e) { var e = window.event; } targ = e.target ? e.target : e.srcElement;

		var userId		= targ.options[targ.selectedIndex].value;
		var redirectUrl = '/portal/logon/loginAsOtherUser.cmd/?userId=' + userId;

		window.location.replace(redirectUrl);
	});
});

function init() {
	pDomApi.addEvent(document.getElementById('persYes'), 'click', function() {
		pDomApi.setClassName(document.getElementById('choiceListFields'), 'show','hide');
	});

	pDomApi.addEvent(document.getElementById('persNo'), 'click', function() {
		pDomApi.setClassName(document.getElementById('choiceListFields'), 'hide','show');
	});

	var persYes = document.getElementById('persYes');
	if ((persYes != undefined) && persYes.checked) {
		pDomApi.setClassName(document.getElementById('choiceListFields'), 'show','hide');
	}

	pDomApi.addEvent(document.getElementById('orderProductButton'), 'click', function(e) {
		if (!e) {
			var e = window.event;
		}

		document.forms['choiceList'].submit();

		pDomApi.preventDefault(e);
	});
	// Teamselect
	pDomApi.addEvent(document.getElementById('clubSelect'), 'change', function(e) {
		switch(this.value) {
			case 'mu':
				// redirect to manchester united
				window.location.replace('http://www.manchesterunitedfanshop.nl');
			break;
			case 'fcb':
				// redirect to manchester united
				window.location.replace('http://www.barcelonafanshop.nl');
			break;
			case 'rm':
				// redirect to manchester united
				window.location.replace('http://www.realmadridfanshop.nl');
			break;
		}

	});
}
//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

//kinda important
var AutoClose = true;

//don't touch
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
    'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
    'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
    'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}
