/**
 * The encode function encodes a string as a component of a URI.
 *
 * @author: Google developers
 */
function encode(param){
    return encodeURIComponent(param).replace(/%20/g, "+").replace(/%3A/g, ":");
}

/**
 * Convert relative form's action url to absolute one
 * @package Pone
 * @since   August 05, 2008
 * @author  pcdinh
 */
function Pone(){}
Pone.baseUrl = function() {
	return jQuery('base')[0].href;
};
Pone.getAbsoluteActionUrl = function(formObject) {
    var actionUrl = formObject.action;	
	var baseUrl = Pone.baseUrl();
    // actionUrl contains baseUrl
    var overlap = actionUrl.indexOf(baseUrl); 
    
    if (overlap != -1) {
        baseUrl = "";
    }
    
    // Remove trailing forward slash in url (if any)
    if (baseUrl.substr(baseUrl.length - 1) == '/' && actionUrl.charAt(0) == '/') {
        // Base url should be always trailed by a forward slash
        actionUrl = actionUrl.substr(1);
    } else if (baseUrl != "" && baseUrl.charAt(baseUrl.length - 1) != '/' && actionUrl.charAt(0) != '/') {
        baseUrl = baseUrl + '/';
    }
        
    return baseUrl + actionUrl;
};
/**
 * Build resful url from form properties
 * This function is part of Pone framework
 * @version 1.0 
 * @since   August 14, 2008          
 * @author  pcdinh 
 */
Pone.buildRestfulUrl = function(form){
  var value;
  var url = '';
  var baseFormUrl = Pone.getAbsoluteActionUrl(form);
    
  jQuery(':input', form).each(function(index, child){
    if (child.type == 'radio' || child.type == 'checkbox') {
      if (child.checked == true) {
        value = child.value;
      }
    } else {
      if (child.multiple == true) {
	  	value = [];
        for (var i = 0, length = child.options.length, c = 0; i < length; i++) {
          if (child.options[i].selected == true) {
            value[c]     = child.options[i].value;  
            c++;         
          }
        }
      } else {
        value = child.value; // 'password', 'textarea', 'hidden', 'text'
      }
    }
        
    if (typeof value == 'string') {
      // remove leading/trailing whitespaces
      value.replace(/^\s+/, '').replace(/\s+$/, '');
      // build restful key/value url component
      url += child.name + '/' + encode(value) + '/';
      value = null;
    } else if (typeof value == 'object' && value.constructor == Array) {
      for (var i = 0, length = value.length; i < length; i++) {
        // remove leading/trailing whitespaces
        value[i].replace(/^\s+/, '').replace(/\s+$/, '');
        // build restful key/value url component
        url += child.name + '/' + encode(value[i]) + '/';         
      }
      value = null;
    }
  }) 
    
  if (baseFormUrl.substr(baseFormUrl.length - 1) == '/' && url.charAt(0) == '/') {
    url = url.substr(1);
  } else if (baseFormUrl != "" && baseFormUrl.charAt(baseFormUrl.length - 1) != '/' && url.charAt(0) != '/') {
    baseFormUrl = baseFormUrl + '/';
  }    
  // redirect url
  return baseFormUrl + url;
};

/**
 * Convert traditional GET urls into restful urls
 * This function is part of Pone framework
 * @version 1.2 Revised August 05, 2008
 * @since   December 07, 2007          
 * @see     Pone_View::addGetSubmitAdapter()
 * @author  pcdinh 
 * @example 
 *          jQuery(document).bind('ready', function() {
 *              jQuery('.searchForm').bind('submit', normalizeQuery);
 *          });
 */
function normalizeQuery(){
  var value;
  var url = '';
  var baseFormUrl = Pone.getAbsoluteActionUrl(this);
    
  jQuery(':input', this).each(function(index, child){
    if (child.type == 'radio' || child.type == 'checkbox') {
      if (child.checked == true) {
        value = child.value;
      }
    } else {
      if (child.multiple == true) {
	  	value = [];
        for (var i = 0, length = child.options.length, c = 0; i < length; i++) {
          if (child.options[i].selected == true) {
            value[c]     = child.options[i].value;  
            c++;         
          }
        }
      } else {
        value = child.value; // 'password', 'textarea', 'hidden', 'text'
      }
    }
        
    if (typeof value == 'string') {
      // remove leading/trailing whitespaces
      value.replace(/^\s+/, '').replace(/\s+$/, '');
      // build restful key/value url component
      url += child.name + '/' + encode(value) + '/';
      value = null;
    } else if (typeof value == 'object' && value.constructor == Array) {
      for (var i = 0, length = value.length; i < length; i++) {
        // remove leading/trailing whitespaces
        value[i].replace(/^\s+/, '').replace(/\s+$/, '');
        // build restful key/value url component
        url += child.name + '/' + encode(value[i]) + '/';         
      }
      value = null;
    }
  }) 
    
  if (baseFormUrl.substr(baseFormUrl.length - 1) == '/' && url.charAt(0) == '/') {
    url = url.substr(1);
  } else if (baseFormUrl != "" && baseFormUrl.charAt(baseFormUrl.length - 1) != '/' && url.charAt(0) != '/') {
    baseFormUrl   = baseFormUrl + '/';
  }    
  // redirect url
  location.href = baseFormUrl + url;
  return false;
}
