//version 1.1 MOD to not erase window contents
var popup_array = Array();
var all_view_status = false;

function open_popup(url, popup_name, options) {

	var cur_popup = popup_array[popup_name];
	var is_popup_defined = (typeof(cur_popup) == 'object');
	var is_popup_open = (is_popup_defined)
		? !(cur_popup.closed)
		: false;
			

	
	if ( is_popup_defined && is_popup_open ) {
		cur_popup.document.write (' ');
            cur_popup.focus();
		cur_popup.location.href = url;
            cur_popup.focus();
		autoStartTimer( the_task );
	} else if ((is_popup_defined && !is_popup_open) || !is_popup_defined) {
		popup_array[popup_name] = window.open(url, popup_name, options);
		popup_array[popup_name].focus();
	}
}

function open_full_popup(url)
{
	window.open(url, '', 'scrollbars=yes, resizable=yes, status=yes, toolbar=yes, location=yes, menubar=yes');
}

function openTimerWindow(the_task)
{
	open_popup('/hub/includes/timer.php?id=' + the_task, 'task_timer_window', 'width=575,height=600,scrollbars=yes,resizable=yes,location=no,menubar=no,status=no,toolbar=no');

}

var all_clicked = '';
function viewAllTimers()
{
	if(confirm('Remember to save your time!\n"OK" to continue without saving.\n"Cancel" to return.'))
	{
		all_clicked = 1;
		resizeTo(600, 400);
		window.location='/hub/includes/timer.php?view_full=1';
	}
}



function confirmUnLoad()
{
	if(all_clicked != 1)
	{
		if(event)
		{
			event.returnValue = ' ';
		}
	}
}

function validate_contact(the_form) {
	var why = '';
	if (the_form.realname.value == '') {
		why += 'Please enter your name.\n';
	}
	var email_reg = /[_a-zA-Z0-9.-]+@[_a-zA-Z0-9.-]+[.][_a-zA-Z0-9.-][_a-zA-Z0-9.-]+/;
	var res = email_reg.test(the_form.email.value);
	if (!res) {
		why += 'Please enter a valid email address.\n';
	}
	if (why != '') {
		alert(why);
		return false;
	} else {
		return true;
	}
}

function validate_service_req(the_form) {
	var why = '';
	if (the_form.project.value == '') {
		why += 'Please enter a project title.\n';
	}
	if (the_form.details.value == '') {
		why += 'Please enter the project details.\n';
	}
	if (why != '') {
		alert(why);
		return false;
	} else {
		return true;
	}
}

function validate_client(the_form) {
	var why = '';
	if (the_form.proj_prefix.value == '') {
		why += 'Please enter a project prefix.\n';
	}
	if (the_form.short_name.value == '') {
		why += 'Please enter a short name.\n';
	}
	if (the_form.first.value == '') {
		why += 'Please enter a client name.\n';
	}
	if (the_form.username.value == '') {
		why += 'Please enter a username.\n';
	}
	if (the_form.password.value == '') {
		why += 'Please enter a password.\n';
	}
	if (why != '') {
		alert(why);
		return false;
	} else {
		return true;
	}
}

function validate_support(the_form) {
	var why = '';
	if (the_form.from.value == '') {
		why += 'Please enter your name.\n';
	}
	var email_reg = /[_a-zA-Z0-9.-]+@[_a-zA-Z0-9.-]+[.][_a-zA-Z0-9.-][_a-zA-Z0-9.-]+/;
	var res = email_reg.test(the_form.email.value);
	if (!res) {
		why += 'Please enter a valid email address.\n';
	}
	if (the_form.message.value == '') {
		why += 'Please enter a message.\n';
	}
	if (why != '') {
		alert(why);
		return false;
	} else {
		return true;
	}
}

function getFieldValue (strFieldName) {
	var strFieldValue;
	var objRegExp = new RegExp(strFieldName + "=([^&]+)","gi");

	if (objRegExp.test(location.search))
		strFieldValue = unescape(RegExp.$1);
		else strFieldValue="";

	return strFieldValue;
}






function task_form_select_status_update(){

	var options = document.getElementById('note_type').options;
	var status_update_select_index = 0;
	for(i=0;i<options.length;i++){
		if(options[i].value == 'Status Update'){
		   status_update_select_index = i;
		}
	}

	document.getElementById('note_type').selectedIndex = status_update_select_index;

}

function task_form_select_client_communication(){

	var options = document.getElementById('note_type').options;
	var status_update_select_index = 0;
	for(i=0;i<options.length;i++){
		if(options[i].value == 'Client Communication'){
		   status_update_select_index = i;
		}
	}

	document.getElementById('note_type').selectedIndex = status_update_select_index;

}


