var que = "?";
var and = "&";
var bracket = "#";
var divider = "@";
var equals = "=";
var inputVal = "value";
var boxChecked = "checked";

function showMenu(id) { 
  if (id.style.visibility == 'hidden') {
    id.style.visibility = 'visible';  
    	}
  else {
	id.style.visibility = 'hidden';
  	}  
      }

function hideMenu(id){
	id.style.visibility = 'hidden';
	}

function openWindow(url, name) {
	window.open(url, name, popupParams(name));
}

function closeWindow() {
	self.close();
}

function popupParams(page) {
	var params;
	
	switch ( page.toLowerCase() )
	{
		case "help" :
			params = 'toolbar=0,location=0,top=150,left=450,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=650,height=400';	
			break;
		case "invoice" :
			params = 'toolbar=1,location=0,top=0,left=350,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=750,height=900';	
			break;
		case "disppicture" :
			params = 'toolbar=0,location=0,top=0,left=250,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=750,height=900';	
			break;
		case "disponepicture" :
			params = 'toolbar=0,location=0,top=0,left=250,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=750,height=500';	
			break;
		case "pdf" :
			params = 'toolbar=1,location=0,top=0,left=350,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=900';	
			break;
		case "blankpage" :
			params = '';
			break;		
		case "popup" :
			params = 'toolbar=0,location=0,top=100,left=350,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,width=500,height=600';
			break;	
		default :
			params = 'toolbar=0,location=0,top=300,left=450,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,width=400,height=400';
			break;	
	}
	
	return params;
}

function chkForm(){

	name = document.form.formsfld2_2.value;
	if(name == ""){
		window.alert("Ange namn!")
		form.formsfld2_2.focus();
		return false;
	}

}

function chkTipFriend(){

	MyName = document.tipFriend.MyName.value;
	if(MyName == ""){
	window.alert("Ange namn!")
	tipFriend.MyName.focus();
	return false;
	}
	RecvEmail = document.tipFriend.RecvEmail.value;
	if (RecvEmail == ""){
	window.alert ("E-post skall anges!");
	tipFriend.RecvEmail.focus();
	tipFriend.RecvEmail.select();
	return false;
	}
	paaPos = RecvEmail.indexOf('@',0);
	if (paaPos == -1){
	window.alert ("E-post skall anges!");
	tipFriend.RecvEmail.focus();
	tipFriend.RecvEmail.select();
	return false;
	}
	punktumPos = RecvEmail.indexOf ('.',paaPos);
	if (punktumPos == -1){
	window.alert ("E-post skall anges!");
	tipFriend.RecvEmail.focus();
	tipFriend.RecvEmail.select();
	return false;
	}
	if (punktumPos+3 > RecvEmail.length) {
	window.alert ("E-post skall anges!");
	tipFriend.RecvEmail.focus();
	tipFriend.RecvEmail.select();
	return false;
	}

}

function chkAcceptCond(f){

	if (f.acceptConditions.checked == false) {
	   window.alert("Ni måste godkänna köpvillkoren!")
	   form1.acceptConditions.focus();
	   return false;
	}
   else return true;

}

//a fix for JavaScript's crappy math handling
function fixIt(num) {
	return(Math.round(num*10000000)/10000000);
}

function updBalDeb(){

	deb = parseFloat(document.acc.debet_01.value);
	deb += parseFloat(document.acc.debet_02.value);
	deb += parseFloat(document.acc.debet_03.value);
	document.acc.acc_balDeb.value = fixIt(eval(deb));

	var cred = parseFloat(document.acc.credit_01.value);
	cred += parseFloat(document.acc.credit_02.value);
	cred += parseFloat(document.acc.credit_03.value);

	var diff = deb - cred;
	diff.toFixed(2);
	diff = fixIt(eval(diff));

	document.acc.acc_diff.value = diff;

}

function updBalCred(){

	var deb = parseFloat(document.acc.debet_01.value);
	deb += parseFloat(document.acc.debet_02.value);
	deb += parseFloat(document.acc.debet_03.value);

	var cred = parseFloat(document.acc.credit_01.value);
	cred += parseFloat(document.acc.credit_02.value);
	cred += parseFloat(document.acc.credit_03.value);
	document.acc.acc_balCred.value = fixIt(eval(cred));

	var diff = deb - cred;
	diff.toFixed(2);

	document.acc.acc_diff.value = fixIt(eval(diff));

}

function chkAccDiff(){

	var diff = parseFloat(document.acc.acc_diff.value);
	if(diff != 0){
		window.alert("Differensen måste vara noll!")
	   acc.debet_01.focus();
		return false;
	}

	if (window.confirm("Skapa verifikation?")) {
		return true;
	}
	
	return false;

}

