/**
 * This scripts are available everywhere in leaptranet application
 * @autor: Francisco Baptista
 * @version:
 */
function expensesValidadeDate(day, month, year){
	if(month < 10) {
		var month = '0'+month;
	}
	var myDate	= new Date();
	myDate.setFullYear(year,month,day);
	var today	= new Date();

	//alert(myDate + ' - ' + today);

	if (myDate>today) {
		alert("You can not choose dates in the future.");
		return false;
	}

	return true;
}
function expensesToggleMileageOptions(seleted_option,id){
	var mileage = 1;
	if(seleted_option == mileage){
		//$('str_description_'+id).hide();
		Effect.Appear('mileage_details_'+id,{duraction:8.0});
		$('int_cost_'+id).disable();
	} else {
		$('mileage_details_'+id).hide();
		//Effect.Appear('str_description_'+id,{duraction:8.0});
		$('int_cost_'+id).enable();
		expensesTotalCost();//update the total cost
	}
}

function expensesToggleReportType(seleted_option){
	//alert(seleted_option);
	if(seleted_option == 'employees'){
		$('departments').hide();
		Effect.Appear('employees',{duraction:8.0});
		Effect.Appear('period_row',{duraction:8.0});
		Effect.Appear('filters',{duraction:8.0});
		Effect.Appear('btsubmit',{duraction:8.0});
	} else if(seleted_option == 'departments'){
		$('employees').hide();
		Effect.Appear('departments',{duraction:8.0});
		Effect.Appear('period_row',{duraction:8.0});
		Effect.Appear('filters',{duraction:8.0});
		Effect.Appear('btsubmit',{duraction:8.0});
	} else {
		$('departments').hide();
		$('employees').hide();
		$('period_row').hide();
		$('filters').hide();
		$('graphs').hide();
		$('btsubmit').hide();
	}
}

function expensesMileageSetCost(cost,field_position) {
	if(cost){
		var miles = $('int_miles_'+field_position).value
		if(cost>0 && miles>0){
			var setedcost = parseFloat(cost * miles);
			$('int_cost_'+field_position).value = setedcost.toFixed(2);
			expensesTotalCost();//update the total cost
		} else {
			//alert('Please fill up the Miles and p/mile cost.');
		}
	}
}

function expensesTotalCost(){
	var els = $('step2_expenses').getElementsByClassName('cost');
	var total = 0;
	for (var xpto=0;xpto<els.length;xpto++){
		if(els[xpto].value>0 && els[xpto].style.display==''){
			total += parseFloat(els[xpto].value);
			//alert(els[xpto].value + " " + els[xpto].type + " " + typeof(total) + " " + typeof(els[xpto].value) + ' - ' + els[xpto].style.display);
		}
	}
	if(total){
		$('total_cost').innerHTML = total.toFixed(2);
	}
}
/**
 * Get the details of the expenses grouped by user,category,currency on the approval report
 */
function expensesShowDetails(user_email, category, currency, status){
	//alert(user_email + ' ' + category + ' ' + currency + ' ' + status);
	Element.toggle($('details_line_'+category+'_'+currency));
	if($('details_line_'+category+'_'+currency).style.display != 'none'){
		new Ajax.Request('http://www.leaptra.net/index.php/expenses/showdetails/'+user_email+'/'+category+'/'+currency+'/'+status+'/', {
			onLoading:function(response){
				$('details_cell_'+category+'_'+currency).innerHTML = 'Loading...';
			},
			onSuccess:function(response){
				var result = response;
				//alert(response.responseText);
				$('details_cell_'+category+'_'+currency).innerHTML = response.responseText;
			}
		});
	}
	if($('details_line_'+category+'_'+currency).style.display != 'none'){
		$('details_line_text_'+category+'_'+currency).innerHTML = "Hide Expense Lines";
	} else {
		$('details_line_text_'+category+'_'+currency).innerHTML = "Show Expense Lines";
	}
}

