/*====================================================================================================================
// javascript untility functions
//
//	show				show an element
//	hide				hide an element
//	ajax_submit			submit a form with ajax
//	ajax_success		form submitted successfully
//	ajax_failure		form submitted failed
//	toggle				toggle all the checkboxes ina form
//
//===================================================================================================================*/
	//====================================================================================================================
	// display an html element (that was hidden)
	//===================================================================================================================*/
	function show(id) {
		document.getElementById(id).style.display = 'block';
	}
	//====================================================================================================================
	// hide an html element
	//===================================================================================================================*/
	function hide (id) {
		document.getElementById(id).style.display = 'none';
	}
	//====================================================================================================================
	// submit a form with ajax
	//===================================================================================================================*/
	function ajax_submit (form_id, hide_id) {
	
		$.ajax({ type: "POST",
				 url: "index.php",
				 data: $("#"+form_id).serialize(),
				 success: ajax_success,
				 error: ajax_failure	
	    }) 
	    if (hide_id != '') hide(hide_id);
	}
	//====================================================================================================================
	// ajax success response
	//===================================================================================================================*/
	function ajax_success (data, textStatus) {
			
		// update the table if we have one ...
		if ($("#response tr:first").size() > 0) { 	
			$("#response tr:first").after(data);
			
		// ... otherwise update the response div ...
		} else if ($("#response").size() > 0) {
			$("#response").html($("#response").html()+data);
			
		// ... otherwise just give an alert
		} else {
			alert(data);
		}
		
	    if (window.remove_id != "undefined") {
		    $("#"+window.remove_id).remove();
	    }
	}
	//====================================================================================================================
	// ajax failure response
	//===================================================================================================================*/
	function ajax_failure (XMLHttpRequest, textStatus, errorThrown) {
		$("#error").text('failed to save, please try again or contact support ('+errorThrown+')');
	}
	
	//====================================================================================================================
	// sets/clears all checkboxes in a form 
	//====================================================================================================================
	function toggle(formName) {
		len = document.forms[formName].elements.length
		var i=0;
		for( i=0 ; i<len ; i++) {
			elem = document.forms[formName].elements[i]; 
			if (elem.type == 'checkbox') {
				
				if (elem.checked == true) {
					elem.checked = false;
				} else {
					elem.checked = true;
				}
			}
		}
	}
	//====================================================================================================================
	// email validation
	//===================================================================================================================*/
	function is_email(str) {
	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}
	//====================================================================================================================
	// number validation
	//===================================================================================================================*/
	function is_numeric( mixed_var ) {
	    if (mixed_var === '') {
	        return false;
	    }
	    return !isNaN(mixed_var * 1);
	}