//Auxillary functions
//check if string has a valid value
function isset(str) {
	if(str === undefined) return 0;
	if(str === null) return 0;
	if(str.match(/^\s*$/)) return 0;
	return 1;
}

/*!
 * \param input_object can be any object
 * \return false on failure, true on success
 */
function isdefined(input_object) {
	if(input_object === undefined) return false;
	if(input_object === null) return false;
	return true;
}

function isKeyPresent(key,object_name) {
	if(key in object_name) return true;
	return false;
}

function isObject(object_name) {
	return ((object_name.constructor == Object) || (object_name.constructor == Array));
}

/*
 *	Input params: field_name, field_type ('string','int','combo')
 *	Return value: false on failure, field_value in case of success
 *	Procedure:
 *			=> If field_type is string then just check if it is set
 *			=> If field type is int then check if it is a valid integer
 *			=> If field type is combo then check if any value is selected
 */
function isFieldSet(field_name,field_type) {
	if(!document.getElementById(field_name)) return false;
	switch(field_type) {
		case 'string':
			var value = document.getElementById(field_name).value;
			if(!isset(value)) return false;
			return value;
		case 'int':
			var value = document.getElementById(field_name).value;
			if(!isValidInteger(value)) return false;
			return value;
		case 'float':
			var value = document.getElementById(field_name).value;
			if(!isValidFloat(value)) return false;
			return value;
		case 'combo':
			var combo_name = document.getElementById(field_name);
			var selec_index = combo_name.selectedIndex;
			if(selec_index < 0) return false;
			var selected_value = combo_name.options[combo_name.selectedIndex].value;
			if(!isset(selected_value)) return false;
			return selected_value;
	}

	return false;
}

/////////////////////////////////////////////////////////////////////
//	These set of functions are used to validate string, integer etc 
/////////////////////////////////////////////////////////////////////

/*
 *	Input params: string
 *	Return value: true/false
 */
function isValidString(str) {
	return isset(str);
}

/*
 *	Input params: value
 *	Return value: true/false
 */
function isValidInteger(int_value) {
	if(int_value == undefined) return false;
	if(!int_value.match(/^\d+$/)) return false;
	return true;
}

/*
 *	\param float_value value to test
 *	\return true on success/false on failure
 */
/*
 *	Make following checks
 *	return false if value is undefined or null
 *	return false if value is empty
 *	return false is value is not float
 */
function isValidFloat(float_value) {
	if( (float_value == undefined) || (float_value == null)) return false;
	if(!float_value.match(/^\d+(.\d+)?$/)) return false;
	return true;
}

