var createdHtmlArea = false;
var okayToSave = false;
var WAIT_SECONDS = 5;
var wait_takes = 0;



var files = 0;
var files_removed = 0;
var file_counter = 0;

var contacts = 0;
var contacts_removed = 0;
var contact_counter = 0;

var domains = 0;
var domains_removed = 0;
var domain_counter = 0;

var links = 0;
var links_removed = 0;
var link_counter = 0;

function ById(a) {
	return byId(a);
}

function byId(a) {
	return document.getElementById(a);
}


function clear_tip_form() {
	
	
	byId('tip_title').value = '';
	
	byId('tip_description').innerHTML = '';
	byId('tip_id').value = '0';
	
	byId('check_files').value = '0';
	byId('check_links').value = '0';
	byId('check_domains').value = '0';
	byId('check_contacts').value = '0';
	
	// all of the linked info div holders probably need to be toggled here.
}

function add_tip() {
	//clear out form
	clear_tip_form();
	show_tip_form('Add tip');
}

function edit_tip(id) { 
	clear_tip_form();
	
	show_tip_form('Edit Tip');
	
	do_edit_load(id);
	//jh.call_php_function('get_tip',id);	
}

function do_edit_load(id) {
	if ( editor ) {
		jh.call_php_function('get_tip',id);	
	} else {
		setTimeout('do_edit_load(' + id + ')',750);
	}
}

function edit_tip_load(tip_id,status,tip_title,tip_description,category_list,
						contact_name,contact_email,contact_state,link_title,link_href,all_docs,all_docs_id) {
	
	byId('tip_id').value = tip_id;
	byId('tip_title').value = tip_title;
	
	if ( status == 1 ) {
		byId('status').checked = true;
	} else {
		byId('status').checked = false;
	}
	
	editor.setHTML(tip_description);
	//byId('tip_fake_id').value = tip_fake_id;
	/*
	 * CONTACTS
	 */
	var contact_num = contact_name.length;
	//basically the same as attachContact
	if ( contact_num > 0 ) {
		byId('contact_attach_holder').style.display = 'block';
		byId('contact_attach_start').style.display = 'none';
		for ( var i=0; i<contact_num; i++ ) {
			attachAnotherContact();
		}
	
		//these for-loops are seperated to give them proper time to load...
		for ( var i=0; i<contact_num; i++ ) {
			byId('contact_name' + (i+1)).value = contact_name[i];
			byId('contact_email' + (i+1)).value = contact_email[i];
			byId('contact_state' + (i+1)).value = contact_state[i];
		}
	}
	/*
	 * CATEGORIES (domains)
	 */
	var category_num = category_list.length;
	if ( category_num > 0 ) {
		byId('domain_attach_holder').style.display = 'block';
		byId('domain_attach_start').style.display = 'none';
		
		for ( var i=0; i<category_num;i++ ) {
			attachAnotherDomain();
		}
		//give time to let load...
		for ( var i=0; i<category_num;i++ ) {
			byId('domain' + (i+1)).value = category_list[i];
		}
	}
	/*
	 * LINKS
	 */
	var link_num = link_title.length;
	if ( link_num > 0 ) {
		byId('link_attach_holder').style.display = 'block';
		byId('link_attach_start').style.display = 'none';
		
		for ( var i=0; i<link_num;i++ ) {
			attachAnotherLink();
		}
		//and some time to load...
		for ( var i=0; i<link_num;i++ ) {
			byId('link_title' + (i+1)).value = link_title[i];
			byId('link_url' + (i+1)).value = link_href[i];
		}
	}
	/*
	 * FILES
	 */
	if ( all_docs.length > 0 ) {
		byId('attached_files_holder').style.display = 'block';
		var tmp_contain = byId('attached_files_list');
		for (var i=0;i<all_docs.length;i++) {
			var tmp_li = document.createElement('li');
			tmp_li.appendChild(document.createTextNode(all_docs[i]));
			tmp_li.appendChild(document.createTextNode(' '));
			tmp_li.id = 'doc_list_' + all_docs_id[i];
			var tmp_a = document.createElement('a');
			tmp_a.href = 'javascript:deactivate_file(' + all_docs_id[i] + ')';
			tmp_a.appendChild(document.createTextNode('remove'));
			tmp_li.appendChild(tmp_a);
			tmp_contain.appendChild(tmp_li);
		}
	}
	
}

function show_tip_form(title) {
	if ( !createdHtmlArea) {
		createdHtmlArea = true;
		HTMLArea.init();
	}
	byId('tip_list_holder').style.display = 'none';
	byId('tip_form').style.display = 'block';
	byId('add_tip').style.display = 'none';
	byId('tip_form_title').innerHTML = title;
}

