var baseurl;
var loading_image;
var status_images = new Array();
var activity_log_type = new Array();
var service_act_log_type = new Array();
var contact_popup_constructed = 0;
var company_popup_constructed = 0;
var company_div = "";
var contact_div = "";
var currently_loaded = 0; //1 = companies loaded, and 2 = contacts loaded
var project_view = 0;
var contact_reload_page = 0;

var sURL = unescape(window.location.pathname);

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function refresh_window()
{
    //  This version of the refresh function will cause a new
    //  entry in the visitor's history.  It is provided for
    //  those browsers that only support JavaScript 1.0.
    //
    window.location.href = sURL;
	window.location.reload(true)
}


function remove_message_box(id)
{
	
	$("#system_message_box_"+id).hide();
	
	var formdata = "msg_id="+ id;
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'user/disable_msg/',
		dataType : 'json',
		data: formdata,
		success : function(data){
			window.location.reload();
		}
	});	

}

function address_warning(element, type){if(!$(element).attr('checked')){$(element).attr("checked", false);return true;}else{var check = confirm("Are you sure that you want to set a new primary " + type + "?\n Doing this will change the contacts primary " + type + ".");if(check === false){$(element).attr("checked", false);return true;}else{return true;}}}

function format_date(date, format)
{
	
	/*if(isDate(date,format))
	{
		
		alert("Date Confirmed");
		
		var date_object = getDateFromFormat(date,format);
		
		alert(date_object);
		
		return formatDate(date_object,"dd MMM yyyy HH:mm");
		
	}
	else
	{
		
		return false;	
		
	}*/
	
	
	return date;
	
}

function runEffect(element, effect){
	
	//most effect types need no options passed by default
	var options = {};
	
	//run the effect
	$(element).toggle(effect, options, 500);
	
};

function get_required_data()
{

	baseurl = document.getElementById("baseurl").value;
	
}

function emptyList(box) 
{
	// Set each option to null thus removing it
	while ( box.options.length ) box.options[0] = null;
}

function resize_element(element, width, height)
{
	
	var element = document.getElementById(element);
	if(width > -1) { element.style.width = width + "px"; }
	if(height > -1) { element.style.height = height + "px"; }
	
	return true;
	
}

function detect_element_height(element)
{
	
	var height = document.getElementById(element).style.height;	
	
	return height;
	
}

function add_table_rows(table_name, row)
{
	
	$(table_name).last().append(row); 

}

$().ready(function() {

  	$('#add_new_company').jqm();
  	$('#add_new_contact').jqm();
	$('#add_new_project').jqm();
	$('#add_new_service').jqm();
	$('#add_serv_invoice_model').jqm();
  	//get_companies();

	status_images[2] = '<img title="This Project is an Opportunity" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/opp-project-icon.png"/>';
	status_images[3] = '<img title="This Project is a Job" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/jobs-project-icon.png"/>';
	status_images[4] = '<img title="This Project has been Lost" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/lost-project-icon.png"/>';
	status_images[5] = '<img title="This Project has been Deferred" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/deferred-project-icon.png"/>';
	status_images[6] = '<img title="This Project has been Archived" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/archived-project-icon.png"/>';
	status_images[1] = '<img title="This project is a Lead" height="35" src="' + $('#baseurl').val() + 'resources/images/icons/lead-project-icon.png"/>';

	activity_log_type[1] = "Project Note";
	activity_log_type[2] = "";
	activity_log_type[3] = "Project File Uploaded";
	activity_log_type[4] = "Action Comment";
	activity_log_type[5] = "Action Reply";
	activity_log_type[6] = "New Note";
	activity_log_type[7] = "File Uploaded";
	activity_log_type[8] = "New Action";
	activity_log_type[9] = "New Project";
	activity_log_type[10] = "New Company";
	activity_log_type[11] = "New Employee";


	service_act_log_type[1] = "New Service";
	service_act_log_type[2] = "Service Note";
	service_act_log_type[3] = "Service Action";
	service_act_log_type[4] = "Service Comment";
	service_act_log_type[5] = "Service Comment Reply";

	$.extend( $.ui.menu.prototype, {
		next: function() {
			this.move("next", ".ui-menu-item:first");
		},
	
		previous: function() {
			this.move("prev", ".ui-menu-item:last");
		},
	
		move: function(direction, edge) {
			if (!this.active) {
				this.activate(this.element.children(edge));
				return;
			}
			var next = this.active[direction + "All"]('.ui-menu-item').eq( 0 );
			if (next.length) {
				this.activate(next);
			} else {
				this.activate(this.element.children(edge));
			}
		}
	});
	
	$.widget("custom.catcomplete", $.ui.autocomplete, {
		_renderMenu: function( ul, items ) {
			var self = this,
				currentCategory = "";
			$.each( items, function( index, item ) {
				if ( item.category != currentCategory ) {
					ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
					currentCategory = item.category;
				}
				self._renderItem( ul, item );
			});
		}
	});

	loading_image = '<div>Please wait<br /><img src="'+ $('#baseurl').val() +'resources/images/ajax-loader.gif" title="Loader" alt="Loader" style="margin-bottom:15px;" /></div>';
	
	get_required_data();
	
});

