/*
*------------------------------------------------------------------------------------
* General form tools
*/

/**
* Javascript equivalent of php's empty() method
* @param mixed_var The var to be checked out
* @return boolean Whether the var exists or not
*/
function empty( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philippe Baumann
    // +      input by: Onno Marsman
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: LH
    // +   improved by: Onno Marsman
    // +   improved by: Francesco
    // *     example 1: empty(null);
    // *     returns 1: true
    // *     example 2: empty(undefined);
    // *     returns 2: true
    // *     example 3: empty([]);
    // *     returns 3: true
    // *     example 4: empty({});
    // *     returns 4: true
    
    var key;
    
    if (mixed_var === ""
        || mixed_var === 0
        || mixed_var === "0"
        || mixed_var === null
        || mixed_var === false
        || mixed_var === undefined
    ){
        return true;
    }
    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            if (typeof mixed_var[key] !== 'function' ) {
              return false;
            }
        }
        return true;
    }
    return false;
}

/**
* Select the text in a form field when clicked
* @param field input field
*/
function select_text(field, defaultValue){
	
	
	// Set default value
	field.def=defaultValue
	
	// Make field normal colour
	field.className="inputNormal";
	
	// Clear text if default value
	if(field.value==field.def){
	
		//alert("clearing value:"+field.value+" (default="+field.defaultValue+")")
		field.value="";
		field.clickedBefore=true
	}
	
	// Only select the text if it doesn't contain the default value
	else{
		if(!field.clickedBefore){
			field.select();
		}
		field.clickedBefore=true
	}

	
	
}

/**
* Select the text in a form field when clicked
* @param field input field
*/
function unselect_text(field){
	// Set field to default calue if it is empty
	if(field.value==""){
		field.value=field.def;
		field.className="inputFaint";
	}
	else if(field.value==field.def){
		field.className="inputFaint";
	}
}


/**
* Handle the response from the php
*/
function submit_join_form_handler(json_string){
	
	// Get the chart_id from the JSON data
	var data=JSON.parse(json_string);
	var form=document.getElementById("join_form")
	
	// SUCCESS
	if(data.success){
		// Go through the fields, wiping any errors
		for(var i=0;i<form.elements.length;i++){
			var field=form.elements[i];
			var msg_div=document.getElementById("join_form_"+field.name+"_msg");
			removeChildNodes(msg_div);
		}
		
		// Display a success message
		var msg_div=document.getElementById("join_form_success_msg");
		removeChildNodes(msg_div);
		appendParagraph(msg_div, "Thanks! You were successfully added to Arika's mailing list.");
	}
	
	// FAILURE
	else{
		if(!empty(data.errors)){
			// Remove any success message
			var msg_div=document.getElementById("join_form_success_msg");
			removeChildNodes(msg_div);
		
			// Go through the fields, check whether each field has an error associated with it
			for(var i=0;i<form.elements.length;i++){
				var field=form.elements[i];
				var msg_div=document.getElementById("join_form_"+field.name+"_msg")				
				if(!empty(data.errors[field.name])){
					removeChildNodes(msg_div);
					appendParagraph(msg_div, data.errors[field.name]);
				}else{
					removeChildNodes(msg_div);
				}
			}	
		}
	}
}


/**
* Get the checked value from a given radio button group
* @param formId Id tag of form containing the radio button group
* @param buttonGroupName The name of the button group
* @return Value of the checked radio button (null if none checked)
*/
function getRadioButtonValue(formId,buttonGroupName) {
    var form=document.getElementById(formId);
	var buttonGroup=form[buttonGroupName];
	var buttonChecked=-1;
    for (var i=0; i <buttonGroup.length; i++) {
        if (buttonGroup[i].checked) {
			buttonChecked=i;
			break;
		}
    }
    if (buttonChecked>-1){
		return buttonGroup[buttonChecked].value;
	}
    else return null;
}
/**
* Get the selected value from a given select box
* @param formId Id tag of form containing the radio button group
* @param selectboxName The name of the button group
* @return Value of the selected option (null if nothing selected)
*/
function getSelectedValue(selectboxId) {
    var selectbox=document.getElementById(selectboxId);
	var selectedIndex=selectbox.selectedIndex;
    if (selectedIndex!=null){
		return selectbox.options[selectedIndex].value;
	}
    else return null;
}
/**
* Disables a select box
* @param selectboxId Id tag of select box
*/
function disableSelectbox(selectboxId) {
     var selectbox=document.getElementById(selectboxId);
	 selectbox.disabled=true;
}
/**
* Enables a select box
* @param selectboxId Id tag of select box
*/
function enableSelectbox(selectboxId) {
     var selectbox=document.getElementById(selectboxId);
	 selectbox.disabled=false;
}
/**
* Disables all radio buttons in a group
* @param formId Id tag of form containing the radio button group
* @param buttonGroupName The name of the button group
*/
function disableRadioButtonGroup(formId,buttonGroupName) {
    var form=document.getElementById(formId);
	var buttonGroup=form[buttonGroupName];
    for (var i=0; i <buttonGroup.length; i++) {
        buttonGroup[i].disabled=true;
    }
}
/**
* Enables all radio buttons in a group
* @param formId Id tag of form containing the radio button group
* @param buttonGroupName The name of the button group
*/
function enableRadioButtonGroup(formId,buttonGroupName) {
    var form=document.getElementById(formId);
	var buttonGroup=form[buttonGroupName];
    for (var i=0; i <buttonGroup.length; i++) {
        buttonGroup[i].disabled=false;
    }
}

/**
* Disable button
* @param Button id tag
*/
function disable_button(button_id){
	var button=document.getElementById(button_id)
	button.disabled=true;
}

/**
* Enable button
* @param Button id tag
*/
function enable_button(button_id){
	var button=document.getElementById(button_id)
	button.disabled=false;
}
