/**
 * This class represent an url.
 * Compatible : IE, Firefox, Safari, Opera
 * @package DHL::NET
 * @author Inoveo technologie inc.
 */

function CM_URL (asUrl) {
	
	this.strOriginalUrl;						// http://www.domain.com/path/to/file.php?id=44
	
	this.strProtocol;							// HTTP, FTP, ...
	this.strUserName;	
	this.strPassword;
	this.strHost;								// www.domaine.com
	this.intPort;								// HTTP = 80, FTP = 21
	this.strPath;								// /path/to/file.php
	this.arrAttribute = new Object();	// id=44
	this.strFrangment = '';
	this.strModel = '';
	this.arrSpecialAttribute = new Object(); // id-1/position-12 

	CM_URL.prototype.parseUrl = function parseUrl(asUrl) {
		this.strOriginalUrl = asUrl;

		re = new RegExp("^(([a-zA-Z]+)://((([a-zA-Z.]+):)?([a-zA-Z.]+)@)?([a-zA-Z.]+)(:([0-9]*))?)?([^?#]*)[?]?([^#$]*)?(#([a-zA-Z0-9]*))?$");
		arrUrl = re.exec(asUrl);

		this.strProtocol = (arrUrl[2] ? arrUrl[2] : undefined);
		this.strUserName = (arrUrl[5] ? arrUrl[5] : undefined);
		this.strPassword = (arrUrl[6] ? arrUrl[6] : undefined);
		if (this.strPassword && !this.strUserName) {
			this.strUserName = this.strPassword;
			this.strPassword = undefined;
		}

		this.strHost = (arrUrl[7] ? arrUrl[7] : undefined);
		this.intPort = (arrUrl[9] ? parseInt(arrUrl[9]) : undefined);
		this.strPath = (arrUrl[10] ? arrUrl[10] : undefined); 
		
		if(this.strPath != "") {
			arrPath = this.strPath.split('/');
			this.strModel = "";
			this.strPath = "";
			var intStartExtra = 3;
			if(arrPath[1].indexOf('.') == -1 || !arrPath[2]) {
				this.strModel = arrPath[1];
				if(arrPath[2]) {
					this.strPath = arrPath[2].replace("-","/");
				}
			}
			else {
				this.strModel = "";
				this.strPath = arrPath[1].replace("-","/");
				intStartExtra = 2;
			}
			
			for(intI = intStartExtra; intI < arrPath.length; intI++) {
				arrParam = arrPath[intI].split('-');
				this.arrSpecialAttribute[arrParam[0]] = arrParam[1];
			}
		}

		var strAttr = arrUrl[11];
		var arrAttr = new Array();

		if (strAttr != undefined) { arrAttr = strAttr.split('&'); }

		var strReg = /\+/g;
		for(i in arrAttr) if (arrAttr[i]) {
			arrElem = arrAttr[i].split('=');
			this.arrAttribute[unescape(arrElem[0]).replace(strReg, ' ')] = (unescape(arrElem[1]).replace(strReg, ' '));
		}
		
		this.strFragment = arrUrl[13];

	}
	
	
	
	CM_URL.prototype.setProtocol = function setProtocol(asProtocol) { 
		this.strProtocol = asProtocol; 
	}

	CM_URL.prototype.getProtocol = function getProtocol() {
		return this.strProtocol; 
	}

	CM_URL.prototype.setUserName = function setUserName(asUserName) {
		this.strUserName = asUserName;	
	}

	CM_URL.prototype.getUserName = function getUserName() { 
		return this.strUserName; 
	}

	CM_URL.prototype.setPassword = function setPassword(asPassword) {
		this.strPassword = asPassword;	
	}

	CM_URL.prototype.getPassword = function getPassword() { 
		return this.strPassword; 
	}

	CM_URL.prototype.setHost = function setHost(asHost) { 
		this.strHost = asHost; 
	}

	CM_URL.prototype.getHost = function getHost() {
		return this.strHost; 
	}

	CM_URL.prototype.setPort = function setPort(aiPort) {
		this.intPort = aiPort; 
	}

	CM_URL.prototype.getPort = function getPort(bProtocolBase) { 
		if (this.intPort) { return this.intPort; }
		var iReturnPort = undefined;
		if (bProtocolBase == true) {
			switch(this.strProtocol) {
			case 'http':
				iReturnPort = 80;
			break;
			case 'https':
				iReturnPort = 443;
			break;
			case 'ftp':
				iReturnPort = 21;
			break;
			}
		}
		return iReturnPort; 
	}

	CM_URL.prototype.setPath = function setPath(asPath) { this.strPath = asPath; }

	CM_URL.prototype.setRelativePath = function setRelativePath(asPath) {
		this.strPath = this.strPath.substring(0, this.strPath.lastIndexOf('/'));
		while (asPath.length >= 3 && asPath.substr(0, 3) == '../') {
			this.strPath = this.strPath.substring(0, this.strPath.lastIndexOf('/'));
			asPath = asPath.substring(3, asPath.length);
		}
		this.strPath = this.strPath + '/' + asPath;
	}

	CM_URL.prototype.getPath = function getPath() {	return this.strPath; }

	CM_URL.prototype.setAttribute = function setAttribute(sName, sValue) { 
		this.arrAttribute[sName] = sValue; 
	}
	
	CM_URL.prototype.clearAttribute = function clearAttribute() {
		this.arrAttribute = new Object();
	}
	
	CM_URL.prototype.removeSpecialAttribute = function removeSpecialAttribute(strName) {
		this.arrSpecialAttribute[strName] = null;
	}
	
	CM_URL.prototype.setSpecialAttribute = function setSpecialAttribute(sName, sValue) { 
		this.arrSpecialAttribute[sName] = sValue; 
	}

	CM_URL.prototype.clearSpecialAttribute = function clearSpecialAttribute() {
		this.arrSpecialAttribute = new Object();
	}

	CM_URL.prototype.getAttribute = function getAttribute(sName) { 
		return this.arrAttribute[sName]; 
	}
	
	CM_URL.prototype.getAllAttribute = function getAllAttribute() {
		var oRet = new Object();
		var bAttributes = false;
		for(i in this.arrAttribute) {
			if (i && this.arrAttribute[i]) {
				bAttributes = true;
				oRet[i] = this.arrAttribute[i];
			}
		}

		return (bAttributes ? oRet : undefined);
	}

	CM_URL.prototype.removeAttribute = function removeAttribute(strAttribute) {
		if(this.arrAttribute[strAttribute]) {
			var arrReplace = new Object();
			for(i in this.arrAttribute) {
				if(i != strAttribute) {
					arrReplace[i] = this.arrAttribute[i];
				}
			}
			this.arrAttribute = arrReplace;
		}
	}

	CM_URL.prototype.setFragment = function setFragment(strFragment) {
		this.strFragment = strFragment;
	}

	CM_URL.prototype.getFragment = function getFragment() {
		return this.strFragment;
	}
	
	CM_URL.prototype.setModel = function setModel(objModel) {
		this.strModel = objModel.getNickname();
	}
	
	CM_URL.prototype.setNickname = function setNickname(strNickname) {
		this.strModel = strNickname;
	}
	
	CM_URL.prototype.getNickname = function getNickname() {
		return this.strModel;
	}
		
	CM_URL.prototype.removeFragment = function removeFragment() {
		this.strFragment = '';
	}

	CM_URL.prototype.toString = function toString() {

		var sUrl = '';
		var sAttribute = '';
		
		if(this.strModel) {
			var strPath = (this.strPath ? this.strPath.replace('/','-'): '');
		}
		else {
			var strPath = this.strPath;
		}
		
		if(strPath.indexOf('.') > 0) {
			strPath = strPath.substr(0, strPath.indexOf('.'));
		}
		strSpecialAttribute = "";


		sUrl += (this.strProtocol ? this.strProtocol + '://' : '');
		sUrl += (this.strUserName ? this.strUserName : '');
		sUrl += (this.strPassword ? (this.strUserName ? ':' : '') + this.strPassword : '');
		sUrl += (this.strUserName || this.strPassword ? '@' : '') ;
		sUrl += (this.strHost ? this.strHost : '');
		sUrl += (this.intPort ? ':' + this.intPort : '');
		sUrl += (this.strModel ? '/' + this.strModel + '/': '');
		sUrl += (strPath ?  strPath + '/': (!this.strModel? '/': ''));
		
		for(strKey in this.arrSpecialAttribute) {
			if(
				this.arrSpecialAttribute[strKey] != null &&
				typeof this.arrSpecialAttribute[strKey] != 'undefined'
			) { 
				strSpecialAttribute += strKey + "-" + this.arrSpecialAttribute[strKey] + '/';
			}
		}
		sUrl += (strSpecialAttribute ? strSpecialAttribute: ''); 
		

		
		for(i in this.arrAttribute) {
			sAttribute += (sAttribute ? '&' : '');
			sAttribute += escape(i) + '=' + escape(this.arrAttribute[i]);
		}
		sUrl += (sAttribute ? '?' + sAttribute : '');
		sUrl += (this.strFragment != '' && typeof this.strFragment != 'undefined' ? '#' + this.strFragment : ''); 

		return sUrl; 
	}

	/**
	 * Retrieve an url from current document.
	 * @return object CM_URL
	 */
	if (asUrl != undefined) {
		this.parseUrl(asUrl);
	}
}

CM_URL.getCurrent = function getCurrent() {
	var oUrl = new CM_URL();
	oUrl.parseUrl(document.location.href);
	return oUrl;
}
