/*******************************************************************************
 * Funktion* rpdemo.js Javascript som hanterar olika funktioner på
 * RPdemoprodukters webbsida.
 * 
 * 2009-05-19 developed & arranged by SenTangChalat for RPdemoprodukter
 * 
 * @author Magnus Mårdh <adress>magnus@thaiskolan.se</adress>
 * @version 1.0 Upphovsrätt © 2008-2010 RPdemoprodukter.
 ******************************************************************************/

var ie = (document.all) ? true : false; // IE4+
var dom = ((document.getElementById) && (!ie)) ? true : false; // Mozilla
var mapAct = 0;
var changed = 0;

function setEventByObject(ob, ev, fu) {
	if (dom)
		ob.addEventListener(ev, fu, false);
	if (ie)
		ob.attachEvent('on' + ev, fu);
}

function setEventById(id, ev, fu) {
	if (dom)
		document.getElementById(id).addEventListener(ev, fu, false);
	if (ie)
		document.getElementById(id).attachEvent('on' + ev, fu);
}

function delEventById(id, ev, fu) {
	if (dom) {
		document.getElementById(id).removeEventListener(ev, fu, false);
	}
	if (ie) {
		document.getElementById(id).detachEvent('on' + ev, fu);
	}
}

setEventByObject(window, "load", start);

var pictBank1 = new Array();
var pBankG = new Array();
var pBankW = new Array();
var pBankD = new Array();
var pBankM = new Array();

var pBankS = new Array();

function picPlay() {
	pictBank1[0] = new Array(2);
	// pictBank1[1] = new Array(6);

}
function picPlay2() {
	pBankG[0] = new Array(6);
	pBankG[1] = new Array(6);
	pBankG[2] = new Array(3);
}

function piggyBank() {
	pictBank1[0][0] = "images/60-810-S2.jpg";
	pictBank1[0][1] = "images/60-810-S1.jpg";
	/*
	 * pictBank1[1] [0] = "images/blomma1.jpg"; pictBank1[1] [1] =
	 * "images/blomma2.jpg"; pictBank1[1] [2] = "images/blomma3.jpg";
	 * pictBank1[1] [3] = "images/blomma4.jpg"; pictBank1[1] [4] =
	 * "images/blomma5.jpg"; pictBank1[1] [5] = "images/blomma6.jpg";
	 */
}

function prodBankG() {
	pBankG[0][0] = "images/3101L-0.jpg";
	pBankG[0][1] = "images/3101L-1.jpg";
	pBankG[0][2] = "images/3101L-2.jpg";
	pBankG[0][3] = "images/3101L-3.jpg";
	pBankG[0][4] = "images/3101L-4.jpg";
	pBankG[0][5] = "images/3101L-5.jpg";
	pBankG[1][0] = "images/3102L-0.jpg";
	pBankG[1][1] = "images/3102L-1.jpg";
	pBankG[1][2] = "images/3102L-2.jpg";
	pBankG[1][3] = "images/3102L-3.jpg";
	pBankG[1][4] = "images/3102L-4.jpg";
	pBankG[1][5] = "images/3102L-5.jpg";
	pBankG[2][0] = "images/60-200-S1.jpg";
	pBankG[2][1] = "images/60-200-S2.jpg";
	pBankG[2][2] = "images/60-200-S3.jpg";
}

function prodBankW() {
	pBankW[0][0] = "";
	pBankW[1][0] = "";
	pBankW[2][0] = "";
	pBankW[3][0] = "";
	pBankW[4][0] = "";
	pBankW[5][0] = "";
	pBankW[6][0] = "";
	pBankW[7][0] = "";
}

function prodBankD() {
	pBankD[0][0] = "";
	pBankW[0][1] = "";
	pBankW[1][0] = "";
	pBankW[1][1] = "";
	pBankW[1][2] = "";
	pBankW[1][3] = "";
	pBankW[1][4] = "";
	pBankW[1][5] = "";
}

function prodBankM() {
	pBankM[0][0] = "";
}

function prodBankS() {
	pBankS[0][1] = "";
	pBankS[0][2] = "";
}

function start() {
	picPlay();
	actP = document.getElementById('actPage').value;
	if (actP == '1') {
		startPicPlay();
	}
}

