// -------------------------------- javascript extension -------------------------------- //

String.prototype.trim = function () {
  return Trim(this);
}

function Trim(s) {
	s = s.replace(/(^\s+)|(\s+$)/g, "");
	return s
}

// -------------------------------- javascript extension -------------------------------- //


/**
 *	common functions for any application
 */

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function isNotFilled(elm) {
  return ((typeof elm == "undefined" || Trim(elm.value) == "" || Trim(elm.value) == null)) ? true : false;
}

function empty( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philippe Baumann
    // *     example 1: empty(null);
    // *     returns 1: true

    return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  ( is_array(mixed_var) && mixed_var.length === 0 ) );
}

function is_null(val) {
	return (typeof val == "undefined" || val == null || val == "") ? true : false;
}

function checkAll(isCheckedAll, form_checkbox_name) {
	var checkboxs = document.getElementsByName(form_checkbox_name);

	if(typeof checkboxs == "undefined") return;
	if(checkboxs.length) {
	    for(var i = 0, cb; cb = checkboxs[i]; i++)
	      cb.checked = isCheckedAll;
	} else {
	    checkboxs.checked = isCheckedAll;
	}

}

function adjustCheckAll(form_checkbox_name, form_checkAll_name) {

	var checkboxs = document.getElementsByName(form_checkbox_name);
	var checkAll = document.getElementById(form_checkAll_name);

	if(checkboxs.length) {
	    // 多個checkbox的處理
	    for(var i = 0, cb; cb = checkboxs[i]; i++) {
	      if(cb.checked) {
	        continue;
	      } else {
	        checkAll.checked = false;	// 只要有一個沒有勾選，全選的勾勾就要拿掉
	        return;
	      }
	    }
	    checkAll.checked = true;
	} else {
	    // 1個checkbox的處理
	    checkAll.checked = checkboxs.checked;
	    return ;
	}
}

function form_multi_check(form_id, checkbox_name) {
	var form = document.getElementById(form_id);
	if(typeof form[checkbox_name] == "undefined") {
		return false;
	}

	if(form[checkbox_name].length) {
		for(var i= 0, cb; cb = form[checkbox_name][i]; i++) {
       		hasChecked = cb.checked;
       		if(hasChecked)
         			break
       		else
         		continue;
     	  	}
  	} else {
     		hasChecked = form[checkbox_name].checked;
   	}
	return  hasChecked;
}

function checkImageExtension(filename) {
	var re = new RegExp("\.(jpg|jpeg|gif|png)$");
	if (!re.exec(filename.toLowerCase())) {
		alert('請上傳 JPG、JPEG、GIF、PNG 等格式的圖檔！');
		return false;
	}
	return true;
}

function img_auto_resize(img, w, h) {

  	if ($(img).width() >= $(img).height()) {
  		if ($(img).width() > w || $(img).width()== 0 ) {
  			$(img).width(w);
  		}
	} else {
  		if ($(img).height() > h || $(img).height() == 0 ) {
  			$(img).height(h);
		}
	}
	$(img).show();
}
