﻿var EmailRegExpPattern = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$");
var ZipRegExpPattern = new RegExp("^\\d{5}$");

var sellingPlacesFrameWidth = 650;
var sellingPlacesFrameHeight = 400;

function chooseCategory(select, productsUrl, categoryBaseUrl)
{
	var index = select.selectedIndex;
	if (index > 0)
	{
		var value = select.options[index].value;
		location.href = value;
	}
}

function productDetail_DisplaySellingPlaces(pageUrl)
{
	var frameContainer = document.getElementById("sellingPlacesContainer");
	var windowSize = getWindowProportions();
	
	if (frameContainer == null)
	{
		var frameInnerContainer = null, frameHeader = null, frameCloseAnchor, frame = null;
		
		// container
		frameContainer = document.createElement("div");
		frameContainer.id = "sellingPlacesContainer";
		frameContainer.className = "selling-places-container";
		frameContainer.style.width = String(windowSize.width) + "px";
		frameContainer.style.height = String(windowSize.height) + "px";
		frameContainer.onclick = productDetail_SellingPlaceClicked;
		window.onscroll = productDetail_SellingPlaceWindowScrolled;
		window.onresize = productDetail_SellingPlaceWindowResized;

		// inner container
		frameInnerContainer = document.createElement("div");
		frameInnerContainer.className = "inner-container";
		
		frameContainer.appendChild(frameInnerContainer);

		// frame header
		frameHeader = document.createElement("div");
		frameHeader.className = "header";
		frameInnerContainer.appendChild(frameHeader);
		
		// close anchor
		frameCloseAnchor = document.createElement("a");
		frameCloseAnchor.className = "close";
		frameCloseAnchor.setAttribute("title", "Zavřít");
		frameCloseAnchor.setAttribute("href", "javascript:void(0);");
		frameCloseAnchor.onclick = function() { productDetail_CloseSellingPlaces(); return false; };
		frameCloseAnchor.innerHTML = "&nbsp;";
		frameHeader.appendChild(frameCloseAnchor);

		// frame
		frame = document.createElement("iframe");
		frame.setAttribute("src", pageUrl);
		frame.setAttribute("frameBorder", "0");
		//frame.setAttribute("scrolling", "no");
		frameInnerContainer.appendChild(frame);
		
		document.body.appendChild(frameContainer);
	}
	else
	{
		frameContainer.style.display = "block";
	}
	
	frameContainer.style.paddingTop = String(productDetail_ComputeSellingPlaceFramePaddingTop()) + "px";
	setElementOpacity("container", 0.5);
}

function productDetail_SellingPlaceWindowScrolled()
{
	var frameContainer = document.getElementById("sellingPlacesContainer");
	if (frameContainer != null)
	{
		frameContainer.style.paddingTop = String(productDetail_ComputeSellingPlaceFramePaddingTop()) + "px";
	}
}

function productDetail_SellingPlaceWindowResized()
{
	var frameContainer = document.getElementById("sellingPlacesContainer");
	if (frameContainer != null)
	{
		var windowSize = getWindowProportions();
		
		frameContainer.style.width = String(windowSize.width) + "px";
		frameContainer.style.height = String(windowSize.height) + "px";
		frameContainer.style.paddingTop = String(productDetail_ComputeSellingPlaceFramePaddingTop()) + "px";
	}
}

function productDetail_ComputeSellingPlaceFramePaddingTop()
{
	var windowSize = getWindowProportions();
	var paddingTop = ((windowSize.height - sellingPlacesFrameHeight) / 2) + getWindowScrollTop();
	if (paddingTop < 170) paddingTop = 170;
	
	return paddingTop;
}

function productDetail_SellingPlaceClicked()
{
	productDetail_CloseSellingPlaces();
}

function productDetail_CloseSellingPlaces()
{
	var frameContainer = document.getElementById("sellingPlacesContainer");
	if (frameContainer != null)
	{
		frameContainer.style.display = "none";
		setElementOpacity("container", 1);
	}
}

function basket_checkQuantity(quantityInputId)
{
	var quantity = trimString(document.getElementById(quantityInputId).value);
	var quantityNumber = 0;
	
	try {
		quantityNumber = Number(quantity);
	}
	catch(e) {
		quantityNumber = -1;
	}
	
	if (quantityNumber > 0) return true;
	else
	{
		window.alert("Zadané množství nelze použít.");
		return false;
	}
}

function basket_confirmItemDeletion()
{
	return window.confirm("Opravdu chcete tento produkt vyjmout z košíku?");
}

function order_validateDeliveryAddress(validator, args, fnameInputId, lnameInputId, streetInputId, cityInputId, zipInputId, compInputId)
{
	var fname = trimString(document.getElementById(fnameInputId).value);
	var lname = trimString(document.getElementById(lnameInputId).value);
	var street = trimString(document.getElementById(streetInputId).value);
	var city = trimString(document.getElementById(cityInputId).value);
	var zip = trimString(document.getElementById(zipInputId).value);
	var comp = trimString(document.getElementById(compInputId).value);
	
	if (fname.length > 0 || lname.length > 0 || street.length > 0 || zip.length > 0 || comp.length > 0)
	{
		if (fname.length == 0 || lname.length == 0 || street.length == 0 || zip.length == 0 || !stringMatchesRegExp(zip, ZipRegExpPattern)) args.IsValid = false;
		else args.IsValid = true;
	}
	else args.IsValid = true;
}

/// <summary>
/// Odstrani prebytecne bile znaky na prave a leve strane.
/// </summary>
function trimString(s) {
	
	if (s == null) return "";
	else
	{
		var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
		return (m == null) ? "" : m[1];
	}
}

function browserIsIE() {
	return navigator.appName.indexOf("Microsoft") != -1;
}

function getWindowProportions() {

	var w = 800; var h = 600;
	
	if (browserIsIE()) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	var size = {
		width: w,
		height: h
	};
	return size;
}

function getWindowScrollTop() {

	var fromTop					= document.body.scrollTop;
	if (fromTop == null || fromTop == 0) {
		if (window.pageYOffset)
			fromTop				= window.pageYOffset;
		else if (document.body.parentElement && document.body.parentElement.scrollTop)
			fromTop				= document.body.parentElement.scrollTop;
	}
	
	return fromTop;
}

function setElementOpacity(elemId, opac) {

	var elem = document.getElementById(elemId);
	
	if (elem != null)
	{
		if (browserIsIE()) elem.style.filter = "alpha(opacity="+String(opac * 100)+")";
		else elem.style.opacity = String(opac);
	}
}

function stringMatchesRegExp(value, rx) {
    
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function keepInputMaxLength(inputId, maxLength) {
	var input = document.getElementById(inputId);
	if (input != null && input.value.length > maxLength)
	{
		input.value = input.value.substring(0, maxLength);
	}
}