function expensesActionDelete(expense_base_id) {
	if(confirm('Are you sure you want to delete this Expenses?')){
		$$('.exp_'+expense_base_id).each(function(el) {
			var element = $(el);
			if(el.checked){
				new Ajax.Request('http://www.leaptra.net/index.php/expenses/actiondelete/'+el.value+'/'+expense_base_id, {
					onLoading:function(response){
						//todo: maybe haveing a spin icon to represent loading...
					},
					onSuccess:function(response){
						//alert(response.responseText);
						if(response.responseText == 'last'){
							Effect.Fade('expenses_'+expense_base_id);
						} else {
							Effect.Fade('expense_row_'+el.value);
						}
					}
				});
			}
		});
	}
}

/**
 * This are for the Your Account page
 *
 */
function defaultAvatar(){
	Element.toggle($('default-avatar'));
	if($('default-avatar').style.display != 'none'){
		$('default_avatar_value').value='default';


	} else {
		$('default_avatar_value').value='';
	}
}
function passwordCheck(){
	if($('password').value && $('password2').value==""){
		alert('Please the retype the password.')
		return false;
	}
	if($('password').value != $('password2').value){
		alert('Please the passwords have to match.')
		return false;
	}
	return true;
}

/**
 * Holidays request
 *
 */
function holidaysTogglePeriodRow(status){
	//alert(status);
	if(status!='0'){
		$('period_row').style.display = '';
		$('period_months').enable();
		$('period_years').enable();
	} else {
		$('period_row').style.display = 'none';
		$('period_months').disable();
		$('period_years').disable();
	}
}
function holidaysSwapPeriodRow(seleted_option,id){
	if(seleted_option=='0'){
		//$(id).hide();
		Effect.Fade(id,{duraction:8.0});
		$('period_months').disable();
		$('period_years').disable();
	} else {
		Effect.Appear(id,{duraction:8.0});
		$('period_months').enable();
		$('period_years').enable();
	}
}
/**
 * Ajax Actions ::
 */
function holidaysActionRequestCancelation(id){
	//Element.toggle($('details-line-'+category));
	if(confirm('Are you sure you want to request the cancelation of this holiday request? \n\r This will email your line manager which has permission to cancel it.')){
		new Ajax.Request('http://www.leaptra.net/index.php/holidays/actioncancelation/'+id, {
			onSuccess:function(response){
				//alert(response.responseText);
				Effect.Fade('request_cancelation_button_'+id);
				$('to_approve_text_'+id).style.color = "red";
				$('to_approve_text_'+id).innerHTML = "Cancelation requested...";
			}
		});
	}
}
function holidaysActionDelete(id){
	//Element.toggle($('details-line-'+category));
	if(confirm('The reqquest will be permanetly deleted. \n\r Are you sure you want to delete this holiday request?')){
		new Ajax.Request('http://www.leaptra.net/index.php/holidays/actiondelete/'+id, {
			onSuccess:function(response){
				//alert(response.responseText);
				//Effect.Fade('request_'+id);
				Effect.Fade('delete_button_'+id);
				if($('approve_button_'+id)){
					Effect.Fade('approve_button_'+id);
				}
				$('to_approve_text_'+id).style.color = "red";
				$('to_approve_text_'+id).innerHTML = "Cancelled";
			}
		});
	}
}
function holidaysActionApprove(id){
	new Ajax.Request('http://www.leaptra.net/index.php/holidays/actionapprove/'+id, {
		onSuccess:function(response){
			//alert(response.responseText);
			if (response.responseText > 0) {
				Effect.Fade('approve_button_'+id);
				$('to_approve_text_'+id).style.color = "green";
				$('to_approve_text_'+id).innerHTML = "Aproved";
			} else {
				alert('Error: Something went really really wrong if you can see this message. \n\r Please contact: support@leaptra.net');
			}
		}
	});
}

