﻿/* ---------------------------------------------------
 URL FRIENDLY 
------------------------------------------------------ */

function urlFriendly(str){

  	var url = str
  		.toLowerCase() // change everything to lowercase
 		.replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces		
  		.replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen
 		.replace(/[^a-z0-9-åäö]+/g, "") // remove all non-alphanumeric characters except the hyphen
 		.replace(/[-]+/g, "-") // replace multiple instances of the hyphen with a single instance
		.replace(/^-+|-+$/g, "") // trim leading and trailing hyphens				
 		; 
 	
    return url;
}

/* ---------------------------------------------------
VALIDATORS
------------------------------------------------------ */
function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if (! email.match(re)) {
        return (false);
    }
    return(true);
}

function startInput(input)
{
    if(input.value == '0')
        input.value = '';
}

function emailValidator(source, arguments)
{
    arguments.IsValid = validateEmail(arguments.Value);
}

function DropDownValidate(source, arguments)
{
    if(arguments.Value != '-1')
        arguments.IsValid = true;
    else
        arguments.IsValid = false;
}


/* ---------------------------------------------------
WINDOW 
------------------------------------------------------ */

function windowOpen(url, name, w, h)
{
    var popUp = window.open(url, name, 'menubar=0,resizable=0,width='+w+',height='+h);
}

/* ---------------------------------------------------
AJAX 
------------------------------------------------------ */

//ADD ARTICLE TO CART
function addArticleToCart(articleId)
{
    //alert(articleId);
    MasterPage.Ajax_AddArticleToCart(articleId, addArticleToCart_Callback);
}

//ADD SUBSCRIPTION TO CART
function addSubscriptionToCart(articleId, priceType, price, subscriptionText)
{
    MasterPage.Ajax_AddSubscriptionToCart(articleId, priceType, price, subscriptionText, addArticleToCart_Callback);
}

//ADD ARTICLE & SUBSCRIPTION CALLBACK
function addArticleToCart_Callback(res){

    if(res.error != null)
    {
        alert('Fel: ' + res.value + ': ' + res.error.Message);
        return;
    }
    if(res.value == null)
    {
        alert('Något gick fel');
        return;
    }
    updateCartDisplay(res.value);
}


//UPDATE CART
function updateCartDisplay(cart)
{
    var cartList = document.getElementById('ctl00_DropDownBasket');
    var orderArticles = cart.Order.Articles;
    
    //alert(cart.Order.Articles.ToString());
    
    //Erase all options in cartList
    for(i = cartList.childNodes.length-1; i >= 0; i--)
    {
        cartList.removeChild(cartList.childNodes[i]);
    }            
    
    var orderArticle, option, optionText, totalSum = 0;
    for(i = 0; i < orderArticles.length; i++)
    {
        orderArticle = orderArticles[i];
        option = document.createElement('option');
        optionText = document.createTextNode(orderArticle.Title + ', ' + orderArticle.Quantity + 'st (á ' + orderArticle.Price + ' kr)');
        option.appendChild(optionText);
        cartList.appendChild(option);
        totalSum += orderArticle.Price * orderArticle.Quantity;
    }
    
    //Summary
    option = document.createElement('option');
    optionText = document.createTextNode('Totalt: ' + parseFloat(totalSum) + ' kr');
    option.appendChild(optionText);
    cartList.appendChild(option);
    cartList.selectedIndex = i - 1;
}

function GetThis(T, C, U, L)
{
var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(T)
+ '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
window.open(targetUrl);
}

function ShowLargeImage(){
    document.getElementById('divLargeImage').style.display = 'block';
}

function CloseLargeImage(){
    document.getElementById('divLargeImage').style.display = 'none';
}

/*


var xmlhttp = null;

function AddToCart(id)
{
	var curUrl = location.href;
	var newUrl = "";
	if(curUrl.indexOf("?") >= 0)
	{
		if(curUrl.indexOf("add") >= 0)
		{
			var vTmp = document.URL;
			vTmp = vTmp.substr(vTmp.indexOf("?"));
			vTmp = vTmp.replace(/\barticleid=[0-9]+&add=[0-9]/gi,"");
			newUrl = vTmp + "articleId=" + id + "&add=1";;
		}
		else
		{
			newUrl = curUrl + "&articleId=" + id + "&add=1";
		}
	}
	else
		newUrl = curUrl + "?articleId=" + id + "&add=1";
	location.href = newUrl;
}

function cartStateChange()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
		// if "OK"
		if (xmlhttp.status==200)
		{
			if(xmlhttp.responseText == undefined) alert("undef");
			var xmlDoc = xmlhttp.responseXML;
			var resp = xmlDoc.getElementById('_cmsContent');
			alert(resp.XML);
		}
		else
		{
			alert("Problem retrieving XML data");
		}
	}
}

 */

//?
var timeOut;