function messagebox(element, message){  
    
	$(element).html(message);
	$(element).fadeTo("slow", 1).animate({display: "none"}, 3000).fadeTo("slow", 0);  
}  

function set_message_cookie()
{
	
	$.cookie('system_message_show', 0, { expires: 0.083 });
	
}


function get_companies()
{
	
	var formdata = $("#add_new_company_form").serialize();
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		data: formdata,
		success : function(data){
			
			var boxname = "contact_company";
			//var boxname2 = "project_company";
			var box = document.getElementById(boxname);	
			var box2 = document.getElementById(boxname);	
			emptyList(box); //in the general.js file
			//emptyList(box2);
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id'] == document.getElementById("comp_id").value){
					
					option = new Option( data.data[i]['comp_name'], data.data[i]['comp_id'],selected='selected' );
					
				}else{
					
					option = new Option( data.data[i]['comp_name'], data.data[i]['comp_id']);
				}
				// Add to the end of the existing options
		
				box.options[box.length] = option;
				//box2.options[box2.length] = option;
			}
			
		}
	});		
	
}

function get_companies_contact()
{
	
	var formdata = $("#add_new_company_form").serialize();
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		data: formdata,
		success : function(data){
			
			var boxname = "contact_company";
			//var boxname2 = "project_company";
			var box = document.getElementById(boxname);	
			var box2 = document.getElementById(boxname);	
			emptyList(box); //in the general.js file
			//emptyList(box2);
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(document.getElementById("comp_id").value !=""){
					if(data.data[i]['comp_id'] == document.getElementById("comp_id").value){
						
						option = new Option( data.data[i]['comp_name'], data.data[i]['comp_id'],selected='selected' );
						
					}else{
						
						option = new Option( data.data[i]['comp_name'], data.data[i]['comp_id']);
					}
				}else{
					
					option = new Option( data.data[i]['comp_name'], data.data[i]['comp_id']);
					
				}
				// Add to the end of the existing options
		
				box.options[box.length] = option;
				//box2.options[box2.length] = option;
			}

			if(document.getElementById("comp_id").value !=""){
				get_locations(document.getElementById("comp_id").value);
			}else{
				get_locations(-1);
			}
			
		}
	});		
	
}


function get_locations(location)
{
	
	if(location == -1){ var location = $("#contact_company").val(); }
	
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_address/',
		dataType : 'json',
		data: "comp_id=" + location,
		success : function(data){
			
			var boxname = "contact_location";
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['cl_location'], data.data[i]['cl_id'] );
		
				// Add to the end of the existing options
		
				box.options[box.length] = option;
			}


		}
	});		
	
}