function cancel_tip_form() {
	var msg = "Are sure you want close without saving?";
	if ( window.confirm(msg) ) {
		window.location.href = 'tip_list.php'; //();
	}
}

/*
 * LINKS
 */

function attachLink() {
	byId('link_attach_holder').style.display = 'block';
	byId('link_attach_start').style.display = 'none';
	attachAnotherLink();	
}

function attachAnotherLink() {
	var container = byId('link_holder');
	links++;
	link_counter++;
	
	var single_holder = document.createElement('div');
	single_holder.id = 'lh' + links;
	
	
	var link_title_elm = document.createElement('input');
	link_title_elm.id = 'link_title' + links;
	link_title_elm.name = 'link_title' + links;
	var link_url_elm = document.createElement('input');
	link_url_elm.id = 'link_url' + links;
	link_url_elm.name = 'link_url' + links;
	single_holder.appendChild(document.createTextNode('Title:'));
	single_holder.appendChild(link_title_elm);
	single_holder.appendChild(document.createTextNode('URL:'))
	single_holder.appendChild(link_url_elm);
	
	var new_remover = document.createElement("span");
	new_remover.id = 'link_nr' + links;
	new_remover.class = links;
	new_remover.onclick = function() {
		var tmp_file = links;
		if ( window.confirm("Are you sure you want to remove this link?")) {
			links_removed++;
			link_counter--;
			removeLinkElement('link_title' + this.class);
			removeLinkElement('link_url' + this.class);
			removeLinkElement('lh' + this.class);
		}
	}
	new_remover.innerHTML = "remove";
	new_remover.style.cursor = "pointer";
	new_remover.style.textDecoration = 'underline';
	new_remover.style.color = 'blue';
	
	single_holder.appendChild(new_remover);
	container.appendChild(single_holder);
	
	byId('check_links').value = links;	
}

function removeLinkElement(elementId) {
    // Removes an element from the document
    var element = document.getElementById(elementId);
    element.parentNode.removeChild(element);
    
    if ( links_removed == links ) {
    	byId('link_attach_holder').style.display = 'none';
    	byId('link_attach_start').style.display = 'block';
    	link_counter = 0;
    }
}

/*
 * DOMAINS
 */

function attachDomain() {
	byId('domain_attach_holder').style.display = 'block';
	byId('domain_attach_start').style.display = 'none';
	attachAnotherDomain();	
}

function attachAnotherDomain() {
	var container = byId('domain_holder');
	domains++;
	domain_counter++;
	
	var single_holder = document.createElement('div');
	single_holder.id = 'dh' + domains;
	
	var domain_elm = document.createElement('select');
	domain_elm.id = 'domain' + domains;
	domain_elm.name = 'domain' + domains;
	
	var domain_list_count = domain_titles.length;
	
	for (var i=0;i<domain_list_count;i++) {
		var tmp_option = document.createElement('option');
		tmp_option.value = domain_ids[i];
		tmp_option.appendChild(document.createTextNode(domain_titles[i]));
		domain_elm.appendChild(tmp_option);
	}
	
	single_holder.appendChild(domain_elm);
	
	var new_remover = document.createElement("span");
	new_remover.id = 'domain_nr' + domains;
	new_remover.class = domains;
	new_remover.onclick = function() {
		var tmp_file = domains;
		if ( window.confirm("Are you sure you want to remove this domain?")) {
			domains_removed++;
			domain_counter--;
			removeDomainElement('domain' + this.class);
			removeDomainElement('dh' + this.class);
		}
	}
	new_remover.innerHTML = "remove";
	new_remover.style.cursor = "pointer";
	new_remover.style.textDecoration = 'underline';
	new_remover.style.color = 'blue';
	
	single_holder.appendChild(new_remover);
	container.appendChild(single_holder);
	
	byId('check_domains').value = domains;
}

function removeDomainElement(elementId) {
    // Removes an element from the document
    var element = document.getElementById(elementId);
    element.parentNode.removeChild(element);
    
    if ( domains_removed == domains ) {
    	byId('domain_attach_holder').style.display = 'none';
    	byId('domain_attach_start').style.display = 'block';
    	domain_counter = 0;
    }
}

/*
 * CONTACTS
 */

function attachContact() {
	byId('contact_attach_holder').style.display = 'block';
	byId('contact_attach_start').style.display = 'none';
	attachAnotherContact();	
}