function holidaysBuildRequest(year, month, day){

	if(month < 10) {
		var month = '0'+month;
	}

	var id 		=  day + '_' + month + '_' + year;
	var myDate	= new Date();
	myDate.setFullYear(year,(month-1),day);
	today 		= new Date();

	//alert(myDate + ' - ' + today);
	if (myDate<today) {
		alert("You can not choose dates in the past.\n\nPlease contact support@leaptra.net otherwise.");
		return false;
	}
	//alert($('cell_'+id).style.backgroundColor);
	if($('cell_'+id).style.backgroundColor == 'rgb(255, 255, 255)' || $('cell_'+id).style.backgroundColor == 'rgb(255, 255, 170)' || $('cell_'+id).style.backgroundColor == '#ffffff' || $('cell_'+id).style.backgroundColor == '#FFFFFF' || $('cell_'+id).style.backgroundColor == 'rgb(255,255,255)') {
		$('cell_'+id).style.backgroundColor = 'rgb(255,231,156)';
		if(!$('field_'+id)) {
			$('choosen_days').innerHTML += '<input id="field_' + id + '" type="hidden" name="days[]" value="' +  day + '-' + month + '-' + year + '" />';
		}
	} else {
		$('cell_'+id).style.backgroundColor='rgb(255, 255, 255)';
		if($('field_' + id)) {
			$('field_' + id).remove();
		}
	}

}
function holidaysDaysHighlight(date) {
	if($('cell_'+date)){
		if($('cell_'+date).style.backgroundColor=='rgb(255, 255, 255)' || $('cell_'+date).style.backgroundColor=='rgb(255, 255, 170)' || $('cell_'+date).style.backgroundColor=='#ffffff' || $('cell_'+date).style.backgroundColor=='#FFFFFF' || $('cell_'+date).style.backgroundColor=='rgb(255,255,255)') {
			$('cell_'+date).style.backgroundColor='rgb(255,231,156)';
		}
	}
}
function holidaysUpdateCount(){
	//count total
	var els = $('holidays_duration').getElementsByClassName('days_duration');
	//alert(els.length);
	var total = 0;
	for (var xpto=0;xpto<els.length;xpto++){
		//alert(xpto);
		if(els[xpto].value == 'day'){
			total += 1;
			//if(!xlen) {xlen=0;}
		} else {
			//xlen = 1;
			total += .5;
		}
	}
	if(total){
		$('total_days').innerHTML = total.toFixed(1);
	}
}
function holidaysRequestPrev(url){
	document.frmStep1.action = url;
	document.frmStep1.submit();
}

/**
 * Site wide functions
 *
 */
//collapse/expand content boxes
function toggle_collapse_and_text(id, text_container) {
	e = document.getElementById(id);
	if(!e) return;

	if(e.style.visibility == "visible") {
		e.style.visibility = "hidden";
		e.style.position = "absolute";
		//text_container.firstChild.className="down-arrow";
	} else {
		e.style.position = "relative";
		e.style.visibility = "visible";
		//text_container.firstChild.className="up-arrow";
	}
}


/**
 * Sickness request
 *
 */
function sicknessTogglePeriodRow(status){
	//alert(status);
	if(status!='0'){
		$('period_row').style.display = '';
		$('period_months').enable();
		$('period_years').enable();
	} else {
		$('period_row').style.display = 'none';
		$('period_months').disable();
		$('period_years').disable();
	}
}
function sicknessSwapPeriodRow(seleted_option,id){
	if(seleted_option=='0'){
		//$(id).hide();
		Effect.Fade(id,{duraction:8.0});
		$('period_months').disable();
		$('period_years').disable();
	} else {
		Effect.Appear(id,{duraction:8.0});
		$('period_months').enable();
		$('period_years').enable();
	}
}
/**
 * Ajax Actions ::
 */
