// JavaScript Document

// SKAPA EN CENTRERAD POPUP
function popUp(url, dw, dh, sc) {
    scW = screen.availWidth ? screen.availWidth : screen.width;
	scH = screen.availHeight ? screen.availHeight : screen.height;
	pl = parseInt((scW/2)-(dw/2));
	pt = parseInt((scH/2)-(dh/2));
	window.open(url,"","scrollbars="+sc+",width="+dw+",height="+dh+",left="+pl+",top="+pt+"");
}

// BEKRÄFTA INNAN NÄSTA SIDA
function confirmGoto(thisstate, url) {
    if(confirm(thisstate))
        location.href = url;
}

// DÖLJA OCH VISA LAGER
function layerAct(id, iState) { // 1 visible, 0 hidden
	if(document.layers)	
		document.layers[id].visibility = iState ? "show" : "hide";   
	else if(document.getElementById) {
		var obj = document.getElementById(id);
		obj.style.visibility = iState ? "visible" : "hidden";
	}
	else if(document.all)
		document.all[id].style.visibility = iState ? "visible" : "hidden";
	else
		alert("Your browser doesn't seem to support layers!");
}

// KONTROLLERA MAILADRESSER
function myMail(field) {
	var strEmail = field.value;
	while(strEmail.charAt(0) == ' ')
		strEmail = strEmail.substring(1);
	while(strEmail.charAt(strEmail.length-1) == ' ')
		strEmail = strEmail.substring(0, strEmail.length-1);
	 
	var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_@.";
	var ok = "yes";
	var temp;
	for(i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if(valid.indexOf(temp) == "-1") ok = "no";
	}
	if(ok == "no") {
		alert("Only '" + valid + "'!");
		field.select(); field.focus();
	}
  
	// do some basic checking that the reg ex returns as true
	if(strEmail.indexOf("..") != -1) { alert(".. = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf(".@") != -1) { alert(".@ = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("@.") != -1) { alert("@. = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("--") != -1) { alert("-- = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("-.") != -1) { alert("-. = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf(".-") != -1) { alert(".- = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("__") != -1) { alert("__ = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("_.") != -1) { alert("_. = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("._") != -1) { alert("._ = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("-_") != -1) { alert("-_ = Wrong!"); field.select(); field.focus(); }
	if(strEmail.indexOf("_-") != -1) { alert("_- = Wrong!"); field.select(); field.focus(); }
  
	// check to see if regular expressions is supported by browser
	if(window.RegExp){
		// supported, use regular expression to validate email
		var objRegExp = new RegExp("^\w+[\.\-\w+]+@\w+[\.\-\w+]+(\.\w+)+$");
		return objRegExp.test(strEmail);
	}
	else {
		// not supported, use classic string parsing to find a valid pattern
		return((strEmail.indexOf("@") == -1) && (strEmail.indexOf(".") == -1) && (strEmail.indexOf(" ") != -1)); 
	}
}

// KONTROLLERA VALFRITT FÄLT
function myField(field, valid, len) {
//	var valid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_:/.~";
	var ok = "yes";
	var temp;
	for(i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if(valid.indexOf(temp) == "-1") ok = "no";
	}
	if(ok == "no") {
		alert("Only '" +valid+ "'!");
		field.focus();
		field.select();
	}
	if(field.value.length < len && field.value.length != 0) {
		alert("Atleast " + len + " chars!");
		field.focus();
		field.select();
	}
}

// BYT BILD
function swapImage(_target,_newsrc) {
    var newImage;
    if(document.getElementById) {
    	newImage = document.getElementById(_target);
    } else if(document.layers) {
	    for(i=0; i<document.layers.length; i++) {
    		if(document.layers[i].document.images[_target]) {
	   			newImage = document.layers[i].document.images[_target]
	    		break;
		    }
	    }
	} else if(document.all) {
	    newImage = document.all[_target];
	}
	if(newImage) { newImage.src = _newsrc; }
}

// FÖRLADDA BILDER
function preloadImages() {
    var aImages = new Array();
    for(var x=0; x < preloadImages.arguments.length; x++) {
        aImages[x] = new Image();
        aImages[x].src = preloadImages.arguments[x];
    }
}

preloadImages('images/background/bg_blue.jpg',
              'images/background/bg_blue_art.jpg',
              'images/background/bg_blue_biography.jpg',
              'images/background/bg_blue_furniture.jpg',
              'images/background/bg_blue_index.jpg',
              'images/background/bg_blue_jewelry.jpg',
              'images/background/bg_blue_knifes.jpg',
              'images/background/bg_blue_music.jpg',
              'images/background/bg_blue_sculpture.jpg'
              );

/*
See http://www.howtocreate.co.uk/perfectPopups.html and
http://www.howtocreate.co.uk/jslibs/termsOfUse.html
for details and terms of use.
To call this script, use something like (the number is a delay before
it closes or 0 for no timed closing -
the true/false says if the window should close when they switch to
another window):
you can style this, but don't try to text-align it to the right, it
will break the resizing effect
keep it narrow, if it is wider than the image, the window will wrap
to this width
the makeright class tells the script to automatically align it to the right

<a href="me.jpg" onclick="return popImageExtra(this.href,'Site
author',true,3000,extraHTML);">link</a>
*/


//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 150;
defaultHeight = 150;

//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>\n<head>\n<title>'+imageTitle+'<\/title>\n<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>\n'+
		'<\/head>\n<body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>\n'+
		(document.layers?('<layer left="0" top="0">\n'):('<div style="position:absolute;left:0px;top:0px;display:table;">\n'))+
		'<img src="'+imageURL+'" alt="'+imageTitle+'" title=="'+imageTitle+'" onload="resizeWinTo();" border="0" />'+
		(document.layers?'<\/layer>\n':'<\/div>\n')+'<\/body>\n<\/html>');
	imgWin.document.close();
	if( imgWin.focus ) { imgWin.focus(); }
	//return false; <br /><a href="javascript:self.close()" style="color:#003366;font-family:sans-serif;font-size:11px;">St&auml;ng f&ouml;nstret</a>
}
