/* -------------- prototypes ----------------------*/

if (typeof(Array.prototype.push) == 'undefined'){
	Array.prototype.push = function(aValue){
		this[this.length] = aValue;
	}
}

function entries (collection) {
    var result = [];  // This is our real duck.

    for (var i = 0; i < collection.length; i++)
        result.push(collection[i]);

    return result;
}

Function.prototype.bind = function(object) {
    var method = this;
    var oldArguments = entries(arguments).slice(1);
    return function() {
        var newArguments = entries(arguments);
        return method.apply(object, oldArguments.concat(newArguments));
    };
}

/* -------------- end prototypes ----------------------*/

var dragging = false;

var VATArray = new Array(25);
VATArray['15'] = 'AT';
VATArray['22'] = 'BE';
VATArray['54'] = 'CY';
VATArray['55'] = 'CZ';
VATArray['56'] = 'DE';
VATArray['58'] = 'DK';
VATArray['63'] = 'EE';
VATArray['67'] = 'ES';
VATArray['69'] = 'FI';
VATArray['74'] = 'FR';
VATArray['77'] = 'GB';
VATArray['226'] = 'GB';
VATArray['89'] = 'GR';
VATArray['100'] = 'HU';
VATArray['102'] = 'IE';
VATArray['110'] = 'IT';
VATArray['132'] = 'LT';
VATArray['133'] = 'LU';
VATArray['134'] = 'LV';
VATArray['150'] = 'MT';
VATArray['163'] = 'NL';
VATArray['176'] = 'PL';
VATArray['180'] = 'PT';
VATArray['192'] = 'SE';
VATArray['195'] = 'SI';
VATArray['197'] = 'SK';

var VATSpanText = new Array(25);
VATSpanText['BE'] = '9';
VATSpanText['CY'] = '9';
VATSpanText['DK'] = '8';
VATSpanText['DE'] = '9';
VATSpanText['EE'] = '9';
VATSpanText['FI'] = '8';
VATSpanText['FR'] = '11';
VATSpanText['GR'] = '9';
VATSpanText['HU'] = '8';
VATSpanText['IE'] = '8';
VATSpanText['IT'] = '11';
VATSpanText['LV'] = '11';
VATSpanText['LT'] = '9 or 12';
VATSpanText['LU'] = '8';
VATSpanText['MT'] = '8';
VATSpanText['NL'] = '12';
VATSpanText['AT'] = '9';
VATSpanText['PL'] = '10';
VATSpanText['PT'] = '9';
VATSpanText['SI'] = '8';
VATSpanText['SK'] = '9 or 10';
VATSpanText['ES'] = '9';
VATSpanText['CZ'] = '8, 9 or 10';
VATSpanText['GB'] = '9 or 9+3 or 2+3';
VATSpanText['SE'] = '12';

var popups = new Array();
addLoadEvent(init);
function SetVATPrefix()
{
return;
	aVATBox = document.getElementById('vatcode');
	aVATSpan = document.getElementById('vatspan');
	aVatRow1 = document.getElementById('vatblk');
	aVatRow2 = document.getElementById('vatinfo');
	aCountrySlct = document.getElementById('countryid');
	aValue = aCountrySlct.options[aCountrySlct.options.selectedIndex].value;
	if (VATArray[aValue] != null)
	{
		aVATBox.value = VATArray[aValue];
		aVATSpan.innerHTML = '(' + label.write('common_numbercountrycode','Number of positions after countrycode: ') + VATSpanText[aVATBox.value] + ')';
		aVatRow1.style.visibility = "visible";
		aVatRow2.style.visibility = "visible";
	}
	else
	{
		aVatRow1.style.visibility = "hidden";
		aVatRow2.style.visibility = "hidden";
	}
}

var dX = 0;
var dY = 0;
var dragOBJ;

function init(){
	document.body.onmousemove = dragLayerFromBody;
	checkBrowser();
}