function chkProdChar(chkWeightZero,chkVolumeZero) {

	invalidChars = "'></,;";
	invalidChars2 = '"';
	var prod = document.addProd.Product.value;
	var shortName = document.addProd.ShortName.value;
	var weight = document.addProd.Weight.value;
	var volume = document.addProd.Volume.value;
	
	if (prod == "") {
		window.alert("Produkt måste fylls i!")
		addProd.Product.focus();
		addProd.Product.select();
		return false;
	}
	
	if (prod.length > 50) {
		window.alert("Max 50 tecken i produktnamnet!")
		addProd.Product.focus();
		addProd.Product.select();
		return false;
	}
	
	//if (shortName == "") {
		//window.alert("Kortnamn måste fylls i!")
		//addProd.ShortName.focus();
		//addProd.ShortName.select();
		//return false;
	//}

	if (shortName.length > 50) {
		window.alert("Max 50 tecken i kortnamnet!")
		addProd.ShortName.focus();
		addProd.ShortName.select();
		return false;
	}
	
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (prod.indexOf(badChar,0) > -1) {
			window.alert("Ej tillåtna tecken ('></,;) i produktnamnet!")
			addProd.Product.focus();
			addProd.Product.select();
			return false;
		}
	}

	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (shortName.indexOf(badChar,0) > -1) {
			window.alert("Ej tillåtna tecken ('></,;) i kortnamn!")
			addProd.ShortName.focus();
			addProd.ShortName.select();
			return false;
		}
	}

	for (i=0; i<invalidChars2.length; i++) {
		badChar = invalidChars2.charAt(i);
		if (prod.indexOf(badChar,0) > -1) {
			window.alert('Ej tillåtna tecken (") i produktnamnet!')
			addProd.Product.focus();
			addProd.Product.select();
			return false;
		}
	}

	if (chkWeightZero == "Y") {
		if (weight == 0 || weight == "") {
			window.alert("Vikt får ej vara noll!")
			addProd.Weight.focus();
			addProd.Weight.select();
			return false;
		}
	}

	if (chkVolumeZero == "Y") {
		if (volume == 0 || volume == "") {
			window.alert("Volym får ej vara noll!")
			addProd.Volume.focus();
			addProd.Volume.select();
			return false;
		}
	}

}

function swtVatModeIV(applUrl){

	window.location=applUrl;

}

function swtVatModeEV(applUrl){

	window.location=applUrl;

}

function userConfDltInv(){

	if (window.confirm("Ta bort faktura?")) {
		return true;
	}

	return false;

}

function userConfDltOrder(){

	if (window.confirm("Ta bort order (samt alla orderrader)?")) {
		return true;
	}

	return false;

}

function userConf(text){

	if (window.confirm(text)) {
		return true;
	}
	return false;

}

function userConfArchiveOrder(){

	if (window.confirm("Arkivera order (samt alla orderrader)?")) {
		return true;
	}

	return false;

}

function userConfCpyDBTbl(){

	if (window.confirm("Kopiera databastabeller?")) {
		return true;
	}

	return false;

}

function userConfCpyInst(){

	if (window.confirm("Kopiera butik?")) {
		return true;
	}

	return false;

}

function userConfDltDBTbl(){

	if (window.confirm("Ta bort databastabeller?")) {
		return true;
	}

	return false;

}

function userConfCpyExtDB(){

	if (window.confirm("Kopiera extern databas?")) {
		return true;
	}

	return false;

}

function userConfRunFixPgm(){

	if (window.confirm("Köra fix program?")) {
		return true;
	}

	return false;

}

function userConfEmptyDBTbl(){

	if (window.confirm("Töm databastabeller (alla produkter, grupper, familjer, order, händelser mm)?")) {
		return true;
	}

	return false;

}

function stripHTML(){

	var re = /(<([^>]+)>)/gi;
	//if (window.confirm("Remove HTML tags?")) {
		for (i=0; i<arguments.length; i++)
		arguments[i].value=arguments[i].value.replace(re, "")
		//return true;
	//}

	//return false;
}

function screenWidth() {

width = screen.availWidth;

return width;

}

function IsRightButtonClicked(e)
{ 
var rightclick = false;
e = e || window.event;
if (e.which)
rightclick = (e.which == 3);
else if (e.button)
rightclick = (e.button == 2);
return rightclick;
}

// Browser Slide-Show script. With image cross fade effect for those browsers
// that support it.
// Script copyright (C) 2004-2008 www.cryer.co.uk.
// Script is free to use provided this copyright header is included.
var FadeDurationMS=1000;
function SetOpacity(object,opacityPct)
{
  // IE.
  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  // Old mozilla and firefox
  object.style.MozOpacity = opacityPct/100;
  // Everything else.
  object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
  var element=document.getElementById(id);
  var msNow = (new Date()).getTime();
  var opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity>=100)
  {
    SetOpacity(element,100);
    element.timer = undefined;
  }
  else if (opacity<=0)
  {
    SetOpacity(element,0);
    element.timer = undefined;
  }
  else 
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",10);
  }
}
function FadeInImage(foregroundID,newImage,backgroundID)
{
  var foreground=document.getElementById(foregroundID);
  if (foreground.timer) window.clearTimeout(foreground.timer);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {
      if (background.src)
      {
        foreground.src = background.src; 
        SetOpacity(foreground,100);
      }
      background.src = newImage;
      background.style.backgroundImage = 'url(' + newImage + ')';
      background.style.backgroundRepeat = 'no-repeat';
      var startMS = (new Date()).getTime();
      foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "'," + FadeDurationMS + "," + startMS + ",100,0)",10);
    }
  } else {
    foreground.src = newImage;
  }
}
var slideCache = new Array();
function RunSlideShow(pictureID,backgroundID,imageFiles,displaySecs)
{
  var imageSeparator = imageFiles.indexOf(";");
  var nextImage = imageFiles.substring(0,imageSeparator);
  FadeInImage(pictureID,nextImage,backgroundID);
  var futureImages = imageFiles.substring(imageSeparator+1,imageFiles.length)+ ';' + nextImage;
  setTimeout("RunSlideShow('"+pictureID+"','"+backgroundID+"','"+futureImages+"',"+displaySecs+")",displaySecs*1000);
  // Cache the next image to improve performance.
  imageSeparator = futureImages.indexOf(";");
  nextImage = futureImages.substring(0,imageSeparator);
  if (slideCache[nextImage] == null)
  {
    slideCache[nextImage] = new Image;
    slideCache[nextImage].src = nextImage;
  }
}

function setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function runOnLoad() {

width = screen.availWidth;
setCookie('myShop_data','SW=' + width,1)
}
