/**
This method can be used to submit an 'input' element data to remote url.
This method would take care of forming name/value pair for submission of data.
Parameters
url: to which data has to be submitted
obj: input element whose data has to be submitted
min_size: minimum length of the input element after which Ajax request should be made
@author Chandan
**/

function checkAvailability(url, obj, min_size) {
	if (obj.value.length<min_size) {
		validation_message = 'should be more than ' + min_size + " characters";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfo (result, obj);
		return false;
	}
	params = obj.name + '=' + obj.value;
	new Ajax.Request (url, {
		method : 'post',
		parameters : params,
		onSuccess: function(transport) {
			processInfo(transport.responseText, obj);
		}
	});
}

/**
This method is similar to above method. Although this works just for email.
**/
function checkAvailabilityForEmail(url, obj, min_size) {
	if (!validate_email(obj.value)) {
		validation_message = "invalid email id";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfo (result, obj);
		return false;
	}
	if (obj.value.length<min_size) {
		validation_message = 'should be more than ' + min_size + " characters";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfo (result, obj);
		return false;
	}
	params = obj.name + '=' + obj.value;
	new Ajax.Request (url, {
		method : 'post',
		parameters : params,
		onSuccess: function(transport) { processInfo(transport.responseText, obj);}
	});
}

/**
This method is similar to above method. Although this works just for email inside larger input boxes..
**/
function checkEmailAvailability(url, obj, min_size) {
	if (!validate_email(obj.value)) {
		validation_message = "invalid email id";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfoForLargerFields (result, obj);
		return false;
	}
	if (obj.value.length<min_size) {
		validation_message = 'should be more than ' + min_size + " characters";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfoForLargerFields (result, obj);
		return false;
	}
	params = obj.name + '=' + obj.value;
	new Ajax.Request (url, {
		method : 'post',
		parameters : params,
		onSuccess: function(transport) { processInfoForLargerFields(transport.responseText, obj);}
	});
}

/**
Added by : Akhil Jain
This method is similar to above method. Although this works just for phone.
**/
function checkAvailabilityForPhone(url, obj, max_size) {
	if(obj.value==''){
		validation_message = "Phone number field cannot be empty";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfoForLargerFields (result, obj);
		return false;
	}
	if (!validate_phone(obj.value)) {
		validation_message = "Phone number is invalid";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfoForLargerFields (result, obj);
		return false;
	}
	if (obj.value.length>max_size) {
		validation_message = 'should not be more than ' + max_size + " digits";
		result = '{"result" : "no" , "data" :"' + validation_message + '"}'
		processInfoForLargerFields (result, obj);
		return false;
	}
	params = obj.name + '=' + obj.value;
	new Ajax.Request (url, {
		method : 'post',
		parameters : params,
		onSuccess: function(transport) { processInfoForLargerFields(transport.responseText, obj);}
	});
}


/**
This method is invoked by checkAvailability. it would create a span element adjacent
to the input element, and it takes care of removing the result if result element
is already present on the page.<br>

In general the method expects data in format <br>
{"result" : "[yes : no]" , "data" : "[actual value to be displayed]"}
for example: <br>

{"result" : "yes" , "data" : "available"}
{"result" : "no" , "data" : "email not available"}
{"result" : "yes" , "data" : "Yippie I crashed the system!"}

Parameters
jsonData: data in JSON format
obj: input element whose result has to be updated
@author Chandan
**/
function processInfo(jsonData, obj) {
	var json_data = jsonData.evalJSON();
	var result_span_name = obj.name + "_result";
	var resultSpan = "";
	if ($(result_span_name)) {
		resultSpan = $(result_span_name);
	} else {
		resultSpan = new Element('span', {id : result_span_name});
	}
	resultSpan.className = 'available';
	if ( json_data.result == 'yes') {
		resultSpan.innerHTML = json_data.data;
		obj.parentNode.appendChild(resultSpan);
	} else {
		resultSpan.className = 'not_available';
		resultSpan.innerHTML = json_data.data;
		obj.parentNode.appendChild(resultSpan);
	}
}
/*
Author: Akhil Jain
This function is exactly same as above, the only difference, is it adds the span to the div surroding the input box.
some of the input boxes are surrounded by divs and hence when the baove method get used to add result message
the message gets hidden in the div itself.
*/
function processInfoForLargerFields(jsonData, obj) {
	var json_data = jsonData.evalJSON();
	var result_span_name = obj.name + "_result";
	if ($(result_span_name)) {
		$(result_span_name).remove();
	}
	var resultSpan = new Element('span', {id : result_span_name});
	resultSpan.addClassName('available');
	if ( json_data.result == 'yes') {
		resultSpan.innerHTML = json_data.data;
		obj.parentNode.parentNode.appendChild(resultSpan);
	} else {
		resultSpan = new Element('span', {id : result_span_name});
		resultSpan.addClassName('not_available');
		resultSpan.innerHTML = json_data.data;
		obj.parentNode.parentNode.appendChild(resultSpan);
	}
}

// todo complete this function
function ajaxifyForm(frm_id) {
	frm = $(frm_id);
	if (frm ==null) {
		return null;
	}
	url = frm.action;
	params = frm.serialize();
}

