var valid = true;

function CheckTab() {
	// Run only in IE
	if ((document.all) && (76==event.keyCode) && (event.ctrlKey) && (event.altKey)) {
		ShowLogin();
		return false;
	}
}

function ShowLogin() {
	var o = new Object();
	o.retval = false;
	window.showModalDialog("modals/modal_login.aspx",o,"dialogHeight: 220px; dialogWidth: 300px; edge: Raised; center: Yes; help: no; resizable: No; status: No;");
	if(o.retval){
		location.href = location.href;
		//top.location.reload();
		}
	
}

function galleryPopUp(resourceId, galleryId) {
	window.open("PictureViewer.aspx?imageid=" + resourceId + "&gid=" + galleryId, "", "width=500,height=500,scrollbars=no,resizable=no,status=no");
}

function printNewsPopUp(newsId) {
	window.open("NewsPrint.aspx?newsid=" + newsId, "", "width=500,height=500,scrollbars=yes,resizable=no,status=no");
}

function initiatePage()
{
	var myDivs = document.getElementsByTagName("div");
	var re = /^divScroll_/;
	for(var i = 0; i < myDivs.length; i++)
	{
		if(re.test(myDivs[i].id))
		{
			var myDiv = myDivs[i];
			if(myDiv.offsetHeight > 290)
			{
				var myArr = myDiv.id.split("_");
				var myModule = myArr[1];
				myDiv.style.height = "290px";
				document.getElementById("tdScroll_" + myModule).style.display = "";
				document.getElementById("tdDoh_" + myModule).className = "htmlContent_wScroll";
				document.getElementById("divScrollContent_" + myModule).style.position = "absolute";
				document.getElementById("divScrollContent_" + myModule).style.top = "0px";
				document.getElementById("divScrollContent_" + myModule).style.left = "0px";
			}
		}	
	}
}

//specify speed of scroll (greater=faster)
var speed=2
var crossobj; 
var contentheight;


function FaqShowItem(moduleId, item)
{
	var i = 1;
	var currentDispay = document.getElementById('faqItem_' + moduleId + "_" + item).style.display;
	
	while(eval("document.getElementById('faqItem_" + moduleId + "_" + i + "')"))
	{
		eval("document.getElementById('faqItem_" + moduleId + "_" + i + "').style.display = 'none'");
		eval("document.getElementById('bullit_" + moduleId + "_" + i + "').src = 'gfx/siteSpecific/faq_arrow_green_right.gif'");
		eval("document.getElementById('header_" + moduleId + "_" + i + "').style.color = '#636467'");
		i++;
	}
	if(currentDispay == "none")
	{
		eval("document.getElementById('faqItem_" + moduleId + "_" + item + "').style.display = 'block'");
		eval("document.getElementById('bullit_" + moduleId + "_" + item + "').src = 'gfx/siteSpecific/faq_arrow_green_down.gif'");
		eval("document.getElementById('header_" + moduleId + "_" + item + "').style.color = '#D41F23'");
	}
	else
	{

		eval("document.getElementById('faqItem_" + moduleId + "_" + item + "').style.display = 'none'");
		eval("document.getElementById('bullit_" + moduleId + "_" + item + "').src = 'gfx/siteSpecific/faq_arrow_green_right.gif'");
		eval("document.getElementById('header_" + moduleId + "_" + item + "').style.color = '#636467'");
	}
}

function email()
{
	location.href = "mailto:larslinde@gmail.com?subject=Maersk EDU&body=" + document.URL;
	return false;
}

function popup(width, height, closeLink, imageLink)
{
	
	var doc = window.open("resources/popup.htm", 'doc', "width=" + width + ", height=" + height + ", scrollbars=no");
	doc.document.write("<html><head><title></title><link rel='stylesheet' href='resources/styles.css'></head><body><img src='" + imageLink + "' border='0'><br>" + (closeLink == true ? "<div align='center'><a href='javascript: window.close();'>Close window</a></div>" : "") + "</body></html>");
}

