
var save_counter = 0;

function load_function(page){
	if(document.forms[0] != null && document.forms[0].elements[0] != null){
		//document.forms[0].elements[0].focus();
		if(document.forms[0].elements[0].name != "department_filter" && document.forms[0].elements[0].name != "user_filter"){ document.forms[0].elements[0].focus(); }
	}
	
	if(page != null){
		if(page == "report.php"){
			document.getElementById("table_loading").style.display = "none";
			document.getElementById("table_report").style.display = "";
		
		} else if(page == "training.php"){
			load_training();
		}
	}
}

function toggle_display(elem){
	document.getElementById(elem).style.display = (document.getElementById(elem).style.display=="none") ? "" : "none";
}

function check_save_counter(){
	save_counter++;
	if(save_counter == 2){
		alert("NOTE: You can only save one item at a time.");
	}
}

function toggle_writeup(elem){
	toggle_display("writeup_"+elem+"_short");
	toggle_display("writeup_"+elem+"_full");
}

function delete_project(){
	var x = confirm("Are you sure you want to delete this promotion?");
	if(x){
		document.theform.save_action.value = "delete_project";
		document.theform.submit();
	}
}

function download_item(type,id,item){
	hidden_frame.location.href = "/authorized/lookup/download.php?type="+type+"&id="+id+"&item="+item;
}

function delete_item(item){
	var x = confirm("Are you sure you want to delete this item?");
	if(x){
		document.theform.save_action.value = "delete_item";
		document.theform.delete_id.value = item;
		document.theform.submit();
	}
}

function delete_form_item(item,form_id){
	var x = confirm("Are you sure you want to delete this item?");
	if(x){
		document.location.href = "forms.php?form_id="+form_id+"&delete_item="+item;
	}
}

function remove_file(id){
	document.getElementById("edit_file_stored_title_"+id).style.display = "none";
	document.getElementById("edit_file_delete_link_"+id).style.display = "none";
	document.theform.elements["edit_file_stored_"+id].value = "";
}

// AJAX *****************************************************************************************
var http = false;
if(navigator.appName == "Microsoft Internet Explorer"){
	http = new ActiveXObject("Microsoft.XMLHTTP");
	http2 = new ActiveXObject("Microsoft.XMLHTTP");
	http3 = new ActiveXObject("Microsoft.XMLHTTP");
	http4 = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	http = new XMLHttpRequest();
	http2 = new XMLHttpRequest();
	http3 = new XMLHttpRequest();
	http4 = new XMLHttpRequest();
}

function edit_item(id){
	check_save_counter();
	if(id == null){ id = ""; }
	http.open("GET", "/authorized/lookup/edit_items.php?id="+id, true);
	http.onreadystatechange=function() {
		if(http.readyState == 4){
			document.getElementById("edit_"+id).innerHTML = http.responseText;
		}
	}
	http.send(null);
}

function home_menu(place,id,type){
	document.body.style.cursor = "wait";
	http2.open("GET", "/authorized/lookup/home_menu.php?id="+id+"&type="+type, true);
	http2.onreadystatechange=function() {
		if(http2.readyState == 4){
			document.getElementById("menu_"+place).innerHTML = http2.responseText;
			document.body.style.cursor = "default";
			document.getElementById("menu_splash").style.display = "none";
			document.getElementById("menu_"+place).style.display = "";
		}
	}
	http2.send(null);
}
function clear_home_menu(place){
	var td_list = document.getElementById("menu_"+place).getElementsByTagName("td");
	for(i=0; i<td_list.length; i++){
		td_list[i].className = "menu_off";
	}
	if(place == 1){
		document.getElementById("menu_2").style.display = "none";
		document.getElementById("menu_3").style.display = "none";
	}
	if(place == 2){
		document.getElementById("menu_3").style.display = "none";
	}
}

function show_training(path,file){
	http3.open("GET", "/authorized/lookup/training.php?path="+path+"&file="+file, true);
	http3.onreadystatechange=function() {
		if(http3.readyState == 4){
			document.getElementById("training_video").innerHTML = http3.responseText;
		}
	}
	http3.send(null);
}

