function hideRow(obj, rowid) {
	var row = document.getElementById(rowid);
	if(obj.value == 1) {
		row.style.display = '';
	} else {
		row.style.display = 'none';
	}
}

function toggleVarComments(obj, rowid) {
	var row = document.getElementById(rowid);
	if(obj.value == 2) {
		row.style.display = '';
	} else {
		row.style.display = 'none';
	}
}

// toggle which optional fields to show based on the current selection
function toggleVarCommentsArr(obj, row_arr) {
	for (var i = 0; i < row_arr.length; i++) {
		if (row_arr[i] == '') {
			continue;
		}
		var row = document.getElementById(row_arr[i]);
		if(obj.value == i) {
			row.style.display = '';
		} else {
			row.style.display = 'none';
		}
	}
}

// display message and return false if input field is null
function checkInputFieldNotNull(id, msg, parentId) {
	if (parentId && !isVisible(parentId)) {
		// can't see the field, don't worry about validating
		return true;
	}
	
	var el = document.getElementById(id);
	
	if (!el || el == '') {
		return true;
	}
	
	if (el.value == '') {
		alert(msg);
		
		return false;
	}

	return true;
}

// return true if an element's display style is not 'none'
function isVisible(id) {
	var el = document.getElementById(id);
	
	if (!el || el == '' || el.style.display == 'none') {
		return false;
	}
	
	return true;
}

function showUploading() {
	var el = document.getElementById('upload_status');
	if (el) {
		el.style.display = 'block';
	}
} 

jq(document).ready(function() {
	//	jQuery code goes here

	// only show region selector when editing users who are regional managers
	function fixSelects(authlevel) {
		var level = authlevel.children('[selected]').text();
		var contractor_company = jq('[name=contractor_company_id]').eq(0);
		var clg_notification = jq('[name=clg_notification]').eq(0);
		
		switch (level) {
			case 'Contractor':
				contractor_company.css('visibility', 'visible');
				clg_notification.css('visibility', 'hidden');
				break;
			case 'CLG Admin':
				contractor_company.css('visibility', 'hidden');
				clg_notification.css('visibility', 'visible');
				break
			default:
				contractor_company.css('visibility', 'hidden');
				clg_notification.css('visibility', 'hidden');
				break;
		}
	}

	var authlevel = jq('[name=AUTHLEVEL]').eq(0);

	if (authlevel.length > 0) {
		// found the select
		var contractor_company = jq('[name=contractor_company_id]').eq(0);
		
		fixSelects(authlevel);
		authlevel.bind('change', null, function(e){
			fixSelects(authlevel);
		});
	}
	
});

//  This jquery plugin allows the easy setup of calendar controls that don't use an input box
jq.fn.setupDatePicker = function(initialDate, options, updateClickFunc) {
    //  Add the update button
    jq(this).append('<div class="datepickerwrapper" style="position: absolute">');
	jq(this).children('div').append('<input class="upd_date_btn" type="button" value="Update Date"/>');

    //  Setup the date picker and set the options, then hide it
    jq(this).children('div').datepicker();
	if (initialDate) { jq(this).children('div').datepicker("setDate", initialDate);}
	if (options) { jq(this).children('div').datepicker(options);}
    jq(this).children('div').hide();

	//  This hides/shows the picker and update button when you click on the image
    jq(this).children("img").bind('click', function() {
        jq(this).parent().children('div').toggle("fast");
    });

	//  This fires off the updateClickFunc function and hides the picker
    jq(this).children('div').children("input.upd_date_btn").bind('click', function() {
        if (updateClickFunc) { updateClickFunc(jq(this).parent());}
        jq(this).parent().parent().children("img").click();
    });
}

function initSaveMilestoneAmountButton()
{
	jq('INPUT[id^=ms_milestone_value_save_]').unbind('click');
	jq('INPUT[id^=ms_milestone_value_save_]').bind('click',function(){
		var milestone_id = jq(this).attr('rel');
		var milestone_amount = jq('INPUT[name=msi_milestone_value1_'+milestone_id+']').val();
		var param = {mid: milestone_id, mvalue: milestone_amount};
		jq.getJSON('/brrp/project/ajax/savemilestoneamount', param, function(json){
			if(json.error_code == 0){
				alert('Saved');
				calculateTotal();
			}else{
				alert(json.error_message);
			}
		});
	});
}
function calculateTotal()
{
	var t = 0;
	jq('INPUT[name^=msi_milestone_value1_]').each(function(index){
		var v = jq(this).val();
		if(isNaN(v) == false && v != ''){
			t = t + parseFloat(v);
		}
	});
	jq('SPAN[name^=span_msi_milestone_value1_]').each(function(index){
		var v = jq(this).text();
		if(isNaN(v) == false && v != ''){
			t = t + parseFloat(v);
		}
	});	
	jq('.mtotal').text(t.toFixed(2));
}