function mandatoryString(objElement)
{
    if (valid)
    {
        if(objElement.value == '')
        {
            valid = false;
            alert('feltet skal udfyldes');
            objElement.focus();
            return false;
        }    
        valid = true;
        return true;
    }
    valid = true;
    return true;
}

function mandatoryNumeric(objElement)
{
    if (valid)
    {
        if(objElement.value == '')
        {
            valid = false;
            alert('feltet skal udfyldes');
            objElement.focus();
            return false;
        }    
        return TalValidering(objElement);
    }
    valid = true;
    return false;
}

function mandatoryDate(objElement)
{
    if (valid)
    {
        if (objElement.value.length == 0)
        {
            valid = false;
            alert("Feltet skal udfyldes");
            objElement.focus();
            return false;
        }
        valid = true;
        return leavedate(objElement);
    }
    valid = true;
    return false;
}

function TalValidering(objElement)
{
    if (!isNumeric(objElement.value))
    {
        valid = false;
        alert("Ugyldigt tal. Indtast kun tal.")
        objElement.focus();
        return false;
    }
    valid = true;
    return true;
}

function isNumeric(s) 
{
    for (var i = 0; i < s.length; i++)
        if (s.charAt(i) < '0' || s.charAt(i) > '9')
            return false;
    return true;
}

var DaysInMonth;
DaysInMonth[0] = 31;
DaysInMonth[1] = 28;
DaysInMonth[2] = 31;
DaysInMonth[3] = 30;
DaysInMonth[4] = 31;
DaysInMonth[5] = 30;
DaysInMonth[6] = 31;
DaysInMonth[7] = 31;
DaysInMonth[8] = 30;
DaysInMonth[9] = 31;
DaysInMonth[10] = 30;
DaysInMonth[11] = 31;

function IsLeapYear(Year)
{
return (((Year % 4) == 0 && (Year % 100) != 0) || ((Year % 400) == 0)) ? 1 : 0;
}

function DaysInMonth(month, year)
{
    return (month == 2 && IsLeapYear(year)) ? 29 : DaysInMonth[month-1];
}

function isValidDateLongFormat(date) {
    var parseddate = parseDelimitedDate4(date);
    if (parseddate == null) return false;
    
    var dd = parseInt(parseddate.substring(0, 2),10);
        var mm = parseInt(parseddate.substring(3, 5), 10);
        var yy = parseInt(parseddate.substring(6, 10), 10);
        
        if ((mm<1) || (mm>12)) return false;
        
        if (yy >= 1000)
        {
        
            if (yy<1930) return false;
            if (yy>2029) return false;
        }
    
        if (dd<1) return false;
        var dim = DaysInMonth(mm,yy);
        
        if (dd>dim) return false;
    
    return true;
    
}

function parseDelimitedDate4(date) {
    if (date.match('[^0-9]*([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,4})[^0-9]*')) {
        var dd = RegExp.$1;
        if (dd.length == 1) dd = '0' + dd; 
        var mm = RegExp.$2;
        if (mm.length == 1) mm = '0' + mm;
        var yy = RegExp.$3;
        if (yy.length == 1)
            yy = '200' + yy;
        else if (yy.length == 2)
        {
            yy = (yy < 30 ? '20' : '19') + yy;
        }
	else if (yy.length == 3)
            return null;
        return dd + '-' + mm + '-' + yy;
    }
    return null;
}

function leavedate(objElement)
{
    var date = objElement.value;
    if (date.length == 0) return true;
    if (isValidDateLongFormat(date)) {
        //  add leading zeros
        objElement.value = parseDelimitedDate4(date);
        return true;
    }
    alert('Indtast korrekt dato fra 01-01-1930 : dd-mm-yyyy');
    objElement.focus();
    return false;
    
}