function add_new_company_display()
{
	get_company_users();
	$('body').append($('#add_new_company'));
	$('#add_new_company').jqmShow();
	
	if(company_popup_constructed == 0)
	{
		
		//hide the all of the element with class msg_body
		$(".content_company_address_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_company_address_holder").click(function()
		{
			$(".content_company_phone_table").hide(500);
			$(".content_company_comment_table").hide(500);
			$(this).next(".content_company_address_table").slideToggle(500);

		});
	
		$(".content_company_phone_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_comapny_phone_holder").click(function()
		{
			$(".content_company_address_table").hide(500);
			$(".content_company_comment_table").hide(500);
			$(this).next(".content_company_phone_table").slideToggle(500);
		});

		$(".content_company_comment_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_comapny_comment_holder").click(function()
		{
			$(".content_company_phone_table").hide(500);
			$(".content_company_address_table").hide(500);
			$(this).next(".content_company_comment_table").slideToggle(500);
		});

	}
	
	//stops the onclick functions being reapplied if they have alreay been applied
	company_popup_constructed = 1;
	
}

function add_new_company_send()
{

	if($("#add_new_company_form").valid())
	{

		var formdata = $("#add_new_company_form").serialize();

		$.ajax({
			type : 'POST',
			url : $('#baseurl').val() + 'contact/add_new_company/',
			dataType : 'json',
			data: formdata,
			success : function(data){
				
				if(data.error == 0) //no error detected, must now process the entry
				{
					
					var boxname = "contact_company";
					var box = document.getElementById(boxname);			
					emptyList(box); //in the general.js file
		
					for ( i = 0; i < data.company_array.data.length; i++ ) {
				
						// Create a new drop down option with the
						// display text and value from arr
						
							
							option = new Option( data.company_array.data[i]['comp_name'], data.company_array.data[i]['comp_id']);
							
				
						// Add to the end of the existing options
				
						box.options[box.length] = option;
						
						if(box.options[i].value == data.comp_id){ box.options[i].selected = true; }
						
						if(project_view == 1)
						{
						
							$('#add_new_project').jqmShow();
							
						}
						
						project_view = 0;
						
						$('#add_new_company_form')[0].reset();
						
					}	
					
					$('#add_new_company').jqmHide();
					
					if(currently_loaded == 2){ view_contact(); }else{ view_companies(); }
					get_companies_project("project_company");
					
				}
				else //error message to be show to the user
				{
					
					jqDialog.alert(data.errormsg);	
					
				}
				
			}
		});
	}
}


function add_new_contact_display()
{
	
	$('body').append($('#add_new_contact'));
	
	get_companies_contact();
	//get_locations(-1)
	
	$('#add_new_contact').jqmShow();
	
	if(contact_popup_constructed == 0)
	{

		var data = [
			{ label: "Developer", category: "General" },
			{ label: "Designer", category: "General" },
			{ label: "Project Manager", category: "Managment" },
			{ label: "Managing Director", category: "Managment" },
			{ label: "Manager", category: "Managment" },
			{ label: "Lead Developer", category: "Managment" },
			{ label: "Lead Designer", category: "Managment" },
			{ label: "Business Developement Manager", category: "Managment" }
		];
		
		$('#contact_position').catcomplete({
			delay: 200,
			source: data
		});

		//hide the all of the element with class msg_body
		$(".content_contact_address_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_contact_address_holder").click(function()
		{
			$(".content_contact_phone_table").hide(500);
			$(this).next(".content_contact_address_table").slideToggle(500);
		});
	
		$(".content_contact_phone_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_contact_phone_holder").click(function()
		{
			$(".content_contact_address_table").hide(500);
			$(this).next(".content_contact_phone_table").slideToggle(500);
		});
		
	}
	
	//stops the onclick functions being reapplied if they have alreay been applied
	contact_popup_constructed = 1;

}

