$(document).ready(function() {
	/* Checkbox fixes voor IE */
	$('input[type="checkbox"]').css({
		border : '0'
	});
	
	$('th input[type="checkbox"]').css({
		'background-color' : '#575757'
	});
	
	$('input[type="text"]').css({
		'width' : '250px'
	});
	
	$('input[type="text"]').css({
		'width' : '250px'
	});
	
	$('form .big').css({
		width : '335px',
		'font-size' : '1.3em',
		'padding-bottom' : '2px'
	});
	
	$('input[type="submit"], input[type="button"]').css({
		'margin-top' : '1px',
		'padding-right' : '4px',
		'padding-left' : '4px',
		color : '#fff',
		background : '#51bbc9',
		border : '1px solid #36b0c1',
		cursor : 'pointer'
	});
	
	$('input[type="submit"], input[type="button"]').hover(
		function() {
			$(this).css({
				background : '#73d0dc'
			});
		},
		function() {
			$(this).css({
				background : '#51bbc9'
			});
		});
	
	$.get('keepalive.asp', function(data) {
		if (window.location.href.indexOf('inbox.asp') != -1 && data.indexOf('dontKeepInboxActiveIdiot') == -1) {
			var refreshType = (data.indexOf('youHaveOneNewEmail') != -1) ? 'announce' : 'all';
			InitializeTimer(60, refreshType);
		}
	});
});

/* AJAX RELOAD */

var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer(time, refreshType)
{
    secs = time
    StopTheClock()
    StartTheTimer(refreshType)
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer(refreshType)
{
    if (secs==0)
    {
        StopTheClock()
        window.location = 'inbox.asp?PG=&REFRESH=' + refreshType;
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer('" + refreshType + "')", delay)
    }
}

/* INBOX */

function checkCheckboxes() {
	var docForm=document.inboxForm;
	var len = docForm.elements.length;
	for( var i=0 ; i<len ; i++) {
		if (docForm.elements[i].name == 'delCheck') {
			if (docForm.elements[i].checked != '') return true;
		}
	}
	return false;
}

function confirmDelete(nothingSelected, deleteConfirm) {
	if ( !checkCheckboxes() ) {
		alert(nothingSelected);
		return false;
	}
	if (confirm(deleteConfirm)) {
		document.inboxForm.submit();
	}
	return false;
}

function checkAll() {
	var docForm=document.inboxForm;
	for (var i=0;i<docForm.elements.length;i++) {
		if (docForm.elements[i].name == 'delCheck') {
			docForm.elements[i].checked = docForm.allCheck.checked;
		}
	}
}

function showMessage( ID ) {
	window.location.href = "message.asp?ID="+ID;
	return false;
}

/* COMPOSE */

function isEmail(val) {
  	return (val.indexOf("@")>0 && val.lastIndexOf(".")>(val.indexOf("@")+1))
}

function validateForm(errorMissingFrom, errorMissingTo, errorMissingSubject) {
  	var strEmailAddr = document.senderForm.eMailFrom.value + document.senderForm.eMailDomain.value;
  	if (!isEmail(strEmailAddr)) {
    	alert(errorMissingFrom);
   		return false;
  	}
  	else if (!isEmail(document.senderForm.eMailTo.value)) {
    	alert(errorMissingTo);
    	return false;
  	}
  	else if (document.senderForm.eMailSubject.value == "") {
    	return confirm(errorMissingSubject);
  	}
  	else {
    	return true;
  	}  
}  

/* UPLOAD FORM */

function clearList() {
	document.forms.senderForm.attachments.value="";
	var params = 'width=550,height=160,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=no';
	window.open("clearattach.asp", 'clearlist', params);
	return false;
}

function openUploader() {
	var isAttachments = (document.forms.senderForm.attachments.value=="") ? "false" : "true";
	var params = 'width=550,height=240,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=no';
	window.open("uploadfile.asp?attachments="+isAttachments, 'uploader', params);
	return false;
}

/* MESSAGE */

function reply(btnReply) {
	document.replyForm.replyAction.value = btnReply;
	document.replyForm.submit();
	return false;
}

function replyAll(btnReplyAll) {
	document.replyForm.replyAction.value = btnReplyAll;
	document.replyForm.submit();
	return false;
}

function msgForward(btnForward) {
	document.replyForm.replyAction.value = btnForward;
	document.replyForm.submit();
	return false;
}

function viewHTML( msg ) {
	var params = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes';
	window.open('viewhtml.asp?ID=' + msg, '', params)
	return false;
}

function openAttachment( fName ) {
	var params = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes';
	window.open('attachment.asp?file=' + fName, '', params)
	return false;
}

/* CONFIRM DELETE */

function showPage( strPage ) {
	window.location.href = strPage
	return false;
}

/* UPLOAD FILE */

function validateUploadForm() {
	if (document.senderForm.file.value == "") {
    	return false;
	} else {
    	return true;
	}
} 

/* CLEAR ATTACHMENT */

function doCancel(attachments) {
	opener.document.forms.senderForm.attachments.value = attachments;
	window.close();
}