


/**
 * @class UrlMaker
 * @desc class to interprate encoded GET, and decode some string arrays into GET
 * @author Janusz Rygal
 * @date 09.07.2008
 */
function UrlMaker(){
		  /**how much should be chars shifted*/
		  var shift 			= 1;	  
		  /**location*/
		  var location 			= new String();
		  /**basic location without GET array*/
		  var basicLocation		= new String();
		  /**GET part of the location*/
		  var getLocation       = new String();
		  /**encoded GET array*/
		  var encodedGetArray   = null;  
		  /**decoded GET array*/
		  var decodedGetArray   = null;
		  /**domain*/
		  var domain			= null;
		  
		 
		  /**
		   * method to get basic url
		   */
		   this.getBasicUrl = function(){
		   	 return basicLocation;
		   }
		  
		  /**
           * shift setter
		   */			
		  this.setShift = function(shift_){
		  		shift = shift_; 
		  }
		  
		  /**
		   * shift getter
		   */	
		  this.getShift = function(){
		  		return shift;
		  }
		  
		  /**
		   * domain getter
		   */
		  this.getDomain = function(){
		  		return domain;
		  } 
		  
		  /**
		   * method to get element by name
		   * @param name of the element to find
		   */
		  this.getElementByName = function(name_){
		  	  if(!checkName(name_))
		  	  	return null;
		  	  return decodedGetArray[name_];	
		  } 
		  
		  /**
		   * method to get element by index
		   * @param the index to find
		   */
		  this.getElementByIndex = function(index_){
				if(!checkIndex(index_))
					return null;
					
				iterNum = 0;	
		  		for(i in decodedGetArray){
		  			if(iterNum == index_)
		  				return decodedGetArray[i];	
		  			++iterNum;	
		  		}
		  		return null;	
		  }
		  
		  this.isGetSet = function(){
		  	if(decodedGetArray == null)
		  		return false;
		  	return true;
		  }
		  
		  /**
		   * method to decode utf 8
		   * @param string to convert
		   * @return string
		   */
		  this.decodeUtf8 = function(utftext){
		        var string = "";
		        var i = 0;
		        var c = c1 = c2 = 0;
		        while ( i < utftext.length ) {
		            c = utftext.charCodeAt(i);
		            if (c < 128) {
		                string += String.fromCharCode(c);
		                i++;
		            }
		            else if((c > 191) && (c < 224)) {
		                c2 = utftext.charCodeAt(i+1);
		                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
		                i += 2;
		            }
		            else {
		                c2 = utftext.charCodeAt(i+1);
		                c3 = utftext.charCodeAt(i+2);
		                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
		                i += 3;
		            }	
		        }
		        return string;
		    }
		    
		  /**
		   * method to encode utf 8
		   * @param string to convert
		   * @return string
		   */
		  this.encodeUtf8 = function(string) {
				        string = string.replace(/\r\n/g,"\n");
				        var utftext = "";
				        for (var n = 0; n < string.length; n++) {
				            var c = string.charCodeAt(n);
				
				            if (c < 128) {
				                utftext += String.fromCharCode(c);
				            }
				            else if((c > 127) && (c < 2048)) {
				                utftext += String.fromCharCode((c >> 6) | 192);
				                utftext += String.fromCharCode((c & 63) | 128);
				            }
				            else {
				                utftext += String.fromCharCode((c >> 12) | 224);
				                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				                utftext += String.fromCharCode((c & 63) | 128);
				            }
				        }
				        return utftext;
		  } 
		  
		 /**
		  *  method to encode the GET 
		  *  @param  associative array of the GET parameters myArray['paramName'] = paramValue
		  *  @return the basicUrl + encoded GET	
		  */ 	  
		  this.encode = function(get_){		
				  if(get_.length == 0)
				  	showError('encode',' Not specified array length')	
							  
		  		  pageLocation = new String(window.location);
		  		  questCharPos = pageLocation.indexOf('?');
		  		  if(questCharPos == -1)
				 	basicLocation = pageLocation;
				  else
				    basicLocation  = pageLocation.substring(0, questCharPos);

				  encodedGetArray = new Array(get_.length);
				  iterNum = 0;	
				  for(iter in get_){
				  		if(get_.length == iterNum)
				  		   break;
				  		   
				  		newKey = new String();
				  		newVal = new String();

				  		//key [begin]				  		 
				  		for(i=0; i<iter.length; ++i){
					  			code    = iter.charCodeAt(i);
					  			code   += shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newKey += char;				
				  		}
					  	//key [end]
					  	
				  		//value [begin]				  		 
				  		for(i=0; i<get_[iter].length; ++i){
					  			code    = get_[iter].charCodeAt(i);
					  			code   += shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newVal += char;				
				  		}
					  	//value [end]		  	
					  	encodedGetArray[newKey] = newVal;					  					  		
				  		iterNum++;
				  }
				  
				  createGetFromArray(encodedGetArray);
				  return (basicLocation + getLocation);
		  }
		  
		  /**
		   * private method to get domain
		   */
		  var findDomain = function(){
		  	var slashCounter = 0;
				  domain = "";
				  for(var i=0; i<basicLocation.length; ++i){
				  	  var chr = basicLocation[i];
				  	  if(chr == "/")
				  	  	++slashCounter;
				  	  if(slashCounter == 3){
				  	  	domain = domain.replace("http://", ""); 
				  	  	break;
				  	  }	
				  	  else	
				  	  	domain += chr;
				  }
				domain.replace("http://", "");   
		  } 
		  
		  /**
		   * method to decode the location
		   * @return the associative array array[GET_key] = GET_value
		   */
		  this.decode = function(){
		  		  pageLocation = new String(window.location);
                  pageLocation = this.encodeUtf8(unescape(pageLocation));
		  		  questCharPos = pageLocation.indexOf('?');
		  		  get          = '';
		  		  if(questCharPos == -1){
				 	basicLocation = pageLocation;
				 	findDomain();
				  	return new Array(0);
				  }	
				  else{
				    basicLocation  = pageLocation.substring(0, questCharPos);
				    findDomain();
				  	get			   = pageLocation.substring(questCharPos+1)
				  } 
				  

				var varArray      = new Array();
				var tmpArray      = new Array(2);
				
				var varArray      = get.split('&');
					
				var varNameArray  = new Array(varArray.length);
				var varValueArray = new Array(varArray.length);
				
				for( var i=0; i<varArray.length; ++i ){
					tmpArray 		 = varArray[i].split('=');
					varNameArray[i]  = tmpArray[0];
					varValueArray[i] = tmpArray[1];	
					
				}
				
				
				encodedGetArray = new Array(varArray.length); 
				for( var i=0; i<varArray.length; ++i ){
					encodedGetArray[varNameArray[i]] = varValueArray[i];
					//alert(varNameArray[i] + ' ' + varValueArray[i]);
				}
				
				
				decodedGetArray = new Array(encodedGetArray.length);
				  iterNum = 0;	
				 
				 for(i=0; i<varNameArray.length; ++i){

				 		newKey = new String();
				  		newVal = new String();
						
				  		//key [begin]				  		 
				  		for(j=0; j<varNameArray[i].length; ++j){
					  			code    = varNameArray[i].charCodeAt(j);
					  			code   -= shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newKey += char;				
				  		}				  		
					  	//key [end]
					  	
					  	encodedGetArray[varNameArray[i]] = decodeURIComponent(encodedGetArray[varNameArray[i]]);
				  		//value [begin]				  		 
				  		for(j=0; j<encodedGetArray[varNameArray[i]].length; ++j){
					  			code    = encodedGetArray[varNameArray[i]].charCodeAt(j);
					  			code   -= shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newVal += char;				
				  		}
					  	//value [end]		  	
					  	decodedGetArray[newKey] = newVal;
					  	//alert(newKey+"  "+newVal);
				 } 
				/*
				  for(iter in encodedGetArray){
            		if(encodedGetArray.length == iterNum)
				  		   break;
				  		   
				  	alert(iter + ' ' + encodedGetArray[iter] + ' ' + encodedGetArray.length);		   
				  		newKey = new String();
				  		newVal = new String();
						
				  		//key [begin]				  		 
				  		for(i=0; i<iter.length; ++i){
					  			code    = iter.charCodeAt(i);
					  			code   -= shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newKey += char;				
				  		}				  		
					  	//key [end]
					  
				  		//value [begin]				  		 
				  		for(i=0; i<encodedGetArray[iter].length; ++i){
					  			code    = encodedGetArray[iter].charCodeAt(i);
					  			code   -= shift;				  			 
					  			char    = String.fromCharCode(code);
					  			newVal += char;				
				  		}
					  	//value [end]		  	
					  	decodedGetArray[newKey] = newVal;	
					  					  					  		
				  		iterNum++;
				  }
				  */
			 return decodedGetArray;	    
		  }

		  /**
		   * method to check the name
		   * @param name_ the name to check
		   * @return boolean if ok then true else false
		   * @private
		   */
		  function checkName(name_){
		  		if(decodedGetArray == null){
		  			showError('getElementByName', 'Undefined decode array');
		  			return false;
		  		}	
		  		if(!decodedGetArray[name_]){
		  			showError('getElementByName', 'Element with name '+name_+' does not exist!');
		  			return false;
		  		}	
		  		return true;	
		  } 

		  
		  /**
		   * method to check the index
		   * @param index_ the index to check
		   * @return boolean if ok then true else false
		   * @private
		   */
		  function checkIndex(index_){
		  		if(decodedGetArray == null){
		  			showError('getElementByIndex', 'Undefined decode array');
		  			return false;
		  		}	
		  		if(index_ >= decodedGetArray.length){
		  			showError('getElementByIndex', 'Index greater then size of the decode array');
		  			return false;
		  		}	
		  		if(index_	< 0){
		  			showError('getElementByIndex', 'Index must be greater or equal zero');
		  			return false;
		  		}
		  		return true;	
		  } 
		  
		  /**
		   * method to create implode GET from associative array
		   * @param array_ array with GET options
		   * @private
		   */
		  function createGetFromArray(array_){
		  		if(array_.length == 0)
				  	showError('createGetFromArray',' Not specified array length');
				
			    numIter = 0;
			    getLocation = '';
			    for(iter in array_){
			    	if(numIter == (array_.length - 1)){
			    		getLocation += iter+'='+array_[iter];
			    		break;
			    	}	
			    	getLocation += iter+'='+array_[iter]+'&';	
			    	numIter++;	
			    }

                getLocation = '?'+escape(getLocation);
			      	
		  } 
		  
		  /**
		   * simply method to show errors
		   * @param method - the method where error occured
		   * @param error_ - the error body
		   * @private
		   */
		  function showError(method ,error_){
		  		alert('Error occured in '+method+' method \n '+error_);
		  } 	
}
