﻿/* ---------------------------------------------------
 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;
}

function changePrice(id, value, size, saleList){
    
      var timer = 500;
      $('#' + id).animate({ opacity: 0}, timer );
      $('#' + id).queue(function() {
      	//debugger;
      	var salePrice = $("#" + saleList).find("option[value='" + size + "']").attr("text");
      	if (salePrice != '0') {
      		$(this).empty().html("<strike>Pris:" + value + " kr</strike><br/><span class='price_sale'>Reapris:" + salePrice + "kr</span>");
      		$(this).dequeue();
      	} else {
      		$(this).empty().text("Pris:" + value + " kr");
      		$(this).dequeue();
      	}
      });
      $('#' + id).animate({ opacity: 1}, timer );
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

function getCategory() {
  var re = new RegExp('.*/Kategori/(.*).aspx');
  var m = re.exec(window.location);
  var s = ""; 
  if (m != null) {
    s = m[1];
  } else {
	s = "Startsidan";
  }
  return s;
}

/* ---------------------------------------------------
GENERAL FUNCTIONS
------------------------------------------------------ */

var cnt = 1;

function add() {
	cnt++;
	set();
}

function sub() {
	if (cnt > 1) {
		cnt--;
		set();
	}
}

function checkValueIfZero() {
cnt = document.getElementById('quantity').value;	
	if (cnt == 0) {
		cnt = "1";
		set();
	}
}

function set() {
	document.getElementById('quantity').value = cnt;
}

function replacePicture(whatPicture) {
	$('.xoxoxo').attr("src", whatPicture);
	document.getElementById('enlarge').src = whatPicture.replace('Stor','Extrastor');
}

/* ---------------------------------------------------
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, 'scrollbars=yes,menubar=0,resizable=0,width='+w+',height='+h);
}

/* ---------------------------------------------------
AJAX 
------------------------------------------------------ */

//ADD ARTICLE TO CART
function addArticleToCart(articleId) {
	var quantity = document.getElementById('quantity').value;
    MasterPage.Ajax_AddArticleToCart(articleId, quantity, addArticleToCart_Callback);
}

function addArticleWithSizeToCart(articleId) {
	var quantity = $('#quantity').val();
	var size = $('#ctl00_Content_sizesDropDownList option:selected').text();
    MasterPage.Ajax_AddArticleWithSizeToCart(articleId, quantity, size, 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 cartLabel = document.getElementById('ctl00_basketText');
    var cartLink = document.getElementById('ctl00_cartLink');
    var orderArticles = cart.Order.Articles;
    var sum = 0;
    var count = 0;

    cartLabel.innerHTML = "";           
    
    var orderArticle;
    for(i = 0; i < orderArticles.length; i++) {
        orderArticle = orderArticles[i];
        count += orderArticle.Quantity;
        sum += orderArticle.Price * orderArticle.Quantity;
    }
   
    cartLabel.innerHTML = "" + count + " varor " + sum + " kr";
    cartLink.style.display = 'inline';
    location.href='/Affär/Varukorg/';
}

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);
}

var timeOut;
