/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) 
	{
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) 
{
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') 
	{
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) 
	{
		if (listElement.options[i].value == listValue)	
		{
			listElement.selectedIndex = i;
		}
	}	
}

/* Kontroll av användardata:*/
function setPaymentInfo(isChecked)
{
	with (window.document.frmCheckout) 
	{
		if (isChecked) {
			txtPaymentFirstName.value  = txtShippingFirstName.value;
			txtPaymentLastName.value   = txtShippingLastName.value;
			txtPaymentCompany.value    = txtShippingCompany.value;
			txtPaymentAddress1.value   = txtShippingAddress1.value;
			txtPaymentEmail.value	   = txtShippingEmail.value;
			txtPaymentPhone.value      = txtShippingPhone.value;			
			txtPaymentCity.value       = txtShippingCity.value;
			txtPaymentPostalCode.value = txtShippingPostalCode.value;
			
			txtPaymentFirstName.readOnly  = true;
			txtPaymentLastName.readOnly   = true;
			txtPaymentCompany.readOnly    = true;
			txtPaymentAddress1.readOnly   = true;
			txtPaymentEmail.readOnly   	  = true;
			txtPaymentPhone.readOnly      = true;			
			txtPaymentCity.readOnly       = true;
			txtPaymentPostalCode.readOnly = true;			
		} else {
			txtPaymentFirstName.readOnly  = false;
			txtPaymentLastName.readOnly   = false;
			txtPaymentAddress1.readOnly   = false;
			txtPaymentEmail.readOnly  	  = false;
			txtPaymentPhone.readOnly      = false;			
			txtPaymentCity.readOnly       = false;
			txtPaymentPostalCode.readOnly = false;			
		}
	}
}


function checkShippingAndPaymentInfo()
{
	with (window.document.frmCheckout) 
	{
		if (isEmpty(txtShippingFirstName, 'Uppgift saknas: Förnamn')) {
			return false;
		} else if (isEmpty(txtShippingLastName, 'Uppgift saknas: Efternamn')) {
			return false;
		} else if (isEmpty(txtShippingAddress1, 'Uppgift saknas: Adress')) {
			return false;
		} else if (isEmpty(txtShippingPhone, 'Uppgift saknas: Telefon')) {
			return false;
		} else if (isEmpty(txtShippingCity, 'Uppgift saknas: Stad')) {
			return false;
		} else if (isEmpty(txtShippingPostalCode, 'Uppgift saknas: Postnummer')) {
			return false;
		} else if (isEmpty(txtPaymentFirstName, 'Uppgift saknas: Förnamn')) {
			return false;
		} else if (isEmpty(txtPaymentLastName, 'Uppgift saknas: Efternamn')) {
			return false;
		} else if (isEmpty(txtPaymentAddress1, 'Uppgift saknas: Adress')) {
			return false;
		} else if (isEmpty(txtPaymentPhone, 'Uppgift saknas: Telefon')) {
			return false;
		} else if (isEmpty(txtPaymentCity, 'Uppgift saknas: Stad')) {
			return false;
		} else if (isEmpty(txtPaymentPostalCode, 'Uppgift saknas: Postnummer')) {
			return false;
		}else if (vilkor.checked == false){
			alert('Du måste läsa och godkänna köpvilkor!');
			return false;
		} else {
			return true;
		}
	}
}

function hideDiv(id)
{
	 //document.getElementById(id).style.display = "none";
	fadeoutdiv(id);
}
function showDiv(id, mess)
{
	document.getElementById(id).innerHTML = mess;
	//document.getElementById(id).style.display = "block";
	fadeindiv(id);
}
function timedHide(id)
{
  setTimeout("hideDiv(\"" + id + "\")", 2000); //5000= 5 seconds
}
function fadeindiv(id) 
{ 
	document.getElementById(id).style.display="none"
	fadeinto=setTimeout("divIn(" + 0 + ",'" + id + "')",100);
} 

function fadeoutdiv(id) 
{ 
	fadeoutto=setTimeout("divOut(" + 100 + ",'" + id + "')",100);
} 

function divIn(opacity, id) 
{ 
    opacity+=20;

    var os = document.getElementById(id).style; 
    
    os.opacity = (opacity / 101); 
    os.MozOpacity = (opacity / 101); 
    os.KhtmlOpacity = (opacity / 101); 
    os.filter = "alpha(opacity=" + opacity + ")";
    os.display="inline";
    
    clearTimeout(fadeinto);
    if(opacity<100)
    {
		fadeinto=setTimeout("divIn(" + opacity + ",'" + id + "')",200);
    }
}

function divOut(opacity, id) 
{ 
    opacity-=10;

    var os = document.getElementById(id).style; 
    os.opacity = (opacity / 101); 
    os.MozOpacity = (opacity / 101); 
    os.KhtmlOpacity = (opacity / 101); 
    os.filter = "alpha(opacity=" + opacity + ")";
    
    clearTimeout(fadeoutto);
    if(opacity>0)
    {
		fadeoutto=setTimeout("divOut(" + opacity + ",'" + id + "')",200);
    }
    else
    {
		os.display="none";
    }
} 
