/// menu

var timer = new Array();

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}




function switchoffopacity1()
{
	document.getElementById('cover_sc').style.display ="none";
}

function switchoffopacity2()
{
	document.getElementById('cover_tc').style.display ="none";
}



function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function gotoNewsletter(){
	var string;
	string = new String(document.getElementById("nemail").value);
	
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	{
		document.location = '/system/user/newsletter_save.asp?email=' + document.getElementById("nemail").value;
	}else{
		alert("Felaktig e-postadress.");
	}
}

function gotoNewsletter_temp(){
	var string;
	string = new String(document.getElementById("nemail").value);
	
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
	{
		document.location = 'register_temp_thanks2.asp?email=' + document.getElementById("nemail").value;
	}else{
		alert("Felaktig e-postadress.");
	}
}


function gotoShoppingcart(){
	document.location = '/system/quotation/create.asp';
}

function voxmenu(_trigger, _box)
{
	closeall(_box);

	var menu   = document.getElementById(_trigger);
	var brands = document.getElementById(_box);

	brands.style.top  = findPosY(menu)+30+"px";
	brands.style.left = findPosX(menu)+"px";
	brands.style.display='block';

	timer[_box]=0
}
function closeall(_box)
{
	if(_box!="brandsmenubox" && document.getElementById("brandsmenubox").style.display=='block')
	document.getElementById("brandsmenubox").style.display='none';
	if(_box!="catmenubox" && document.getElementById("catmenubox").style.display=='block')
	document.getElementById("catmenubox").style.display='none';
	if(_box!="specialmenubox" && document.getElementById("specialmenubox").style.display=='block')
	document.getElementById("specialmenubox").style.display='none';
}
function xclose(_box)
{
	var brands = document.getElementById(_box);

	if(timer[_box])
	{
		brands.style.display='none';
	}
}

function ImageObj (_a,_b,_c)
{
	Img=new Object();
	Img.imagepath = _a;
	Img.imagedesc = _b;
	Img.thumbnail = _c;
	return Img;
}


function printImages()
{
	this.TotalImages=imagearr.length;
	this.CurrentImage=0;
	this.ImagePosition=0;
	this.swap=Fswap;

	if(TotalImages>0)
	{
		if(TotalImages>1)
			{
			document.write("<a href='javascript:this.swap(0)'>");
			document.write("");
			document.write("<img border='0' id='imagecontainer' src='"+ imagearr[0].imagepath +"' />");
			document.write("</a>");
			}
		else
			{
			document.write("<img border='0' id='imagecontainer' src='"+ imagearr[0].imagepath +"' />");	
			}

		//if(TotalImages>1)
			//document.write("</a>");
		
//		document.write("<div id='imagedesc' style='width:360px;'>"+unescape(imagearr[0].imagedesc)+"</div>")

	}

/*	document.write("<div style='width:320px;height:10px;'></div>");
	for(i=0;i<TotalImages;i++)
	{
		document.write("<div style='float:left;padding-right:7px;' id='thumbnail'><a href='javascript:this.swap("+(i+1)+")'><img border='0' src='"+ imagearr[i].thumbnail +"'></a></div>");
	}
	document.write("<div style='clear:left'></div>");*/
}

function Fswap(_id)
{
	if(!_id)
	{
		if(this.ImagePosition >= this.TotalImages-1)
			this.ImagePosition = 0;
		else
			this.ImagePosition++;
	}
	else
	{
		this.ImagePosition= _id > 0 ? (_id-1):this.ImagePosition;
	}
	imagedescok = unescape(imagearr[this.ImagePosition].imagedesc).replace(/^\s*/,  '').replace(/\s*$/, '');
	document.getElementById("imagecontainer").src = imagearr[this.ImagePosition].imagepath;
//		document.getElementById("imagedesc").childNodes[0].nodeValue = imagedescok; 
}


function timerclose(_box)
{
	timer[_box]=1;
	setTimeout("xclose('"+_box+"')", 1500);
}

////

function showPic (whichpic) 
{
	if (document.getElementById) 
	{
		document.getElementById('imagecontainer').src = whichpic;
		return false;
	} 
	else 
	{
		return true;
	}
}

function text_search(_form)
{
	if(document.forms[_form].q.value != "")
		submit_form(_form);
	else
		alert("Type something to search for...");
}
function togglelayer(_name)
{
	if(document.getElementById(_name).style.display == 'block')
	{
		document.getElementById(_name).style.display = 'none'
		hide(_name);
	}
	else
	{
		document.getElementById(_name).style.display = 'block'
		show(_name);
	}

}

function open_xml_edit()
{
	matrix_window = window.open("matrix.asp", "matrix", "status=yes,scrolling=yes,width=400,height=400");
}

function validate_form(id,adm)
{
		var formObj = document.forms[id];
		var ok = 1;

		if (!adm)
		{
			// check pno if invoice
			krefrm = document.getElementById("persno");
			if(krefrm)
			{
				if(new personnummer(krefrm.value).valid != true)
				{
					alert("Please enter a valid \"Personnummer\" - YYMMDDXXXX");
					ok=0;
				}
				formObj.pno.value=krefrm.value;
			}

			if(formObj.address && !isValidString(formObj, "address", 2))
			{
				alert("Please enter your address");
				ok=0;

			}
			else if(formObj.zip && !isValidString(formObj, "zip", 4))
			{
				alert("Please check your zip code");
				ok=0;
			}
			else if(formObj.city && !isValidString(formObj, "city", 2))
			{
				alert("Please enter your city");
				ok=0;
			}
			else if(formObj.phone && !isValidString(formObj, "phone", 2))
			{
				alert("Please enter your phone number");
				ok=0;
			}
		}
		if(ok)
		{
			submit_form(id)			
		}
}

