/*#############################################################
Name: Niceforms
Version: 0.9
Author: Lucian Slatineanu
URL: http://www.badboy.ro/

Feel free to use and modify but please provide credits.
#############################################################*/

//global variables that can be used by all the functions on this page.
var selectId;
var selects;
var selectWidth;
var selectText;
var i;

//this function runs when the page is loaded so put all your other onload stuff in here too.
function initSelect(selectAreaEl,selectEl,selectTextEl,i) {
	selectText = selectTextEl;
	//check if styles are enabled and only then start replacing elements
	if (document.getElementById('formStyle'))
	{
		if(findPosX(document.getElementById('formStyle')) == -999) {
			replaceSelects(selectAreaEl,selectEl,i);
		}
	}
}

function replaceSelects(selectAreaEl,selectEl,i) {
	//get all the select fields on the page
	selectId = document.getElementById(selectAreaEl);
    selects = selectId.getElementsByTagName('select');
	selectWidth = document.getElementById(selectEl).offsetWidth;

	//create and build div structure
	selectArea = document.createElement('div');
	left = document.createElement('div');
	right = document.createElement('div');
	center = document.createElement('div');
	button = document.createElement('a');
	text = document.createTextNode(selectText);
	center.id = "mySelectText"+i;
	button.href="javascript:showOptions("+i+")";
	selectArea.style.width = selectWidth + 30 + "px";
	selectArea.className = "selectArea";
	left.className = "left";
	right.className = "right";
	center.className = "center";
	center.style.width = selectWidth -10 + "px";
	right.appendChild(button);
	center.appendChild(text);
	selectArea.appendChild(left);
	selectArea.appendChild(right);
	selectArea.appendChild(center);
	
	//hide the select field
	selects[0].style.display='none'; 
	
	//insert select div
	selects[0].parentNode.insertBefore(selectArea, selects[0]);
	
	//build & place options div
	var optionsDiv = document.createElement('div');
	var optionsDiv2 = document.createElement('div');
	optionsDiv.className = "optionsDivInvisible";
	optionsDiv.id = "optionsDiv"+i;
	optionsDiv.style.width = selectWidth + 18 + 'px';
	optionsDiv.style.left = findPosX(selectArea) + 'px';
	optionsDiv.style.top = findPosY(selectArea) + 17 + 'px';
	optionsDiv2.className = "optionsIn";
	optionsDiv.appendChild(optionsDiv2);
	//get select's options and add to options div
	for(var j=0; j < selects[0].options.length; j++) {
		var optionHolder = document.createElement('p');
		var optionLink = document.createElement('a');
		var optionTxt = document.createTextNode(selects[0].options[j].text);
		optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[0].id+"',"+j+","+i+");";
		optionLink.appendChild(optionTxt);
		optionHolder.appendChild(optionLink);
		optionsDiv2.appendChild(optionHolder);
	}
	
	//insert options div
	document.getElementById(selectAreaEl).appendChild(optionsDiv);
}

function showOptions(g) {
		elem = document.getElementById("optionsDiv"+g);
		if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";}
		else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}
}

function selectMe(selectFieldId,linkNo,selectNo) {
	//feed selected option to the actual select field
	selectField = document.getElementById(selectFieldId);
	for(var k = 0; k < selectField.options.length; k++) {
		if(k==linkNo) {
			selectField.options[k].selected = "selected";
		}
		else {
			selectField.options[k].selected = "";
		}
	}
	//show selected option
	textVar = document.getElementById("mySelectText"+selectNo);
	var newText = document.createTextNode(selectField.options[linkNo].text);
	textVar.replaceChild(newText, textVar.childNodes[0]);
	//window.open(selectField.options[linkNo].value);
	textVar.style.background = "transparent";
	if (selectNo=="0") {
		window.open(selectField.options[linkNo].value);
	} else if (selectNo=="1") {
		window.open(selectField.options[linkNo].value,"","width=1024,height=743");
	} else if (selectNo=="2") {
		location.href=selectField.options[linkNo].value;
	}
}

function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {
		posTop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {
		posLeft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return posLeft;
}