function sicknessActionRequestCancelation(id){
	//Element.toggle($('details-line-'+category));
	if(confirm('Are you sure you want to request the cancelation of this sickness leave? \n\r This will email your line manager which has permission to cancel it.')){
		new Ajax.Request('http://www.leaptra.net/index.php/sickness/actioncancelation/'+id, {
			onSuccess:function(response){
				//alert(response.responseText);
				Effect.Fade('request_cancelation_button_'+id);
				$('to_approve_text_'+id).style.color = "red";
				$('to_approve_text_'+id).innerHTML = "Cancelation requested...";
			}
		});
	}
}
function sicknessActionDelete(id){
	//Element.toggle($('details-line-'+category));
	if(confirm('The reqquest will be permanetly deleted. \n\r Are you sure you want to delete this sickness leave?')){
		new Ajax.Request('http://www.leaptra.net/index.php/sickness/actiondelete/'+id, {
			onSuccess:function(response){
				//alert(response.responseText);
				//Effect.Fade('request_'+id);
				Effect.Fade('delete_button_'+id);
				if($('approve_button_'+id)){
					Effect.Fade('approve_button_'+id);
				}
				$('to_approve_text_'+id).style.color = "red";
				$('to_approve_text_'+id).innerHTML = "Cancelled";
			}
		});
	}
}
function sicknessActionApprove(id){
	new Ajax.Request('http://www.leaptra.net/index.php/sickness/actionapprove/'+id, {
		onSuccess:function(response){
			//alert(response.responseText);
			if (response.responseText > 0) {
				Effect.Fade('approve_button_'+id);
				$('to_approve_text_'+id).style.color = "green";
				$('to_approve_text_'+id).innerHTML = "Aproved";
			} else {
				alert('Error: Something went really really wrong if you can see this message. \n\r Please contact: support@leaptra.net');
			}
		}
	});
}

function sicknessBuildRequest(year, month, day){

	if(month < 10) {
		var month = '0'+month;
	}

	var id 		=  day + '_' + month + '_' + year;
	var myDate	= new Date();
	myDate.setFullYear(year,(month-1),day);
	today 		= new Date();

	//alert(myDate + ' - ' + today);
	if (myDate>today) {
		alert("You can not choose dates in the future.\n\nPlease contact support@leaptra.net otherwise.");
		return false;
	}
	//alert($('cell_'+id).style.backgroundColor);
	if($('cell_'+id).style.backgroundColor == 'rgb(255, 255, 255)' || $('cell_'+id).style.backgroundColor == 'rgb(255, 255, 170)' || $('cell_'+id).style.backgroundColor == '#ffffff' || $('cell_'+id).style.backgroundColor == '#FFFFFF' || $('cell_'+id).style.backgroundColor == 'rgb(255,255,255)') {
		$('cell_'+id).style.backgroundColor = 'rgb(255,231,156)';
		if(!$('field_'+id)) {
			$('choosen_days').innerHTML += '<input id="field_' + id + '" type="hidden" name="days[]" value="' +  day + '-' + month + '-' + year + '" />';
		}
	} else {
		$('cell_'+id).style.backgroundColor='rgb(255, 255, 255)';
		if($('field_' + id)) {
			$('field_' + id).remove();
		}
	}

}
function sicknessDaysHighlight(date) {
	if($('cell_'+date)){
		if($('cell_'+date).style.backgroundColor=='rgb(255, 255, 255)' || $('cell_'+date).style.backgroundColor=='rgb(255, 255, 170)' || $('cell_'+date).style.backgroundColor=='#ffffff' || $('cell_'+date).style.backgroundColor=='#FFFFFF' || $('cell_'+date).style.backgroundColor=='rgb(255,255,255)') {
			$('cell_'+date).style.backgroundColor='rgb(255,231,156)';
		}
	}
}
function sicknessUpdateCount(){
	//count total
	var els = $('sickness_duration').getElementsByClassName('days_duration');
	//alert(els.length);
	var total = 0;
	for (var xpto=0;xpto<els.length;xpto++){
		//alert(xpto);
		if(els[xpto].value == 'day'){
			total += 1;
			//if(!xlen) {xlen=0;}
		} else {
			//xlen = 1;
			total += .5;
		}
	}
	if(total){
		$('total_days').innerHTML = total.toFixed(1);
	}
}

function sicknessRequestPrev(url){
	document.frmStep1.action = url;
	document.frmStep1.submit();
}

function usersConfirmDissabled() {
	if($('dissable').checked) {//do nothing
		if(confirm('Are you sure you want to dissable this User Account?')){
			return true;
		} else {
			return false;
		}
	}
}