function validate_task_update(){
	
	// Get the task note form
	var tasknote_form = document.getElementById('project_form');

	/* Find out if there is something in any of the file upload fields @TODO: check the 'object' conditional in all browsers */
	var upload_field_count = 1; // How many upload fields are there

	if(document.project_form.fileCount)
	{
		upload_field_count = document.project_form.fileCount.value;
	}
	
	var upload_field_has_something = false;
	var file_value = '';
	var submit_form = true;

	for(i=1; i <= upload_field_count; i++){ // For each of the upload fields

		file_value = document.getElementsByName('userfile' + i)[0].value;

		// It has something in it or we haven't already detected that it has something in it
		if(upload_field_has_something == false && typeof(file_value) == 'string' && file_value.length > 0){
			upload_field_has_something = true;
		}
	}

	/* Find out if we are automatically appending to a note */
	var note_body_has_notification =  (document.getElementById('dev_status_changed').value == 'yes') || (document.getElementById('qcgl_proc_changed').value == 'yes');
	

	
	/* Find out if the note type is filled out */
	var note_type_has_something =  (document.getElementById('note_type').value !== '');
	/* Find out if a note has been entered (note body */
	var note_body_has_something =  (document.getElementById('culprit').value !== '');

	/* Find out if there a "SEND EMAIL TO:" is selected */
	var inputs = document.getElementById('send_email_to').getElementsByTagName('input');

	var a_user_selected = false;

	for(i=0;i <inputs.length;i++){ 
		theCheckbox = inputs[i];
		if(theCheckbox.type == 'checkbox'){
			if(theCheckbox.checked){
					a_user_selected = true;
				}
		}
	}

	if(document.getElementsByName('email_to_additional')[0].value !== ''){
		a_user_selected = true;
	}
	if(document.getElementsByName('email_to_additional2')[0].value !== ''){
		a_user_selected = true;
	}

	/*
	console.log('Test basic singlular simplified conditionals:');
	console.log('upload_field_has_something:', upload_field_has_something);
	console.log('note_type_has_something:', note_type_has_something);
	console.log('note_body_has_something:', note_body_has_something);
	console.log('a_user_selected:', a_user_selected);
	*/

	/*
	 * 
	 * !IMPORTANT!
	 *
	 * List of situations here:
	 * 
	 * https://spreadsheets.google.com/a/trinetsolutions.com/ccc?key=0AqeHh6PfZYSzdEdMUkpWQ0xSUzFqTFJRNFEyVHFlVkE&hl=en
	 *
	 */

	submit_form = true;

	/*if(!a_user_selected){
		if(!confirm('Note: You have not selected anyone to send email to.')){
			return false;
		} 
	}*/
	
	// Situation #1
	if(!note_body_has_something && !note_type_has_something && a_user_selected && !upload_field_has_something && !note_body_has_notification){
		if(confirm('Since you selected to email one or more people but didn\'t enter a note and didn\'t select a note type, would you like \'Status Update\' to be automatically selected for the type and \'See previous note(s)\' to be entered as the note message?')){
			task_form_select_status_update();
			document.getElementById('culprit').value = 'See previous note(s)';
			submit_form = true;
		}
		else{
			submit_form = false;
		} 
	}



	// Situation #2
	if(note_body_has_something && !note_type_has_something && !a_user_selected && upload_field_has_something && !note_body_has_notification){
		if(!a_user_selected){
			if(!confirm('Note: You have not selected anyone to send email to. Press OK to continue.')){
				return false;
			} 
		}
		
		if((document.URL).indexOf("view_projects.html") != "-1") {
			if(confirm('You have entered a note without a note type, would you like "Client Communication" to be selected?')){
				task_form_select_client_communication();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		} else  {
			if(confirm('You have entered a note without a note type, would you like "status update" to be selected?')){
				task_form_select_status_update();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		}
	}

	// Situation #3
	if(note_body_has_something && !note_type_has_something && a_user_selected && !upload_field_has_something && !note_body_has_notification){
		if((document.URL).indexOf("view_projects.html") != "-1") {
			if(confirm('You have entered a note without a note type, would you like "Client Communication" to be selected?')){
				task_form_select_client_communication();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		} else  {
			if(confirm('You have entered a note without a note type, would you like "status update" to be selected?')){
				task_form_select_status_update();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		}
	}

	// Situation #4
	if(note_body_has_something && !note_type_has_something && a_user_selected && upload_field_has_something && !note_body_has_notification){
		
		if((document.URL).indexOf("view_projects.html") != "-1") {
			if(confirm('You have entered a note without a note type, would you like "Client Communication" to be selected?')){
				task_form_select_client_communication();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		} else  {
			if(confirm('You have entered a note without a note type, would you like "status update" to be selected?')){
				task_form_select_status_update();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		}
	}

	// Situation #5
	if(!note_body_has_something && note_type_has_something && a_user_selected && upload_field_has_something && !note_body_has_notification){

		if(confirm('Would you like to insert "see attached file(s)" to the task note?')){
			document.getElementById('culprit').value = 'See attached file(s)';
			submit_form = true;
		}
		else{
			submit_form = false;
		}
	}

	// Situation #6
	if(!note_body_has_something && note_type_has_something && !a_user_selected && upload_field_has_something && !note_body_has_notification){

		if(!a_user_selected){
			if(!confirm('Note: You have not selected anyone to send email to. Press OK to continue.')){
				return false;
			} 
		}
		if(confirm('Would you like to insert "see attached file(s)" to the task note?')){
			document.getElementById('culprit').value = 'See attached file(s)';
			submit_form = true;
		}
		else{
			submit_form = false;
		}
	}

	// Situation #7
	if(!note_body_has_something && !note_type_has_something && a_user_selected && upload_field_has_something && !note_body_has_notification){

		if(confirm('You have entered a note without a note type, would you like "status update" to be selected and enter a note that says "see attached file(s)"')){
			task_form_select_status_update();
			document.getElementById('culprit').value = 'See attached file(s)';
			submit_form = true;
		}
		else{
			submit_form = false;
		}
	}

	// Situation #8

	// Situation #9
	if(!note_body_has_something && note_type_has_something && a_user_selected && !upload_field_has_something && !note_body_has_notification){

		if(confirm('Would you like "See previous note(s)" to be entered as the note message? (you have entered a note type and selected one or more users but not entered a task note)')){
			task_form_select_status_update();
			document.getElementById('culprit').value = 'See previous note(s)';
			submit_form = true;
		}
		else{
			submit_form = false;
		}
	}
 

	// Situation #10
	if(!note_body_has_something && note_type_has_something && !a_user_selected && !upload_field_has_something && !note_body_has_notification){

		if(!a_user_selected){
			if(!confirm('Note: You have not selected anyone to send email to. Press OK to continue.')){
				return false;
			} 
		}
			alert('You have selected a note type but with no note or file, please enter a note or attach a file.');
			submit_form = false;
	}

	// Situation #11 - 14

	// Situation #15
	if(note_body_has_something && !note_type_has_something && !a_user_selected && !upload_field_has_something && !note_body_has_notification){

		if(!a_user_selected){
			if(!confirm('Note: You have not selected anyone to send email to. Press OK to continue.')){
				return false;
			} 
		}
		

		
		if((document.URL).indexOf("view_projects.html") != "-1") {
			if(confirm('You have entered a note without a note type, would you like "Client Communication" to be selected?')){
				task_form_select_client_communication();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		} else  {
			if(confirm('You have entered a note without a note type, would you like "status update" to be selected?')){
				task_form_select_status_update();
				submit_form = true;
			}
			else{
				submit_form = false;
			}
		}

	}
	
	// Situation #16
	if (note_body_has_notification) {

		if (document.getElementById('dev_status_changed').value == 'yes') {
		var selectedDevStatus = document.getElementById("dev_status");
		var dev_status = selectedDevStatus.options[selectedDevStatus.selectedIndex].value;
			document.getElementById('culprit').value = 'Automated Message:\nThe dev status was changed to "'+ document.getElementById('dev_status_' + dev_status).value + '"\n-------------------------------------\n' + document.getElementById('culprit').value ;
		}
		if (document.getElementById('qcgl_proc_changed').value == 'yes') {
			var process_for_qc = document.getElementById("process_for_qc_val").value;
			var process_for_going_live = document.getElementById("process_for_going_live_val").value;
			document.getElementById('culprit').value = 'Automated Message:\nThe QC/Go-Live procedure  has been updated\nThe QC Procedure is now: "'+ document.getElementById('process_for_qc_' + process_for_qc).value + '"\n The Go Live procedure is now: "' + document.getElementById('process_for_going_live_' + process_for_going_live).value + '\n-------------------------------------\n' + document.getElementById('culprit').value ;
		}
		
		submit_form = true;
		
		if (!note_type_has_something) {
			task_form_select_status_update();
		}
	}

	if(submit_form == true)
	{
		tasknote_form.submit();
	} 
	
	return false;
}


function toggleDisplayBlock(element){

	if(element.style.display== 'block' || element.style.display== ''){
		element.style.display= 'none';
	}
	else{
		element.style.display= '';
	}

}