function dragLayerFromBody(e){
	if(dragging){

		if (!e) var e = window.event;

		var posx = 0;
		var posy = 0;
		if (e.pageX || e.pageY)
		{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			posx = e.clientX + document.documentElement.scrollLeft;
			posy = e.clientY + document.documentElement.scrollTop;
		}

		if(e.type == "mousemove" && dragging){
			dragOBJ.style.left = (posx - dX) + 'px';
			dragOBJ.style.top = (posy - dY) + 'px';
		}
		if(e.type == "mouseup" /*|| e.type == "mouseout"*/){
			dragging = false;
			alert("done");
		}

	}
}

function dragLayer(e, id){
	var targ;
	if (!e) var e = window.event;
	var targ = document.getElementById(id);
	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.documentElement.scrollLeft;
		posy = e.clientY + document.documentElement.scrollTop;
	}

	if(e.type == "mousedown"){
		dragObj = getTarget(e);
		dX = posx - parseInt(targ.style.left);
		dY = posy - parseInt(targ.style.top);
		//if(dY < 21)//restricts movement to the top 21 px
		dragging = true;
		dragOBJ = targ;
	}

	if(e.type == "mousemove" && dragging){
		targ.style.left = (posx - dX) + 'px';
		targ.style.top = (posy - dY) + 'px';
	}
	if(e.type == "mouseup"){
		dragging = false;
	}
}

function hideUploader(){
	uploadpopup.hide(popups.length-1);
}

function signout()
{
	document.location = "default.aspx?pageid=1&logout=true";
}