function personnummer(nr)
{
	this.valid=false;
	if(!nr.match(/^(\d{2})(\d{2})(\d{2})(\d{4})$/)){ return false; }
	this.now=new Date(); this.nowFullYear=this.now.getFullYear()+""; this.nowCentury=this.nowFullYear.substring(0,2); this.nowShortYear=this.nowFullYear.substring(2,4);
	this.year=RegExp.$1; this.month=RegExp.$2; this.day=RegExp.$3; this.controldigits=RegExp.$4;
	this.fullYear=(this.year*1<=this.nowShortYear*1)?(this.nowCentury+this.year)*1:((this.nowCentury*1-1)+this.year)*1;
	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(this.fullYear%400==0||this.fullYear%4==0&&this.fullYear%100!=0){ months[1]=29; }
	if(this.month*1<1||this.month*1>12||this.day*1<1||this.day*1>months[this.month*1-1]){ return false; }
	this.alldigits=this.year+this.month+this.day+this.controldigits;
	var nn="";
	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
	this.checksum=0;
	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
	this.valid=(this.checksum%10==0)?true:false;
	this.sex=parseInt(this.controldigits.substring(2,3))%2;
}

function confirm_del(msg, url)
{
	if(confirm(msg))
		location.href = url;
}

function chk_form(_form, msg)
{
	var msg = new String(msg)
	form = document.forms[_form];
	if(this.form.size)
	{
		if(this.form.size.value != 0)
		{
			submit_form(_form);
		}
		else
		{
			alert(msg);
		}
	}
}


function CheckAll(formObj)
{
	for (var i=0;i<formObj.elements.length;i++)
	{
		var e = formObj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
			e.checked = !e.checked
	}
}

function checkKey(e, func)
{
	if(e.keyCode == 13)
		eval(func);
}

function open_login(str, url)
{
	if(confirm(str))
		location.href = url;
}

function open_popup(url, handler, w, h)
{
	var wa = screen.Width;
	var ha = screen.Height;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	wHandler = window.open(url, "" ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,top=0,left=0');
	wHandler.moveTo(wa,ha);
	wHandler.focus();
}

function open_popup_noscroll(url, handler, w, h)
{
	var wa = screen.Width;
	var ha = screen.Height;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	wHandler = window.open(url, "" ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=no,resizable=no,status=no,top=0,left=0');
	wHandler.moveTo(wa,ha);
	wHandler.focus();
}

function submit_form(id)
{
	document.forms[id].submit();
}
function show(object) 
{

	if (document.getElementById) 
		document.getElementById(object).style.visibility = 'visible';
	else if (document.layers && document.layers[object] != null) 
		document.layers[object].visibility = 'visible';
	else if (document.all) 
			document.all[object].style.visibility = 'visible';
	return false;
}


function toggle(object) 
{

	if (document.getElementById) 
	{
		if (document.getElementById(object).style.visibility == 'visible')
			document.getElementById(object).style.visibility = 'hidden';
	    else
			document.getElementById(object).style.visibility = 'visible';
	}

	else if (document.layers && document.layers[object] != null) 
	{
		if (document.layers[object].visibility == 'visible' || document.layers[object].visibility == 'show' )
			document.layers[object].visibility = 'hidden';
		else
			document.layers[object].visibility = 'visible';
	}

	else if (document.all) 
	{
		if (document.all[object].style.visibility == 'visible')
			document.all[object].style.visibility = 'hidden';
		else
			document.all[object].style.visibility = 'visible';
	}
	return false;
}


function hide(object) 
{

	if (document.getElementById) 
		document.getElementById(object).style.visibility = 'hidden';
	else if (document.layers && document.layers[object] != null) 
		document.layers[object].visibility = 'hidden';
	else if (document.all) 
			document.all[object].style.visibility = 'hidden';
	return false;
}


// form handling
function setFocus(formObject, itemObject, isSelect)
{
	eval("formObject." + itemObject +".focus()");
	if(isSelect)
		eval("formObject."+ itemObject +".select()");
}
function isValidEmail(formObject, itemObject)
{
	var string;
	string = eval("formObject." + itemObject +".value");
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
		return true;
	else
		setFocus(formObject, itemObject, 1);
		return false;
}
function isValidEmail2(itemObject, error)
{
	string = itemObject.value;
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1)
		return true;
	else
		alert(error);
		itemObject.focus();
		return false;
}
function isValidSelect(formObject, itemObject)
{
	var val;
	val = eval("formObject." + itemObject + ".value");
	if(val == 0)
		{
			setFocus(formObject, itemObject, 0)
			return false;
		}
	return true;
}
function isValidString(formObject, itemObject, stringLength)
{
	var my_string;
	my_string = eval("formObject." + itemObject + ".value");
	if((my_string.length) > stringLength)
		return true;
	setFocus(formObject, itemObject, 1);
	return false;
}

function removeText(field) 
{
	  field.value = '';
}

function CompareFields(formObject, itemObjectA, itemObjectB)
{
		var A = eval("formObject." + itemObjectA + ".value");
		var B = eval("formObject." + itemObjectB + ".value");

		if(A == B)
			return true
		return false
}

function isChecked(formObject, itemObject)
{
	if( eval("formObject."+ itemObject +".checked") )
		return true
	return false
}