function load_training(){
	http4.open("GET", "/authorized/lookup/training_menu.php", true);
	http4.onreadystatechange=function() {
		if(http4.readyState == 4){
			document.getElementById("training_menu").innerHTML = http4.responseText;
		}
	}
	http4.send(null);
}
// **********************************************************************************************

function new_value(elem){
	if(elem.options[elem.selectedIndex].value == "new_value"){
		document.theform.elements[elem.name+"_new"].style.display = "";
		document.theform.elements[elem.name+"_new"].focus();
	} else {
		document.theform.elements[elem.name+"_new"].style.display = "none";
	}
}

function save_note(){
	if(document.theform.new_note.value == ""){
		alert("The note is empty.");
	} else {
		document.theform.save_action.value = "new_note";
		document.theform.submit();
	}
}

function save_task(){
	if(document.theform.new_task.value == ""){
		alert("The task is empty.");
	} else {
		document.theform.save_action.value = "new_task";
		document.theform.submit();
	}
}

function complete_tasks(){
	document.theform.save_action.value = "complete_tasks";
	document.theform.submit();
}

function delete_note(id){
	var x = confirm("Are you sure you want to delete this note?");
	if(x){
		document.theform.save_action.value = "delete_note";
		document.theform.note_id.value = id;
		document.theform.submit();
	}
}

function validateDate(elem) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = "Please enter valid date as month, day, and year.";
    if (elem.value != "" && !elem.value.match(RegExPattern)){
        alert(errorMessage);
        elem.focus();
    }
}

/****************************************************************************************************/

function add_project(type){
	check_save_counter();
	
	var str = "";
	if(document.theform.project_name.value == ""){ str += "\n- Name"; }
	if(document.theform.project_department.value == ""){ str += "\n- Team"; }
	if(document.theform.project_industry.value == ""){ str += "\n- Industry"; }
	else if(document.theform.project_industry.value == "new_value" && document.theform.project_industry_new.value == ""){ str += "\n- New Industry"; }
	if(document.theform.project_type.value == ""){ str += "\n- Type"; }
	else if(document.theform.project_type.value == "new_value" && document.theform.project_type_new.value == ""){ str += "\n- New Type"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		if(type != null && type == "edit"){
			document.theform.save_action.value = "edit_project";
		} else {
			document.theform.save_action.value = "add_project";
		}
		document.theform.submit();
	}
}


// WRITEUPS
function new_writeup(val){
	check_save_counter();
	
	document.getElementById("new_writeup").style.display = "";
	document.theform.new_writeup_group.value = val;
	document.theform.new_writeup_name.focus();
}
function add_writeup(){
	if(document.theform.new_writeup_file.value == "" && document.theform.new_writeup_text.value == ""){
		alert("Please pick a file or add a writeup.");
	} else {
		document.theform.save_action.value = "add_writeup";
		document.theform.submit();
	}
}
function edit_writeups(id){
	if(document.theform.elements["edit_file_"+id].value == "" && document.theform.elements["edit_file_stored_"+id].value == "" && document.theform.elements["edit_text_"+id].value == ""){
		alert("Please pick a file or add a writeup.");
	} else {
		document.theform.edit_id.value = id;
		document.theform.edit_type.value = "writeups";
		document.theform.save_action.value = "edit_item";
		document.theform.submit();
	}
}