function checkPasswordBoxes()
{
	orgPassword = document.getElementById('password');
	newPassword = document.getElementById('newpassword');
	cnfPassword = document.getElementById('confirmnewpassword');
	if (orgPassword.value.length < 1)
	{
		alert(label.write('common_originalpasswordrequired','Its required to enter your original password'));
		orgPassword.focus();
		return false;
	}
	else if (newPassword.value.length < 1)
	{
		alert(label.write('common_newpasswordrequired','Its required to enter a new password'));
		newPassword.focus();
		return false;
	}
	else if (newPassword.value != cnfPassword.value)
	{
		alert(label.write('common_passwordnomatch','Your new and confirmed password don\'t match,\nplease enter them again'));
		newPassword.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function editJob(aID)
{
	document.location = 'jobhandler.aspx?job_id='+aID;
}

function replaceTextNode(rootNodeObject, newTextNode){
 	if(rootNodeObject.childNodes.length > 0){
		objToReplace = rootNodeObject.childNodes[0];
		if(objToReplace.nodeType == 3){
			rootNodeObject.removeChild(objToReplace);
			rootNodeObject.insertBefore(newTextNode, null);
		}else{
			return false;
		}
	}else{
		rootNodeObject.appendChild(newTextNode);
	}
 }

function toggleFtpDiv(aDivId)
{
	var aDiv = document.getElementById(aDivId);
	if (aDiv)
	{
		aDiv.style.display = (aDiv.style.display == "none" || aDiv.style.display == "") ? "block" : "none";
	}
}

function getStyle(el,styleProp)
{
	if(typeof(el) == "String"){
		var x = document.getElementById(el);
	}else{
		var x = el;
	}
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

var uploadpopup;
function showUploader(e)
{
	uploadpopup = new Popup("uploader");
	uploadpopup.setCaption(label.write("common_choosepictures","Choose pictures"));

	var d = document.createElement('div')
	d.id = "img-uploader";

	uploadpopup.setContent(d);

	flashObj.write('img-uploader');
	uploadpopup.positionToEvent(e, 0,-250);

}
function showPopupInfo(e,aId)
{
	var aInfoTxt = "";
	switch(aId)
	{
		case 0:
			aInfoTxt = label.write("common_allwithin48","All your orders are delivered within 48 hours.");
			break;
		case 1:
			aInfoTxt = label.write("common_jobestimation", "This is an estimation of the price of this job, the real price may differ.");
			break;
		case 2:
			aInfoTxt = label.write("common_promotionalinfo","A promotional code, consists of a number followed by 10 letters. For example '123ABCDEFGHIJ'. Enter a valid promotional code to receive a discount.");
			break;
		default:
			aInfoTxt = "";
			break;
	}
	var d = document.createElement('div');
	d.innerHTML = aInfoTxt;
	infopopup = new Popup("info");
	infopopup.setCaption("Information");
	infopopup.setContent(d);
	infopopup.positionToEvent(e, 0,0);
}
function killDragging(){
}

function checkBrowser(){
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;
	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (checkIt('firefox')) browser = "Firefox"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}
	if(! (browser == "Internet Explorer" || browser == "Firefox" || (browser == "Safari" && version > 3 ))){

		if(document.getElementById('subheader')){
			var alertString = label.write("common_browsersorry","Sorry, your browser is not fully supported by Misterclipping, see our list of <a href='/default.aspx?pageid=45'>compatible browsers</a>");
			document.getElementById('subheader').innerHTML = alertString;
			document.getElementById('subheader').style.display = 'block';
		}
	}
}

function addLoadEvent(fn){
	if (window.addEventListener) {
		window.addEventListener("load", fn, false);
	}else if (document.addEventListener) {
		document.addEventListener("load", fn, false);
	}else if (window.attachEvent) {
		window.attachEvent("onload", fn);
	}else if (typeof window.onload == "function") {
		var fnOld = window.onload;
		window.onload = function(){
			fnOld();
			fn();
		};
	}else {
		window.onload = fn;
	}
}

function addUnloadEvent(fn){
	if (window.addEventListener) {
		window.addEventListener("unload", fn, false);
	}else if (document.addEventListener) {
		document.addEventListener("unload", fn, false);
	}else if (window.attachEvent) {
		window.attachEvent("onunload", fn);
	}else if (typeof window.onunload == "function") {
		var fnOld = window.onunload;
		window.onload = function(){
			fnOld();
			fn();
		};
	}else {
		window.onunload = fn;
	}
}

function validateCode(ev){
	if(document.getElementById('actioncode')){
		//if (ev.which < 48){ return; }
		var input = document.getElementById('actioncode');
		input.title = label.write("common_enterpromocode","Enter a valid promotional code to receive a discount");
		var code = input.value;
		var rx = /\d+[a-zA-Z]{10}/
		//document.getElementById('_ctl0_jobDescription').value = job.xmlURL+'?jobid='+job.jobID+'&function=validateActionCode&actioncode='+document.getElementById('actioncode').value;
		input.style.color ='';
		
		if(rx.exec(code)||code==""){
			input.disabled = true;
			try{
				AjaxRequest.get(
					{
						'url': job.xmlURL+'?jobid='+job.jobID+'&function=validateActionCode&actioncode='+document.getElementById('actioncode').value
						,'onSuccess': function(req){
						        job.getLatestPictures();
								var aXML = req.responseXML;
								if (aXML.firstChild)
								{
									//var aDiscount = aXML.firstChild.getAttribute("value");
									var aDiscount = aXML.firstChild.attributes.item(0).nodeValue;
									if (aDiscount != "0")
									{
										job.reloadCosts();
										input.style.color ='green';
									}
									else
									{
										input.title = label.write("common_invalidcode","Invalid promotional code entered");
										input.style.color ='red';
									}
								}
								input.disabled = false;
							}
						,'onError': function(req){ alert('ERROR!\nStatusText='+req.statusText); input.disabled = false;}
					}
				);
			}catch(e){
			    alert(e.description)
			}
		}
	}
	return true;
}


function highlightRow(sender)
{
    status = sender.tagName +" " + sender.className;  
    sender.oldClassName = sender.className;
    sender.className = "highlight";
}

function deHighlightRow(sender)
{
    if (sender.oldClassName)
        sender.className = sender.oldClassName;
}
