/**
 * js.js
 * @copyright Ayrmer Software 2006
 * @author Sam West
 * 
 */

function set_link(page){
	document.forms[0].insert.value = "index.php?"+((parseFloat(page)) ? "page=" : "alias=") + page
}

var requester;


/**
 * Sends an ajax request to get a list of organisations
 * @param string search_string 		= what to search for
 * @param integer id				= the contact id 
 */
function get_orgs(search_string, id) {
	
	if(!requester)
		requester = new Requester();
	
	if(document.getElementById('org_search').style.display = 'none')
		document.getElementById('org_search').style.display = '';
		
	requester.setAction(set_orgs);
	requester.loadURL('ajax.contact_to_org.php','search_string='+search_string+'&id='+id);
}

function get_comm_orgs(search_string) {
	
	if(!requester)
		requester = new Requester();
	
	if(document.getElementById('org_search').style.display = 'none')
		document.getElementById('org_search').style.display = '';
		
	requester.setAction(set_orgs);
	requester.loadURL('ajax.comm_to_org.php','search_string='+search_string);
}

function get_cats(search_string,newsletter) {
	
	if(!requester)
		requester = new Requester();
	
	if(document.getElementById('cat_search').style.display == 'none')
		document.getElementById('cat_search').style.display = '';		
	
	requester.setAction(set_cat);
	requester.loadURL('ajax.contact_types.php','search_string='+search_string+'&newsletter=' +newsletter);
}

function add_ab_to()
{
   if(document.forms[0].to.value != "")
   {
      document.forms[0].to.value = document.forms[0].to.value + ', ' + document.forms[0].address_book_to.value;
   }
   else
   {
      document.forms[0].to.value = document.forms[0].address_book_to.value;   
   }
}

function add_ab_cc()
{
   if(document.forms[0].cc.value != "")
   {
      document.forms[0].cc.value = document.forms[0].cc.value + ', ' + document.forms[0].address_book_to.value;
   }
   else
   {
      document.forms[0].cc.value = document.forms[0].address_book_to.value;   
   }
}

function add_ab_bcc()
{
   if(document.forms[0].bcc.value != "")
   {
      document.forms[0].bcc.value = document.forms[0].bcc.value + ', ' + document.forms[0].address_book_to.value;
   }
   else
   {
      document.forms[0].bcc.value = document.forms[0].address_book_to.value;   
   }
}

function set_orgs() {
	document.getElementById('org_search').innerHTML = requester.getText();
}

function set_cat() {
	document.getElementById('cat_search').innerHTML = requester.getText();
}


/**
 * Disables the enter button of a field so that the form doesn't submit when not wanted to
 * @param reference field 		= the html calling field
 * @param reference event		= the calling event
 */
function disable_enter(field, event) {
	
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if (keyCode == 13) {
		field.focus();
		return false;	
	}
	
	return true;
}

/**
 * Clears an input field
 * @param reference element 	= the html input field
 * @param string string			= the string within the field
 */
function input_select_clear(element, string) {
	
	// if an element and string has been supplied
	if(element && string != '') {
		
		// if the value of the element is the same as the string clear the field
		if(element.value == string) {
			
			element.value = '';

			element.string = string;
			
			// add an on blur function to repopulate the string if the field is left empty
			element.onblur = function() {
				if(this.value == '') {
					this.value = this.string;	
				}
			};
		}
	}
}



function populate_auth_checks(element, num) {
	try {
		if(!num)
			return;
		
		// get the checks id
		caller_id = element.id;
		
		// split it into its parts
		caller_bits = caller_id.split('_');

		// get the caller number and remove it from the caller bits array
		caller_number = caller_bits.pop();
		
		// clone the caller_bits array
		var parent_elements = new Array;
		for(var i = 0; i < caller_bits.length; i++) {
			parent_elements[i] = caller_bits[i];
		}
		
		
		// remove the prefix string from the array
		parent_elements.shift();
		
		//alert(parent_elements);
		
		
		
			
		
		/*
		alert(typeof(parent_elements));
		parent_elements.shift();
		alert(parent_elements);*/
		
		
		
//		for(var a = 1; a < temp.length; a++) {
//			alert(temp[a]);
//		}
		

		// loop the number of elements
		for(var i=1; i <= num; i++) {
			
			// if the current itteration is the caller id ignore it
			if(caller_number == i) {
				continue;
			}
			
			// stick the id back together plus i to get the id
			id = caller_bits.join('_')+'_'+i;
				
			// get the element
			sub_element = document.getElementById(id) 
			
			// if its below the clicked element select it (unless its the first one)
			if(i <= caller_number) {
					sub_element.checked = true;
			}
			// otherwise unselect it
			else {
				sub_element.checked = false;
			}
		}
	}
	catch (error) {alert(error)}
}	


