﻿// Get-Set-Add Functions //////////////////////////////

function clsURL_setProtocol( strValue ){ this.strProtocol = strValue; }
function clsURL_setHost( strValue ){ this.strHost = strValue; }
function clsURL_setPath( strValue ){ 
	this.strPath = strValue; 
	this.arrPath = strValue.split("/");
	if( this.arrPath[0] == "" ) this.arrPath.shift();
	if( this.arrPath[ this.arrPath.length-1 ] == "" ) this.arrPath.pop();
}
function clsURL_addPath( strValue ){ this.arrPath.push( strValue ); this.strPath = this.arrPath.join("/"); }
function clsURL_setPage( strValue ){ this.strPage = strValue; }
function clsURL_setQueryString( strValue ){ this.strQueryString = strValue; this.setParamsFromQueryString(); }
function clsURL_setParams( mapParams ){	this.mapParams = mapParams; }
function clsURL_setParamNames( arrParamNames ){	this.arrParamNames = arrParamNames; }
function clsURL_setParam( strName , mixValue ){	this.mapParams[ strName ] = mixValue; }
function clsURL_addParam( strName , mixValue ){	this.arrParamNames.push( strName );	this.setParam( strName , mixValue ); }
function clsURL_subParams( strNameList ){	
	arrNames = strNameList.split(",")
	for( intI = 0; intI < arrNames.length; intI++ ){
		for( intY = 0; intY < this.arrParamNames.length; intY++ ){
			if( this.arrParamNames[intY] == arrNames[intY] ){
				this.arrParamNames.splice( intY , 1 );
				delete this.mapParams[ arrNames[intY] ];
			}
		}
	}
}
function clsURL_setLangParamName( strValue ){	this.strLangParamName = strValue; }
function clsURL_setLangParamValue( strValue ){ this.mapParams[ this.strLangParamName ] = strValue; }
function clsURL_getURL(){	return this.getProtocol() + this.getHost() + this.getPath() + this.getPage() + this.getQueryString(); }
function clsURL_getProtocol( strValue ){ return this.strProtocol+"//"; }
function clsURL_getHost( strValue ){ return this.strHost+"/"; }
function clsURL_getPath( strValue ){ return this.arrPath.join("/"); }
function clsURL_getPage( strValue ){ return "/"+this.strPage; }
function clsURL_getQueryString(){
	strQueryString = "";
	if( this.arrParamNames.length > 0 ){
		strQueryString += "?";
		for( intI = 0; intI < this.arrParamNames.length; intI++ ){
			strCParamName = this.arrParamNames[intI];
			strQueryString += strCParamName + "=" + escape(this.mapParams[ strCParamName ]);
			if( intI < this.arrParamNames.length - 1)
				strQueryString += "&";
		}
	}
	return strQueryString;
}

// Retrieve Functions //////////////////////////////

function clsURL_retrieveURL(){ 
	this.strURL = window.location.toString();
	this.arrURL = this.strURL.split("/");
	// protocol
	this.setProtocol( this.arrURL[0] );
	// host
	this.setHost( this.arrURL[2] );
	// path
	for( intI = 3; intI < this.arrURL.length - 1; intI++ )
		this.addPath( this.arrURL[intI] );
	// page
	intLast = this.arrURL.length-1;
	arrPageAndQueryString = this.arrURL[ intLast ].split("?");
	this.setPage( arrPageAndQueryString[0] );
	// querystring
	if( arrPageAndQueryString.length > 1 )
		this.setQueryString( arrPageAndQueryString[1] );
	
	if( 0 ){
		alert( "URL "+this.strURL );
		alert( "Protocol "+this.strProtocol );
		alert( "Host "+this.strHost );
		alert( "Path "+this.strPath );
		alert( "Page "+this.strPage );
		alert( "QS "+this.strQueryString );
	}
}

function clsURL_setParamsFromQueryString(){
	if( this.strQueryString == "" ) return;
	arrParamsWithValues = this.strQueryString.split("&");
	for( intI = 0; intI < arrParamsWithValues.length; intI++ ){
		arrCParam = arrParamsWithValues[intI].split("=");
		this.arrParamNames.push(arrCParam[0]);
		this.mapParams[ arrCParam[0] ] = arrCParam[1];
	}
}

// Go Functions //////////////////////////////

function clsURL_goURL(){
	window.location = this.getURL();
}

function clsURL_goLangURL( strLang ){
	if( this.strURL.indexOf( this.strLangParamName+"=" ) > 1 ){
		this.setLangParamValue( strLang.toLowerCase() );
	} else {
		this.arrPath[0] = strLang;
	}
	this.goURL();
}

// Costruttore //////////////////////////////

function clsURL()
{
	// strings
	this.strURL = "";
	this.strProtocol = "";
	this.strHost = "";
	this.strPath = "";
	this.strPage = "";
	this.strQueryString = "";
	this.strLangParamName = "l";
	this.strLangParamValue = "";
	// arrays
	this.arrURL = new Array();
	this.arrPath = new Array();
	this.arrParamNames = new Array();
	// maps
	this.mapParams = new Object();
	// functions
		// get-set-add
		this.setProtocol = clsURL_setProtocol;
		this.setHost = clsURL_setHost;
		this.setPath = clsURL_setPath;
		this.addPath = clsURL_addPath;
		this.setPage = clsURL_setPage;
		this.setQueryString = clsURL_setQueryString;
		this.setParam = clsURL_setParam;
		this.setParams = clsURL_setParams;
		this.setParamNames = clsURL_setParamNames;
		this.setParamsFromQueryString = clsURL_setParamsFromQueryString;
		this.addParam = clsURL_addParam;
		this.subParams = clsURL_subParams;
		this.setLangParamName = clsURL_setLangParamName;
		this.setLangParamValue = clsURL_setLangParamValue;
		this.getProtocol = clsURL_getProtocol;
		this.getHost = clsURL_getHost;
		this.getPath = clsURL_getPath;
		this.getPage = clsURL_getPage;
		this.getQueryString = clsURL_getQueryString;
		// retrieve
		this.retrieveURL = clsURL_retrieveURL;
		// go
		this.getURL = clsURL_getURL;
		this.goURL = clsURL_goURL;
		this.goLangURL = clsURL_goLangURL;
		
		this.retrieveURL();
}
