/*
 This file is part of HiNii.

 Copyright (C) 2008 Carnino Claudio (jollyr0ger) <jollyr0g3r@gmail.com>
 */

/**
 * Collection of Ajax.Request
 * @author jollyr0ger
 *
 * Attention need:
 * -Prototype
 * -AjaxUtils
 * -DomUtils
 */
var AjaxRequest = Class.create({ });
Object.extend(AjaxRequest, {
	
	
	/**
	 * Ajax.Request to AjaxController::fieldvalidateAction(),
	 * and set the result into the dom structure
	 * 
	 * @param	string			lang				User language
	 * @param	int				category			Category id
	 * @param	string			controllerName		Controller name frome where is called (eg 'Register' or 'Point')
	 * @param 	string			fieldName			Field name (eg. username or city)
	 * @param	string			errorElem			Element name where to put the errors, ussulliy a div
	 * @param	bool			keepPreviousMsg		If true does not eliminate previous error message 
	 * @param	string			styleWithMessage	Name of the css style to apply if there are messages
	 * @param	string			styleWithNoMessage	Name of the css style to apply if there arn't message
	 */
	validateField: function(lang, category, controllerName, fieldName, errorElem, keepPreviousMsg, styleWithMessage, styleWithNoMessage){

		// Strip spaces and tags
		if( !fieldName.include('password') ){
			DomUtils.stripAll(fieldName);
		}

		if( fieldName.include('telephone') ){
			DomUtils.filterDigits(fieldName);
		}
	
		new Ajax.Request(
			'/'+ lang +'/ajax/fieldvalidate/', {
			parameters: { 
				'category': category,
				name: controllerName +'_'+ fieldName,
				value: $(fieldName).value
			},
			onSuccess: function(transport){
			
				AjaxUtils.setCheckResult(
					transport, 
					fieldName, 
					errorElem, 
					keepPreviousMsg,
					styleWithMessage,
					styleWithNoMessage
				);
			
			}
		});

	},
	
	
	
	/**
	 * Make an ajax request to AjaxController.php to get similar points
	 * @param	string		lang		User language
	 * @param	Element		name		Point's name
	 * @param	Element		city		Point's city
	 * @param	Element		address		Point's address
	 * @param	Element		where		Where place the result
	 * @param	int			id_diff		Choosed points need to be different from the id
	 */
	similPoints: function(lang, name, city, address, where, id_diff){
		
		new Ajax.Request(
			'/'+ lang +'/ajax/similpoints/', 
			{ 
				parameters: {
					'name': DomUtils.stripAll(name),
					'city': DomUtils.stripAll(city),
					'address': DomUtils.stripAll(address)
				},
				onSuccess: function(transport){
				
					// Get the div where to put the new elements
					var listDiv = $(where);
					Element.update(listDiv, '');
				
					var response = transport.responseJSON;				
					for(iRes = 0; iRes < response.size(); iRes++){
						
						// Remove the point with the passed id
						if(response[iRes].id != id_diff){
							
							// Prepare the new point's div
							var html = '<div class="IpSimilarPointsElements">';
							html += '<br /><a href="/'+ lang +'/point/view/id/'+ response[iRes].id +'/" target="_blank">'+ response[iRes].name.capitalize() +'</a>';
							
							if(response[iRes].address != ''){
								html += '<br />'+ response[iRes].address.capitalize().unescapeHTML();
							}
							
							html += '<br />'+ response[iRes].city.capitalize() +' ('+ response[iRes].country.capitalize() +')';
							html += '</div>';
							
							// Insert it
							listDiv.insert(html, 'bottom');
							
						}
						
					}
				
				}			
		});
		
	},
	
	
	
	/**
	 * Bookmark a point, making an Ajax request
	 * @param	int			id				Point id
	 * @param	int			type			If the operation is Bookmark or RememberMe 
	 * @param	string		elementName		Image element, name
	 * @param	string		imgSource		Image souce path
	 */
	bookmarkPoint: function(id, type, elementName, imgSource){
		
		// Both parameters needed
		if(id !== null && type !== null){
			
			new Ajax.Request(
				'/en/ajax/bookmarkpoint/', {
				parameters: { 
					'id': id,
					'type': type
				},
				onSuccess: function(transport){

					if(transport.responseJSON){

						// Block the Bookmark or RememberMe button
						// depending on 'type'
						AjaxUtils.lightBookmarkButtons(elementName, imgSource);
					}
				}
			});
			
		}
		
	},
	
	
	
	/**
	 * Bookmark a point, making an Ajax request
	 * @param	int			id					Point id
	 * @param	int			type				If the operation is Bookmark or RememberMe 
	 * @param	string		imgElement			Image element, name
	 * @param	string		imgSource			Image souce path
	 * @param	string		sentenceElement		Sentence element, name
	 * @param	string		sentenceSource		Sentence to update
	 */
	bookmarkPointAlt: function(id, type, imgElement, imgSource, sentenceElement, sentenceSource){
		
		// Both parameters needed
		if(id !== null && type !== null){
			
			new Ajax.Request(
				'/en/ajax/bookmarkpoint/', {
				parameters: { 
					'id': id,
					'type': type
				},
				onSuccess: function(transport){

					if(transport.responseJSON){

						// Block the Bookmark or RememberMe button
						// depending on 'type'
						AjaxUtils.lightBookmarkButtonsAlt(imgElement, imgSource, sentenceElement, sentenceSource);
					}
				}
			});
			
		}
		
	}



});