
<!--
/*
Ce code est (c) Adeixis. Toute reproduction, meme partielle, sans autorisation d'Adeixis est strictement interdite et fera l'objet de poursuites judiciaires.
This code is (c) Adeixis. Any copy, even in parts, without Adeixis's authorization is strictly forbidden and will lead to prosecution.
--
Certaines parties/Some parts (c) Simon Willison - Stewart Rosenberger.
*/

function toggleMoreInfo(which) {
	availableBlocks = document.getElementsBySelector('table.moreInfo');
	for(i=0;  i<availableBlocks.length; i++) {
		if (availableBlocks[i].id == which) {
			try {
				availableBlocks[i].style.display = "table";
			} catch(error) {
				// MSIE is buggy (again) and needs 'block' instead of standard-compliant 'table-row'
				availableBlocks[i].style.display = 'block';
			}
		} else availableBlocks[i].style.display = "none";
	}
	if (which == "") {
			document.getElementById('dxdiagAndMsinfo').style.display = "none";
	} else if (which == "account") {
			document.getElementById('dxdiagAndMsinfo').style.display = "none";
			document.getElementById('tr-os').style.display = "none";
			document.getElementById('avatarname').className = "";
			document.getElementById('os').className = "";
	} else {
			try {
				document.getElementById('dxdiagAndMsinfo').style.display = "table";
				document.getElementById('tr-os').style.display = "table-row";
			} catch(error) {
				document.getElementById('dxdiagAndMsinfo').style.display = "block";
				document.getElementById('tr-os').style.display = "block";
			}
			document.getElementById('avatarname').className = "mandatory";
			document.getElementById('os').className = "mandatory";
	}
}

function toggleOther(moreinfoId, which) {
	otherObj = document.getElementById(moreinfoId+"-other");
	if (which == "other") {
		try {
			otherObj.style.display = "table-row";
		} catch(error) {
			otherObj.style.display = 'block';
		}
	} else
		otherObj.style.display = "none";
}

function validate(form) {
	mandatoryBlocks = document.getElementsBySelector('table#main .mandatory');
	validated = true;
	for(i=0;  i<mandatoryBlocks.length; i++) {
		if (mandatoryBlocks[i].value == "") {
			validated = false;
			mandatoryBlocks[i].className = 'mandatory missing';
		} else {
			mandatoryBlocks[i].className = 'mandatory';
		}
	}
	issue = document.getElementById("issue").value;
	if (issue != "") {
		mandatoryBlocks = document.getElementsBySelector('table#'+issue+' .mandatory');
		for(i=0;  i<mandatoryBlocks.length; i++) {
			if (mandatoryBlocks[i] && mandatoryBlocks[i].value == "" && mandatoryBlocks[i].value != undefined)  {
				validated = false;
				mandatoryBlocks[i].className = 'mandatory missing';
			} else {
				mandatoryBlocks[i].className = 'mandatory';
			}
		}
	}
	if (validated == false) {
		alert("Some mandatory fields are unfilled. Please go through the page and check for red fields.");
		return false;
	} else {
	return true;
	}
}

function loader() {
	issue = document.getElementById('issue').value;
	toggleMoreInfo(issue);
	try {
		subIssueObj = document.getElementById(issue+'[issue]');
		if (subIssueObj) toggleOther(issue, subIssueObj.value);
	} catch(error) {
	}
}

document.getElementsBySelector = function(selector)
{
	var tokens = selector.split(' ');
	var currentContext = new Array(document);
	for(var i=0;i<tokens.length;i++)
	{
		token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');
		if(token.indexOf('#') > -1)
		{
			var bits = token.split('#');
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if(tagName && element.nodeName.toLowerCase() != tagName)
				return new Array();
			currentContext = new Array(element);
			continue;
		}

		if(token.indexOf('.') > -1)
		{
			var bits = token.split('.');
			var tagName = bits[0];
			var className = bits[1];
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
				if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
	    }

		if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/))
		{
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
	        	if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction;
			switch(attrOperator)
			{
				case '=':
					checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
					break;
				case '~':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
					break;
				case '|':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
					break;
				case '^':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
					break;
				case '$':
					checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
					break;
				case '*':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
					break;
				default :
					checkFunction = function(e) { return e.getAttribute(attrName); };
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(checkFunction(found[k]))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
		}

		tagName = token;
		var found = new Array;
		var foundCount = 0;
		for(var h=0;h<currentContext.length;h++)
		{
			var elements = currentContext[h].getElementsByTagName(tagName);
			for(var j=0;j<elements.length; j++)
				found[foundCount++] = elements[j];
		}

		currentContext = found;
	}

	return currentContext;
}
