﻿// JScript File

/*  Sets the height of specified item
    @param itemName - the name of the item who's height to set
*/
function SetContentHeight(itemName)
{
    //var myItem = document.getElementById(itemName);
    //myItem.height = window.screen.height - 358;
}

function SetAdminContentHeight(itemName)
{
    var myItem = document.getElementById(itemName);
    myItem.height = window.screen.height - 308;
}

function openPrintPage(url) {
    window.open(url, "_blank", "height=" + (window.screen.height-100) + ",width=750,left=0,top=0,status=0,scrollbars=1, toolbar=0, location=0,resizable=1")
}

function SetFocus(itemName)
{
    var myItem = document.getElementById(itemName);
    myItem.focus();
}

function printPage() {
  if (window.print)
    window.print();
  else
    alert("Tyvärr stödjer inte din browser denna funktion!");
}

function isMaxLength(obj, maxLength) {
    if (obj.getAttribute && obj.value.length > maxLength)
    {
        obj.value = obj.value.substring(0, maxLength)
        alert("Max " + maxLength + " tecken!" );
    }
}

function validateDate(obj, allowEmpty)
{
    if (obj.value.length > 0)
    {
        var date = obj.value;
	    var dateFormat = "yyyy-MM-dd";
	    var dateSeparator = (date.indexOf("/")!=-1)?"/":(date.indexOf(".")!=-1?".":(date.indexOf("-")!=-1?"-":" "));
	    var strDate = _parseDate(date, allowEmpty, dateFormat, dateSeparator, dateFormat);

	    if(strDate == "")
		    obj.value = "";

	    obj.value = formatDate(strDate, dateFormat);
	}
};

function _parseDate(date, allowEmpty, dateFormat, dateSeparator)
{
	
	// Index
	var yInx = dateFormat.indexOf("y");
	var mInx = dateFormat.indexOf("M");
	var dInx = dateFormat.indexOf("d");

	// Order
	var yOrd, mOrd, dOrd;
	yOrd = yInx == 0 ? 0 : 2;
	mOrd = yOrd == 0 ? 1 : (mInx == 0 ? 0 : 1);
	dOrd = yOrd == 0 ? 2 : (dInx == 0 ? 0 : 1);
	
	var today = new Date();
	var date_string, day, month, year;

	// Convert the input parameter to a string
	date_string = date.toString();
	// If empty result is allowed, and the input is empty...
	if(allowEmpty && date.length == 0)
	{
		// Return nothing
		return "";
	}
	
	// If the input date contains separators
	if(date_string.indexOf(dateSeparator) >= 0)
	{
		// Split the string into an array
		var parts = date_string.split(dateSeparator);
		
		// Get the date parts
		if(yOrd == 0)
		{
			day = parts[parts.length - 1];
			month = parts[parts.length - 2];
			year = parts[parts.length - 3];
		}
		else
		{
			day = parts[dOrd];
			month = parts[mOrd];
			year = parts[yOrd];
		}
		// If year is undefined, get current year
		if(year == undefined)
			year = today.getYear();

	}
	else
	{
		// The input date doesn't contain any separators
		day = undefined;
		month = undefined;
		year = undefined;
		
		if(date_string.length > 1 && date_string.length % 2 == 1)
			throw "not_valid_date";
		
		// If year is entered...
        if(date_string.length > 6)
		{
			//if (yOrd == 0)
			year = date_string.slice(yOrd * 2, yOrd * 2 + 4);
			//else throw "not_valid_date";
		}
		else if(date_string.length > 4)
     		year = date_string.slice(yOrd * 2, yOrd * 2 + 2);

		// If month is entered...
		if(date_string.length >= 4)
		{
			if(yOrd > 0)
				month = date_string.slice(mOrd * 2, mOrd * 2 + 2);
			else
				month = date_string.slice(date_string.length - 4, date_string.length - 2);
		}

		// If day is entered...
		if(date_string.length > 2)
		{
			if(yOrd > 0)
				day = date_string.slice(dOrd * 2, dOrd * 2 + 2);
			else
				day = date_string.slice(date_string.length - 2, date_string.length);
		}
		else if(date_string.length > 0)
			day = date_string;
		// Now fill in missing parts from current date
		if(year == undefined)
			{year = today.getYear();
			if(navigator.appName == "Netscape")
				{year =year +1900};
			}	
		if(month == undefined)
			month = today.getMonth() + 1;
		if(day == undefined)
			day = today.getDate();
	}
	if(month > 12)
		throw "not_valid_date";
	// If there are only two digits in the year, add current century
	if(year.length <=2)
		year = today.getFullYear().toString().slice(0, 1) * 1000 + parseInt(year,10);
	
	// Construct a new date
	var d = new Date(year, month - 1, day);
	if(day != d.getDate() && date.length > 0)
		throw "not_valid_date";

	return d;
};

function formatDate(date, dateFormat)
{
	var str = dateFormat;
	
	str = str.replace(/yyyy/g, date.getFullYear());
	str = str.replace(/yy/g, date.getFullYear());
	
	if(date.getMonth() + 1 < 10)
		str = str.replace(/MM/g, "0" + (date.getMonth() + 1));
	else
		str = str.replace(/MM/g, date.getMonth() + 1);
	str = str.replace(/M/g, date.getMonth() + 1);
	
	if(date.getDate() < 10)
		str = str.replace(/dd/g, "0" + date.getDate());
	else
		str = str.replace(/dd/g, date.getDate());
	str = str.replace(/d/g, date.getDate());
	
	return  str;
}