function attachAnotherContact() {
	var container = byId('contact_holder');
	contacts++;
	contact_counter++;
	var single_holder = document.createElement("div");
	single_holder.id = 'ch' + contacts;
	
	var contact_name_elm = document.createElement("input");
	contact_name_elm.id = 'contact_name' + contacts;
	contact_name_elm.name = 'contact_name' + contacts;
	contact_name_elm.type = 'text';
	
	var contact_email_elm = document.createElement('input');
	contact_email_elm.id = 	'contact_email' + contacts;
	contact_email_elm.name = 'contact_email' + contacts;	
	contact_email_elm.type = 'text';
	
	// need to bring all the states....
	
	var contact_state_elm = document.createElement('select');
	contact_state_elm.id = 'contact_state' + contacts;
	contact_state_elm.name = 'contact_state' + contacts;
	contact_state_elm.style.width = '120px';
	
	var state_list_length = full_state.length;
	
	for ( var i=0;i<state_list_length;i++ ) {
		var tmp_option = document.createElement('option');
		tmp_option.value = short_state[i];
		tmp_option.appendChild(document.createTextNode(full_state[i]));
		contact_state_elm.appendChild(tmp_option);
	}
	
	
	single_holder.appendChild(document.createTextNode('Name:'));
	single_holder.appendChild(contact_name_elm);
	single_holder.appendChild(document.createTextNode('Email:'));
	single_holder.appendChild(contact_email_elm);
	single_holder.appendChild(document.createTextNode('State:'));
	single_holder.appendChild(contact_state_elm);

	var new_remover = document.createElement("span");
	new_remover.id = 'contact_nr' + contacts;
	new_remover.class = contacts;
	new_remover.onclick = function() {
		var tmp_file = contacts;
		if ( window.confirm("Are you sure you want to remove this contact?")) {
			contacts_removed++;
			contact_counter--;
			removeContactElement('contact_name' + this.class);
			removeContactElement('contact_email' + this.class);
			removeContactElement('contact_state' + this.class);
			removeContactElement('ch' + this.class);
		}
	}
	new_remover.innerHTML = "remove";
	new_remover.style.cursor = "pointer";
	new_remover.style.textDecoration = 'underline';
	new_remover.style.color = 'blue';
	
	single_holder.appendChild(new_remover);
	container.appendChild(single_holder);
	byId('check_contacts').value = contacts;
}



function removeContactElement(elementId) {
    // Removes an element from the document
    var element = document.getElementById(elementId);
    element.parentNode.removeChild(element);
    
    if ( contacts_removed == contacts ) {
    	byId('contact_attach_holder').style.display = 'none';
    	byId('contact_attach_start').style.display = 'block';
    	contact_counter = 0;
    }
}

/*
 * FILES...
 */

function attachFile() {
	byId('attach_holder').style.display = 'block';
	byId('attach_start').style.display = 'none';
	attachAnotherFile();
	//files++;
}

function attachAnotherFile() {
	var container = byId('file_holder');
	files++;
	file_max = 5;
	if ( file_counter < file_max ) {
		file_counter++;
		single_holder = document.createElement("div");
		single_holder.id = 'sh' + files;
		
		new_input = document.createElement("input");
		new_input.id = 'upload' + files;
		new_input.name = 'upload' + files;
		new_input.type = 'file';
		
		var new_remover = document.createElement("span");
		new_remover.id = 'nr' + files;
		new_remover.class = files;
		new_remover.onclick = function() {
			var tmp_file = files;
			if ( window.confirm("Are you sure you want to remove this file?")) {
				files_removed++;
				file_counter--;
				removeFileElement('upload' + this.class);
				removeFileElement('sh' + this.class);
			}
		}
		
		
		new_remover.innerHTML = "remove";
		new_remover.style.cursor = "pointer";
		new_remover.style.textDecoration = 'underline';
		new_remover.style.color = 'blue';
		
		single_holder.appendChild(new_input);
		single_holder.appendChild(new_remover);
		container.appendChild(single_holder);
		byId('check_files').value = files;
	} else {
		window.alert("Sorry, you can not upload more than " + file_max + " files");
	}
}

function removeFileElement(elementId) {
    // Removes an element from the document
    var element = document.getElementById(elementId);
    element.parentNode.removeChild(element);
    
    if ( files_removed == files ) {
    	byId('attach_holder').style.display = 'none';
    	byId('attach_start').style.display = 'block';
    	file_counter = 0;
    }
}

function deactivate_file(file_id) {
	if ( window.confirm('Are you sure you want to delete this file?') ) {
		jh.call_php_function('deactivate_file',file_id);
	}
}

function deactivate_file_done(file_id) {
	byId('doc_list_' + file_id).style.display = 'none';
}

