/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function urldecode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}


function addFormField(fName,fValue) {
  var myFormElement = document.getElementById('myPostForm');
  var newField = document.createElement('input');
  newField.setAttribute('type','hidden');
  newField.setAttribute('name',fName);
  newField.setAttribute('value',fValue);
  myFormElement.appendChild(newField);
}

function parseAnchorTags(frameId) {
// 	var anchorTags = document.getElementsByTagName("a");
// 	for (var i = 0; i < anchorTags.length; i++)
// 	{
// 		if(anchorTags[i].href.substr(0,10) != 'javascript') {
// 			anchorTags[i].setAttribute('rev',anchorTags[i].href);
// 			anchorTags[i].setAttribute('id','F'+frameId+'G2PT'+i);
// 			anchorTags[i].href = 'javascript:goToAddr(\'F'+frameId+'G2PT'+i+'\')';
// 		}
// 	}
}

function goToAddr(aTagId) {
	var frameId = aTagId.substr(1,9);
	
	if (frameId != "mainFrame") {
		var oIframe = document.getElementById(frameId);
		var oDoc = oIframe.contentWindow || oIframe.contentDocument;
		if (oDoc.document)
		oDoc = oDoc.document;
	} else {
		oDoc = document;
	}
	
	var aTag = oDoc.getElementById(aTagId);
	var items = parseUri(aTag.rev);

	var postform = document.createElement('form');
	postform.setAttribute('method','POST');
	postform.setAttribute('id','myPostForm');
	postform.setAttribute('action',items.protocol+'://'+items.host+':'+items.port+items.path);
	postform.setAttribute('target',aTag.target);

	document.getElementById('get2post').appendChild(postform);
	
	for (var i in items.queryKey) {
		addFormField(i,urldecode(items.queryKey[i]));
	}
	
	postform.submit();
	return true;
}