function add_new_contact_send()
{
	
	if($("#add_new_contact_form").valid())
	{

		var formdata = $("#add_new_contact_form").serialize();

		$.ajax({
			type : 'POST',
			url : $('#baseurl').val() + 'contact/add_new_contact/',
			dataType : 'json',
			data: formdata,
			success : function(data){
				
				if(data.error == 0) //no error detected, must now process the entry
				{
											
					$('#add_new_contact').jqmHide();
					
					if(currently_loaded == 2){ view_contact(); }else if(currently_loaded == 1){ view_companies(); }
					
					$('#add_new_contact_form')[0].reset();
					
					if(contact_reload_page == 1)
					{
						
						window.location.reload();	
						
					}
					
				window.location.reload();
				}
				else //error message to be show to the user
				{

					jqDialog.alert(data.errormsg);
					
					return false;

				}
				
			}
		});
	}
	//alert("hi");
}

function get_companies_project()
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			
			var boxname = "project_company";
			var box = document.getElementById(boxname);	
			alert(box.value);
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});			
}


function get_users()
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_json/',
		dataType : 'json',
		success : function(data){
			
			var boxname = "project_owner";
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});		
	
}

function add_new_project_send()
{
	
	var formdata = $("#add_new_project_form").serialize();
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/add_new_process/',
		dataType : 'json',
		data: formdata,
		success : function(data){

			if(data.error == 0)
			{
				$("#add_new_project_form")[0].reset;
				$('#add_new_project').jqmHide();
				get_current_projects();
			}
			else
			{
				
				alert(data.message);	
				
			}
		}
	});		
	get_company_projects();
	get_current_projects_company_specific();
}

function add_new_project_note_send()
{
	
	var formdata = $("#add_new_project_note_form").serialize();
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/add_new_project_note/',
		dataType : 'json',
		data: formdata,
		success : function(data){

			if(data.error == 0)
			{
				alert("Note Inserted");
			}
		}
	});		
	
}


function add_new_project()
{

	get_types();
	get_companies_project("project_company");
	//get_users();
	//project_select_popup();
	$('#add_new_project').jqmShow();
	
}

function new_invoice_popup()
{
	
	get_companies_project("company_id_dropdown");
	get_current_projects("project_id_dropdown");
	get_assigned_services("service_dropdown_assigned");
	$('#add_new_invoice_model').jqmShow();	
		
}


function serv_invoice_popup()
{
	get_current_services("service_id_dropdown");
	$('#add_serv_invoice_model').jqmShow();
		
}

function get_current_services(dropdown)
{
	var service_id = $('#s_id').val();

	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'services/get_current_services/',
		dataType : 'json',
		success : function(data){
			
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "service_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['s_id'] == service_id){
					
					option = new Option(data.data[i]['s_name'], data.data[i]['s_id'],selected='selected');
					
				}else{
					
					option = new Option(data.data[i]['s_name'], data.data[i]['s_id']);
					
				}
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});			
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}



function get_assigned_services(dropdown)
{

	var formdata = "company_id=" + $('#comp_id').val()+"&project_id="+$('#prj_id').val();

	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'services/get_assigned_services/',
		dataType : 'json',
		data: formdata,
		success : function(data){
		
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "service_dropdown_assigned")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
				// Create a new drop down option with the
				// display text and value from arr
					
					option = new Option(data.data[i].s_name, data.data[i].s_id);
					
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});		
	
}

function get_current_projects(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_current_projects/',
		dataType : 'json',
		success : function(data){
			
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "project_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				option = new Option(data.data[i]['prj_name'] + " - " + data.data[i]['comp_name'] , data.data[i]['prj_id']);
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});			
}

function get_current_projects_sel(dropdown,id)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_current_projects/',
		dataType : 'json',
		success : function(data){
			
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "project_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['prj_id'] == id){
					
					option = new Option(data.data[i]['prj_name'] + " - " + data.data[i]['comp_name'] , data.data[i]['prj_id'],selected='selected');
					
				}else{
					
					option = new Option(data.data[i]['prj_name'] + " - " + data.data[i]['comp_name'] , data.data[i]['prj_id']);
				}
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});			
}