// PRINT ADS
function new_print_ad(val){
	check_save_counter();
	
	document.getElementById("new_print_ad").style.display = "";
	document.theform.new_print_ad_group.value = val;
	document.theform.new_print_ad.focus();
}
function add_print_ad(){
	var str = "";
	if(document.theform.new_print_ad.value == ""){ str += "\n- File"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_print_ad";
		document.theform.submit();
	}
}
function edit_print_ads(id){
	document.theform.edit_id.value = id;
	document.theform.edit_type.value = "print_ads";
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// RADIO SPOT SCRIPTS
function new_radio(val){
	check_save_counter();
	
	document.getElementById("new_radio").style.display = "";
	document.theform.new_radio_group.value = val;
	document.theform.new_radio.focus();
}
function add_radio(){
	var str = "";
	if(document.theform.new_radio.value == ""){ str += "\n- File"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_radio";
		document.theform.submit();
	}
}
function edit_radio_spot_scripts(id){
	document.theform.edit_id.value = id;
	document.theform.edit_type.value = "radio_spot_scripts";
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// PROMOTIONAL SUMMARIES
function new_summary(val){
	check_save_counter();
	
	document.getElementById("new_summary").style.display = "";
	document.theform.new_summary_group.value = val;
	document.theform.new_summary_name.focus();
}
function add_summary(){
	if(document.theform.new_summary_file.value == "" && document.theform.new_summary_text.value == ""){
		alert("Please pick a file or add a writeup.");
	} else {
		document.theform.save_action.value = "add_summary";
		document.theform.submit();
	}
}
function edit_summaries(id){
	if(document.theform.elements["edit_file_"+id].value == "" && document.theform.elements["edit_file_stored_"+id].value == "" && document.theform.elements["edit_text_"+id].value == ""){
		alert("Please pick a file or add a writeup.");
	} else {
		document.theform.edit_id.value = id;
		document.theform.edit_type.value = "summaries";
		document.theform.save_action.value = "edit_item";
		document.theform.submit();
	}
}


// ATTACHMENTS
function new_attachment(val){
	check_save_counter();
	
	document.getElementById("new_attachment").style.display = "";
	document.theform.new_attachment_group.value = val;
	document.theform.new_attachment.focus();
}
function add_attachment(){
	var str = "";
	if(document.theform.new_attachment.value == ""){ str += "\n- File"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_attachment";
		document.theform.submit();
	}
}
function edit_attachments(id){
	document.theform.edit_id.value = id;
	document.theform.edit_type.value = "attachments";
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// LISTS
function new_list(val){
	check_save_counter();
	
	document.getElementById("new_list").style.display = "";
	document.theform.new_list_group.value = val;
	document.theform.new_list.focus();
}
function add_list(){
	var str = "";
	if(document.theform.new_list.value == ""){ str += "\n- List Item"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_list";
		document.theform.submit();
	}
}
function edit_lists(id){
	var str = "";
	if(document.theform.elements["edit_name_"+id].value == ""){ str += "\n- List Item"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.edit_id.value = id;
		document.theform.edit_type.value = "lists";
		document.theform.save_action.value = "edit_item";
		document.theform.submit();
	}
}


// PHOTOS
function new_photo(val){
	check_save_counter();
	
	document.getElementById("new_photo").style.display = "";
	document.theform.new_photo_group.value = val;
	document.theform.new_photo.focus();
}
function add_photo(){
	var str = "";
	if(document.theform.new_photo.value == ""){ str += "\n- Photo"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_photo";
		document.theform.submit();
	}
}
function edit_photos(id){
	document.theform.edit_id.value = id;
	document.theform.edit_type.value = "photos";
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// VIDEOS
function new_video(val){
	check_save_counter();
	
	document.getElementById("new_video").style.display = "";
	document.theform.new_video_group.value = val;
	document.theform.new_video.focus();
}
function add_video(){
	var str = "";
	if(document.theform.new_video.value == ""){ str += "\n- Video"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_video";
		document.theform.submit();
	}
}
function edit_videos(id){
	document.theform.edit_id.value = id;
	document.theform.edit_type.value = "videos";
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// PLAYBALL
function new_playball(val){
	check_save_counter();
	
	document.getElementById("new_playball_"+val).style.display = "";
	document.theform.elements["new_playball_file_"+val].focus();
}
function add_playball(val){
	var str = "";
	if(document.theform.elements["new_playball_file_"+val].value == ""){ str += "\n- File"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_playball";
		document.theform.department_id.value = val;
		document.theform.submit();
	}
}
function edit_playball(id){
	document.theform.edit_id.value = id;
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}


// GALLERY
function new_gallery(val){
	check_save_counter();
	
	document.getElementById("new_gallery_"+val).style.display = "";
	document.theform.elements["new_gallery_file_"+val].focus();
}
function add_gallery(val){
	var str = "";
	if(document.theform.elements["new_gallery_file_"+val].value == ""){ str += "\n- Photo"; }

	if(str != ""){
		alert("Please provide the following required information:\n"+str);
	} else {
		document.theform.save_action.value = "add_gallery";
		document.theform.department_id.value = val;
		document.theform.submit();
	}
}
function edit_gallery(id){
	document.theform.edit_id.value = id;
	document.theform.save_action.value = "edit_item";
	document.theform.submit();
}
