
/* ******************************* */
/*          REWRITE MANAGER		   */
/* ******************************* */
	
function RewriteManager() {
	// alert("kiki");
}

RewriteManager.prototype.execute = function () {
	if( i_campaign = this._get_campaign() ){
		// this._i_ref( "kaka" );
		this._i_ref( i_campaign );
	}
};

RewriteManager.prototype._i_ref = function ( campaign ) {
	this._set_anchors( campaign );
	this._set_forms( campaign );
};

RewriteManager.prototype._set_forms = function ( campaign ) {
	var input = document.getElementsByTagName("input");
	
	for(var index = 0; index < input.length; index++ ){
		if( input[index].name == "i_campaign"  ){
			input[index].value = campaign;
		}
	}
};


RewriteManager.prototype._set_anchors = function ( campaign ) {
	var a = document.getElementsByTagName("a");
	
	var pattern = new RegExp("i_campaign=\\d+");
	for(var index = 0; index < a.length; index++ ){
		var result = a[ index ].href.replace(pattern, "i_campaign=" + campaign);
		
		a[ index ].href = result;
	}
};


RewriteManager.prototype._get_campaign = function () {
	var neoUrl = new RegExp("-(\\d+)\\.html$");
	var url = window.location.href.split('?');

	// new style url 
	if( result = url[0].match( neoUrl ) ){
		return result[1];
	}
	// old style url 
	else {
		if( url.length == 2 ){
			var args = url[1].split('&');
			
			var pattern = new RegExp("i_campaign=(\\d*)");
			for(var index = 0; index < args.length; index++ ){
				if( result = args[index].match( pattern ) ){
					return result[1];
				}
			}
		}
	}
	
	return false;
};