function get_companies_project(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "company_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==document.getElementById('company_check').value)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}

			get_company_owner();
		}
	});			
}


function project_select_popup()
{

	get_types();
	get_companies_project("project_company");
	get_users();
	get_current_projects("project_id");
	$('#select_project_modal').jqmShow();	
	
}

function get_users()
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_json/',
		dataType : 'json',
		success : function(data){
			
			var boxname = "project_owner";
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});		
	
}

function get_types()
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_type/',
		dataType : 'json',
		success : function(data){
			
			var boxname = "project_type";
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['pt_desc'], data.data[i]['pt_id'] );
		
				// Add to the end of the existing options
		
				box.options[box.length] = option;
			}


		}
	});		
	
}

function get_company_owner()
{
	var compid = document.getElementById('project_company').value;
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_comp/'+compid,
		dataType : 'json',
		success : function(data){

			var boxname = "project_owner";
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
				if(box.options[i].value == data.owner){ box.options[i].selected = true; }
				
			}


		}
	});		
	
}

function get_company_users()
{

	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_json/',
		dataType : 'json',
		success : function(data){
			
			var boxname = "company_owner";
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});		
	
}

function proj_filter_owner()
{

	var formdata = $("#proj_owner_form").serialize();
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_projects_owner_specific/',
		dataType : 'json',
		data: formdata,
		success : function(data){
			
			project_view_constructor(data);


		}
	});			
}

function send_prj_status($status)
{
	document.getElementById('proj_status').value = $status;
	document.getElementById('proj_owner').value = 0;
	
}

function take_payment()
{
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'user/get_payment_type/',
		dataType : 'json',
		success : function(data){
			
			if(data.error == 1) //no error detected, must now process the entry
			{
				get_companies_project("company_id_payment_dropdown");
				get_current_projects("project_id_payment_dropdown");
				$('#payment_invoice_model').jqmShow();	
				
			}else{				
				alert("Please select a payment method in account settings.");			
			}
		}
	});
	
}

function new_project_payment()
{
	get_types_project();
	get_companies_project1("project_company1");
	//get_users();
	//project_select_popup();
	$('#add_new_project').jqmShow();
	
}

function get_types_project()
{

	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_type/',
		dataType : 'json',
		success : function(data){
		
			var	boxname = "project_type1";
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
		
				option = new Option( data.data[i]['pt_desc'], data.data[i]['pt_id'] );
		
				// Add to the end of the existing options
		
				box.options[box.length] = option;
			}

		
		}
	});		
	
}

function get_company_owner1()
{
	
	var compid = document.getElementById('project_company1').value;
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_comp/'+compid,
		dataType : 'json',
		success : function(data){
		
			var boxname = "project_owner1";
		
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
				if(box.options[i].value == data.owner){ box.options[i].selected = true; }
				
			}


		}
	});		
	
}

function get_companies_project1(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "company_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==document.getElementById('company_check').value)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}
			
			get_company_owner1();
			
		}
	});			
}

function add_new_company_send1()
{

	if($("#add_new_company_form").valid())
	{

		var formdata = $("#add_new_company_form").serialize();

		$.ajax({
			type : 'POST',
			url : $('#baseurl').val() + 'contact/add_new_company/',
			dataType : 'json',
			data: formdata,
			success : function(data){
				
				if(data.error == 0) //no error detected, must now process the entry
				{
					
					var boxname= "company_id_payment_dropdown";
						
					var box = document.getElementById(boxname);			
					emptyList(box); //in the general.js file
		
					for ( i = 0; i < data.company_array.data.length; i++ ) {
				
						// Create a new drop down option with the
						// display text and value from arr
						
							
							option = new Option( data.company_array.data[i]['comp_name'], data.company_array.data[i]['comp_id']);
							
				
						// Add to the end of the existing options
				
						box.options[box.length] = option;
						
						if(box.options[i].value == data.comp_id){ box.options[i].selected = true; }
						
						
						if(payment_view == 1)
						{
							$('#payment_invoice_model').jqmShow();
							
						}else{
						
							$('#add_new_project').jqmShow();
							
						}
												
						project_view = 0;
						payment_view = 0;
						
						$('#add_new_company_form')[0].reset();
						
					}	
					
					
					$('#add_new_company').jqmHide();
					
					if(currently_loaded == 2){ view_contact(); }else{ view_companies(); }
					
						
						get_companies_project1("company_id_payment_dropdown");
											
					
				}
				else //error message to be show to the user
				{
					
					jqDialog.alert(data.errormsg);	
					
				}
				
			}
		});
	}
}