var marked_row = new Array;
function markRowsInit(odd_class_name, even_class_name, marked_class_name, hover_class_name) {
	
	if(!odd_class_name) {
		odd_class_name = 'odd';
	}
	if(!even_class_name) {
		even_class_name = 'even';
	}
	
	if(!marked_class_name) {
		marked_class_name = "marked";
	}
	
	if(!hover_class_name) {
		hover_class_name = "hover";
	}
	
	
	
    // for every table row ...
	var rows = document.getElementsByTagName('tr');
	
	
	for ( var i = 0; i < rows.length; i++ ) {
	    // ... with the class 'odd' or 'even' ...
		if ( odd_class_name != rows[i].className && even_class_name != rows[i].className ) {
			
		    continue;
		}
		
	    // ... add event listeners ...
        // ... to highlight the row on mouseover ...
	    if ( navigator.appName == 'Microsoft Internet Explorer' ) {
	        // but only for IE, other browsers are handled by :hover in css
			rows[i].onmouseover = function() {
			    this.className += ' '+hover_class_name;
			}
			rows[i].onmouseout = function() {
			    this.className = this.className.replace( ' '+hover_class_name, '' );
			}
	    }
        // ... and to mark the row on click ...
		rows[i].onmousedown = function() {
		    var unique_id;
            var checkbox;

 
            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
		        return;
		    }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                marked_row[unique_id] = true;
            } else {
                marked_row[unique_id] = false;
            }

            if ( marked_row[unique_id] == true) {
			    this.className += ' '+marked_class_name;
            } else {
				
			    this.className = this.className.replace(' '+marked_class_name, '');

            }

            if ( checkbox && checkbox.disabled == false ) {
                checkbox.checked = marked_row[unique_id];
            }
           
		}

		// ... and disable label ...
		var labeltag = rows[i].getElementsByTagName('label')[0];
		if ( labeltag ) {
		    labeltag.onclick = function() {
		        return false;
		    }
	    }
	    // .. and checkbox clicks
		var checkbox = rows[i].getElementsByTagName('input')[0];
		if ( checkbox ) {
		    checkbox.onclick = function() {
		        // opera does not recognize return false;
		        this.checked = ! this.checked;
		    }
	    }
	}
}


/**
 * Marks / Unmarks all depending on the callers state
 *
 * @param caller the check box that called
 * @param    container    DOM element
 */
function checkUncheckAll(caller, container) {
	if(caller.checked) {
		
		markAllRows(caller, container);

	}
	else {
		unMarkAllRows(caller, container);

	}
		
}
/**
 * Unmarks all rows and deselects the first checkbox inside the given element
 *
 * @param caller the check box that called
 * @param    container    DOM element
 */
function markAllRows( caller, container_id ) {

	var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' && checkbox != caller) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
	    }
	}

//	return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param	caller	the checkbox that called
 * @param    container    DOM element
 */
function unMarkAllRows( caller, container_id ) {

	var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' && checkbox != caller) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
	}

//	return true;
}

function resize_email_iframe(element) {
	try {	
		iframe = document.getElementById('email_body');
		iframe.height = element.offsetHeight + 35;
	}
	catch (error) {}	
}

function my_contacts() {
	try {
		
	}
	catch(error) {}
	
}


function auto_upload() {
	try{
		document.getElementById('add_attachment_button').click();
	}
	catch(errors){}
}

function enable_followup(caller) {
	try {
		if(caller.checked == false) {
			document.getElementById('comm_followup_date_d').disabled = true;
			document.getElementById('comm_followup_date_m').disabled = true;
			document.getElementById('comm_followup_date_y').disabled = true;
			document.getElementById('comm_followup_date_hr').disabled = true;
			document.getElementById('comm_followup_date_mi').disabled = true;
			document.getElementById('followup_by').disabled = true;
		}
		else {
			document.getElementById('comm_followup_date_d').disabled = false;
			document.getElementById('comm_followup_date_m').disabled = false;
			document.getElementById('comm_followup_date_y').disabled = false;
			document.getElementById('comm_followup_date_hr').disabled = false;
			document.getElementById('comm_followup_date_mi').disabled = false;
			document.getElementById('followup_by').disabled = false;
		}
		
	}
	catch(errors){}
}

function enable_followup_onload() {
	enable_followup(document.getElementById('comm_requires_followup'));
	
}