var c = 0;
var d = 0;
var t;
var g = 0;
function startPicPlay() {
	if (g == 0 && c == 0) {
		// picPlay();
		piggyBank();
		bId = 'blenddiv';
		iId = 'blendimage';
		// scrollTo(300, 10);
	} else if (g == 0 && c > 1) {
		c = 0;
		g = 0;
		d++;
		bId = 'blenddiv';
		iId = 'blendimage';
	} else if (g == 0 && d == 2)
	/*
	 * { g = 1; d = 3; c = 0; bId = 'blenddiv2'; iId = 'blendimage2';
	 * //scrollTo(300, 500); } else if( d == 2 )//1 && d == 3 && c >= 6
	 */
	{
		clearTimeout(t);
		return true;
	}

	blendimage(bId, iId, pictBank1[g][c], 800);
	c++;
	t = setTimeout("startPicPlay()", 2500);
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	// set the current image as background
	if (divid != 0)
		document.getElementById(divid).style.backgroundImage = "url("
				+ document.getElementById(imageid).src + ")";

	// make image transparent
	changeOpac(0, imageid);

	// make new image
	document.getElementById(imageid).src = imagefile;

	// fade in image
	for (i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')", (timer * speed));
		timer++;
	}
}

// change the opacity for different browsers
function changeOpac(opacity, pId) {
	var object = document.getElementById(pId).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function openBox() {
	document.getElementById('currPicCell').style.backgroundImage = "url('')";
	box = document.getElementById('bBox');
	if (ie)
		box.className = "bildBox";
	else {
		box.setAttribute('class', "bildBox");
	}

}

function closeBox() {
	if (!e)
		var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation)
		e.stopPropagation();
	document.getElementById('currPicCell').style.backgroundImage = "url('')";
	box = document.getElementById('bBox');
	if (ie)
		box.className = "hidden";
	else
		box.setAttribute('class', "hidden");

}

function imgPlay(obj) {
	actP = document.getElementById('actPage').value;
	picPlay2();
	if (actP == '2') {
		prodBankG();
		openBox();// Här talar vi om hur stor boxen ska vara
		if (obj == '1') {
			startShow('0');// '3101L'
		} else if (obj == '2') {
			startShow('1');// '3102L'
		} else if (obj == '3') {
			startShow('2');// '60-200'
		}
	}
}

var c = 0;
var t;
function startShow(pic) {
	boxB = document.getElementById('bBox');
	if (pic == '0' || pic == '1')
		boxB.style.width = "500px";
	if (pic == '0')
		boxB.style.height = "525px";
	if (pic == '1')
		boxB.style.height = "518px";
	if (pic == '2') {
		boxB.style.width = "367px";
		boxB.style.height = "538px";
		boxB.style.top = "50px";
	}
	if (((pic == '0' || pic == '1') && c == 6) || (pic == '2' && c == 3)) {
		c = 0;
		clearTimeout(t);
		return true;
	}
	bId = 'currPicCell';
	iId = 'currPic';
	document.getElementById(iId).alt = c;
	blendimage(bId, iId, pBankG[pic][c], 800);
	c++;
	t = setTimeout("startShow(" + pic + ")", 2000);
}

/*
 * Change CSS-selector to show the picture in img-tag with id = nam, showPic
 */
function showPix(nam, id) {
	if (mapAct != 0) {
		alert("st\u00E4ng f\u00F6nstret, det drar!");
	} else {
		var Obj = document.getElementById(id);
		var Obj2 = document.getElementById(nam);
		if (ie)
			Obj2.className = "picDivExt";
		if (dom) {
			Obj2.removeAttribute("class");
			Obj2.setAttribute("class", "picDivExt");

		}
		high(id);
		setEventById(nam, "click", new Function("stopDuo('" + nam + "','" + id
				+ "');"));
		mapAct = 1;
	}
}

// Change CSS-selector back to default
function stopDuo(nam, id) {
	if (mapAct != 0) {
		var Obj = document.getElementById(id);
		var Obj2 = document.getElementById(nam);
		low(id);
		if (ie)
			Obj2.className = "picDiv";
		if (dom) {
			Obj2.removeAttribute("class");
			Obj2.setAttribute("class", "picDiv");

		}
		clearInterval(highlighting);
		mapAct = 0;
		delEventById(nam, "click", stopDuo);
	} else
		return 0;
}

function high(id) {
	var Obj = document.getElementById(id);
	theobject = Obj;
	highlighting = setInterval("highlightit(theobject)", 2);
}

function low(id) {
	var Obj = document.getElementById(id);
	clearInterval(highlighting);
	if (dom) {
		Obj.style.MozOpacity.opacity = 10 / 100;
	}
	if (ie) {
		Obj.filters.alpha.opacity = 10;
	}
}