function payment_add_company(type)
{

	if($("#add_new_company_form").valid())
	{

		var formdata = $("#add_new_company_form").serialize();

		$.ajax({
			type : 'POST',
			url : $('#baseurl').val() + 'contact/add_new_company/',
			dataType : 'json',
			data: formdata,
			success : function(data){
				
				if(data.error == 0) //no error detected, must now process the entry
				{
					
					var boxname= "project_company1";
						
					var box = document.getElementById(boxname);			
					emptyList(box); //in the general.js file
		
					for ( i = 0; i < data.company_array.data.length; i++ ) {
				
						// Create a new drop down option with the
						// display text and value from arr
						
							
							option = new Option( data.company_array.data[i]['comp_name'], data.company_array.data[i]['comp_id']);
							
				
						// Add to the end of the existing options
				
						box.options[box.length] = option;
						
						if(box.options[i].value == data.comp_id){ box.options[i].selected = true; }
						
					
						if(type == 1)
						{
							$('#payment_invoice_model').jqmShow();
							
						}else if(type == 2){
						
							$('#add_new_project').jqmShow();
							
						}
												
						project_view = 0;
						payment_view = 0;
						
						$('#add_new_company_form')[0].reset();
						
					}	
					
					
					$('#add_new_company').jqmHide();
					
					
						
						//get_companies_project1("project_company1");
						get_project_owner('project_company1',data.comp_id);
											
					
				}
				else //error message to be show to the user
				{
					
					jqDialog.alert(data.errormsg);	
					
				}
				
			}
		});
	}
}


function get_project_owner(dropdown,comp_id)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "project_company1")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==comp_id)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}
			
			get_company_owner1();
			
		}
	});			
}

function payment_project_add()
{
	var formdata = $("#add_new_project_form1").serialize();

	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/add_new_process/',
		dataType : 'json',
		data: formdata,
		success : function(data){

			if(data.error == 0)
			{
				$("#add_new_project_form1")[0].reset;
				$('#add_new_project').jqmHide();
				get_project_added('project_id_payment_dropdown',data.project_id);
				$('#payment_invoice_model').jqmShow();
			}
			else
			{
				
				alert(data.message);	
				
			}
		}
	});		

}

function get_project_added(dropdown,project_id)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_current_projects/',
		dataType : 'json',
		success : function(data){
			
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "project_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				option = new Option(data.data[i]['prj_name'] + " - " + data.data[i]['comp_name'] , data.data[i]['prj_id']);
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				if(box.options[i].value == project_id){ box.options[i].selected = true; }
				
			}


		}
	});			
}

function add_company_payment()
{

	get_company_users();
	$('body').append($('#add_new_companyy'));
	$('#add_new_companyy').jqmShow();
	
	if(company_popup_constructed == 0)
	{
		
		//hide the all of the element with class msg_body
		$(".content_company_address_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_company_address_holder").click(function()
		{
			$(".content_company_phone_table").hide(500);
			$(".content_company_comment_table").hide(500);
			$(this).next(".content_company_address_table").slideToggle(500);

		});
	
		$(".content_company_phone_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_comapny_phone_holder").click(function()
		{
			$(".content_company_address_table").hide(500);
			$(".content_company_comment_table").hide(500);
			$(this).next(".content_company_phone_table").slideToggle(500);
		});

		$(".content_company_comment_table").hide();
		//toggle the componenet with class msg_body
		$(".icon_comapny_comment_holder").click(function()
		{
			$(".content_company_phone_table").hide(500);
			$(".content_company_address_table").hide(500);
			$(this).next(".content_company_comment_table").slideToggle(500);
		});

	}
	
	//stops the onclick functions being reapplied if they have alreay been applied
	company_popup_constructed = 1;
	
}