function save_tip_done() {
	//window.//alert(title + " saved successfully");
	byId('tip_uploading_box').style.display='none';
	show_done_message();
	byId('count_down').innerHTML = (WAIT_SECONDS);
	setTimeout('reload_window()',1000);
}

/*
 * CHECKING...
 */


function check_save_tip_form() {
	
	var required_fields = new Array('tip_title');
	var required_pretty = new Array('Title');
	var all_required = true;
	var missing_domain = false;
	var missing_contact = false;
	
	var err_msg = '';
	var err_msg_holder = byId('err_msg');
	removeChildrenFromNode(err_msg_holder);
	var br1 = document.createElement('br');
	var tmp_p = document.createElement('p');
	for ( var i=0;i<required_fields.length;i++ ) {
		if ( byId(required_fields[i]).value.length == 0 ) {
			all_required = false;
			
			//tmp_p.appendChild(document.createTextNode(required_pretty[i] + ' is a required field'));
			//tmp_p.appendChild(document.createElement('br'));
			//tmp_p.appendChild(document.createElement('br'));
			byId(required_fields[i]).style.backgroundColor = '#FFFFBF';
		} else {
			byId(required_fields[i]).style.backgroundColor = 'white';
		}
	}
	
	
	/*
	 * CHECK DESCRIPTION
	 */
	var description_text = editor.getHTML();
	
	if ( description_text.length <= 15 ) { //13 == blank (just html)
		all_required = false;
		//tmp_p.appendChild(document.createTextNode('Description is a required field.'));
		//tmp_p.appendChild(document.createElement('br'));
		//tmp_p.appendChild(document.createElement('br'));
	} else {
		byId('tip_d_actual').value = description_text;
	}
	
	/*
	 * CHECK DOMAIN
	 */
	
	if ( domain_counter < 1 ) {
		all_required = false;
		//tmp_p.appendChild(document.createTextNode('A Domain is required.'));
		//tmp_p.appendChild(document.createElement('br'));
		//tmp_p.appendChild(document.createElement('br'));
	} 
	
	/*
	 * CHECK FOR CONTACT
	 */
	if ( contact_counter < 1 ) {
		missing_contact = true;
	} else {
		for ( var i = 1; i <= contact_counter; i++ ) {
			if ( byId('contact_name' + i).value.length == 0 || byId('contact_email' + i).value.length == 0 ) {
				missing_contact = true;
			}
		}
	}
	
	if ( missing_contact) {
		all_required = false;
		//tmp_p.appendChild(document.createTextNode('A Contact is required (all fields).'));
		//tmp_p.appendChild(document.createElement('br'));
		//tmp_p.appendChild(document.createElement('br'));
	}
	
	/*
	 * SHOW ERRORS OR SAVE FORM...
	 */
	if ( !all_required ) { // exit here if the form is not ready to go...
		err_msg_holder.appendChild(tmp_p);
		//scroll to top to show user their errors
		scroll(0,0);
		return;
	} else { //submit this wild from, since it is ready to go.
		byId('do_upload').submit();
		scroll(0,0);
	}
	
}

function show_done_message() {
	byId('tip_done_box').style.display="block";
	
	byId('tip_list_holder').style.display = 'none';
	byId('tip_form').style.display = 'none';
	byId('add_tip').style.display = 'none';
	//byId('assessment_form_title').innerHTML = title;
}

function force_reload_window() {
	window.location.href = 'tip_list.php';
}

function reload_window() {
	if ( (WAIT_SECONDS) <= wait_takes ) {
		force_reload_window();
	} else {
		wait_takes++;
		byId('count_down').innerHTML = (WAIT_SECONDS - wait_takes);
		setTimeout('reload_window()',1000);
	}
}

function delete_tip(tip_id) {
	if ( window.confirm("Are you sure you want to delete this tip?") ) {
		jh.call_php_function('delete_tip',tip_id);
	}	
}

function delete_done(flag) {
	if ( flag == 1 ) {
		window.alert("The tip has been deleted. The page will now refresh to reflect this change.");
		force_reload_window();
	} else {
		window.alert("I am sorry you do not have permission to delete tips. In fact, that you are even getting this error message is suspect...and you probably have been messing around where you shouldn't.");
	}
}

function removeChildrenFromNode(node) {
	  
    var len = node.childNodes.length;

	while (node.hasChildNodes())
	{
	  node.removeChild(node.firstChild);
	}
}

function stopUpload(success) {
	var result = '';
	//alert('upload done');
	if (success == 1){
		okayToSave = true;
		save_tip_done();
	} else {
	}

	return true;
}


function startUpload() {
	//alert('started upload')
	return true;
}