function highlightit(Obj) {
	if (dom)
		if (Obj.style.MozOpacity.opacity < 100)
			Obj.style.MozOpacity.opacity = 100;
		else if (window.highlighting)
			clearInterval(highlighting);
	if (ie) {
		if (Obj.filters.alpha.opacity < 100)
			Obj.filters.alpha.opacity += 5;
		else if (window.highlighting)
			clearInterval(highlighting);
	}
}

function validateForm(El) {
	bia = document.forms[El];
	// alert('hej vi kom fram!'+bia.elements['name'].value);
	// alert('hej '+El.name.value);
	if (bia.elements['name'].value == "")// || changed==0)
	{
		alert("Hoppsan.. Du gl\u00F6mde att skriva namn.");
		bia.elements['name'].focus();
		bia.elements['name'].value = "";
		changed = 1;
		return false;
	}
	if (bia.elements['gatuadress'].value == "")// || changed==0)
	{
		alert("Hoppsan.. Du gl\u00F6mde att skriva adress.");
		bia.elements['gatuadress'].focus();
		bia.elements['gatuadress'].value = "";
		changed = 1;
		return false;
	}
	if (bia.elements['postnr'].value == "")// || changed==0)
	{
		alert("Hoppsan.. Du gl\u00F6mde att skriva postnummer.");
		bia.elements['postnr'].focus();
		bia.elements['postnr'].value = "";
		changed = 1;
		return false;
	}
	if (bia.elements['stad'].value == "")// || changed==0)
	{
		alert("Hoppsan.. Du gl\u00F6mde att skriva ort.");
		bia.elements['stad'].focus();
		bia.elements['stad'].value = "";
		changed = 1;
		return false;
	}
	if (bia.elements['email'].value == "") {
		alert("Hoppsan.. Du gl\u00F6mde att skriva en mailadress.");
		bia.elements['email'].focus();
		return false;
	}

	if (!checkEmail(bia.elements['email'].value)) {
		return false;
	}
	return true;
}

function whois(ipt) {
	var nyhetsadress = "whois.php?ip=" + ipt;
	open(nyhetsadress, 'popup', 'width=700,height=600,scrollbars=1');
}

function checkEmail(emailStr) {
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;

	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	var validChars = "\[^\\s" + specialChars + "\]";

	var quotedUser = "(\"[^\"]*\")";

	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	var atom = validChars + '+';

	var word = "(" + atom + "|" + quotedUser + ")";

	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");

	var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");

	var matchArray = emailStr.match(emailPat);

	if (matchArray == null) {

		alert("Emailadressen \u00E4r fel. (Kontrollera '@' och '.')");
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			alert("F\u00F6rledet i emailadressen inneh\u00E5ller ogilitiga tecken.");
			return false;
		}
	}
	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			alert("Dom\u00E4nen inneh\u00E5ller ogiltiga tecken.");
			return false;
		}
	}

	if (user.match(userPat) == null) {
		alert("Anv\u00E4ndaren ser ut att vara felaktig.");
		return false;
	}

	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {

		for ( var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				alert("IP adress \u00E4r felaktig!");
				return false;
			}
		}
		return true;
	}

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			alert("Fel dom\u00E4n.");
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length - 1].length != 2
			&& domArr[domArr.length - 1].search(knownDomsPat) == -1) {
		alert("Emailadressen m\u00E5ste komma fr\u00E5n en k\u00E4nd topdom\u00E4n, eller landsdom\u00E4n");
		return false;
	}

	if (len < 2) {
		alert("Hoppsan.. fel i emailadressen!");
		return false;
	}
	return true;
}

function corrChar(ord) {
	var ordet = "";
	for (x = 0; x < ord.length; x++) {
		teck = ord.charAt(x);

		if (teck == "&") {
			teck1 = ord.charAt(x + 1);
			if (teck1 == "o") {
				teck = "\u00F6";
				x = x + 5;
			} else if (teck1 == "a") {
				teck2 = ord.charAt(x + 2);
				if (teck2 == "u") {
					teck = "\u00E4";
					x = x + 5;
				} else {
					teck = "\u00E5";
					x = x + 6;
				}
			}
		}
		if (teck == "å")
			teck = "\u00E5";
		else if (teck == "ä")
			teck = "\u00E4";
		else if (teck == "ö")
			teck = "\u00F6";
		ordet += teck;
	}
	return ordet;
}