function add_new_service()
{
	get_companies_services("service_company");

	$('#add_new_service').jqmShow();
	
}


function get_companies_services (dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "company_id_dropdown")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==document.getElementById('company_check').value)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}

			get_company_owner_service("service_owner");
		}
	});			
}


function get_company_owner_service(dropdown)
{

	var compid = document.getElementById('service_company').value;
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_useraccount_comp/'+compid,
		dataType : 'json',
		success : function(data){

			var boxname = dropdown;
			var box = document.getElementById(boxname);		
			emptyList(box); //in the general.js file

			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				
				option = new Option( data.data[i]['au_fname'] + " " + data.data[i]['au_lname'], data.data[i]['au_id'] );
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
				if(box.options[i].value == data.owner){ box.options[i].selected = true; }
				
			}


		}
	});		
	
}

function add_new_service_send()
{

	var formdata = $("#add_new_service_form").serialize();
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'services/add_new_service/',
		dataType : 'json',
		data: formdata,
		success : function(data){

			if(data.error == 0)
			{
				$("#add_new_service_form")[0].reset;
				$('#add_new_service').jqmHide();
				get_current_projects();
				window.location.reload();
			}
			else
			{
				
				alert(data.message);	
				
			}
		}
	});
	
}

function new_purchase_popup()
{
	get_companies_purchase("company_id_dropdown2");
	get_companies_supplier("company_id_dropdown3");
	get_projects_purchase("project_id_dropdown2");
	$('#add_new_purchase_model').jqmShow();
		
}

function get_projects_purchase(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'project/get_current_projects/',
		dataType : 'json',
		success : function(data){
			
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "project_id_dropdown2")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				option = new Option(data.data[i]['prj_name'] + " - " + data.data[i]['comp_name'] , data.data[i]['prj_id']);
		
				// Add to the end of the existing options
				box.options[box.length] = option;
				
			}


		}
	});			
}

function get_companies_purchase(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "company_id_dropdown2")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==document.getElementById('company_check').value)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}

			get_company_owner();
		}
	});			
}


function get_companies_supplier(dropdown)
{
	
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/get_company_json/',
		dataType : 'json',
		success : function(data){
			var boxname = dropdown;
			var box = document.getElementById(boxname);			
			emptyList(box); //in the general.js file
			
			if(dropdown == "company_id_dropdown3")
			{ 
			
				option = new Option("None" , 0);
				box.options[0] = option;
			
			}			
			
			for ( i = 0; i < data.data.length; i++ ) {
		
				// Create a new drop down option with the
				// display text and value from arr
				if(data.data[i]['comp_id']==document.getElementById('company_check').value)
					{
					option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id'], selected="selected");
			box.options[box.length] = option;
					}
					else
					{
						option = new Option(data.data[i]['comp_name'], data.data[i]['comp_id']);
			box.options[box.length] = option;
					}
		
				// Add to the end of the existing options
				
				
			}

			get_company_owner();
		}
	});			
}

function del_person(emp_id)
{

	var formdata = "empid="+emp_id;
	$.ajax({
		type : 'POST',
		url : $('#baseurl').val() + 'contact/delete_person/',
		dataType : 'json',
		data: formdata,
		success : function(data){

			if(data.error == 0)
			{
				window.location.reload();
			}
			else
			{
				
				alert('Employee details could not be deleted');	
				
			}
		}
	});
	
}

function filter_projects(){
	$("#filter_projects_name").keyup(function(event){
		if(event.keyCode == 13){
			filter_current_projects_byname();
		}
	});
}

function reset_project_filter(){
	$("#filter_projects_name").val("Search projects...");
	get_current_projects();
}
