/*#############################################################
Name: Niceforms
Version: 0.9
Author: Lucian Slatineanu
URL: http://www.badboy.ro/
 
Feel free to use and modify but please provide credits.
 
Modified by: Basti
URL: http://www.arraystudio.com
#############################################################*/
 //global variables that can be used by all the functions on this page.
var selects;
var inputs;
var hovers = new Array();
var buttons = new Array();
var selectText = "please select";
//this function runs when the page is loaded so put all your other onload stuff in here too.
function init() { 
 //check if styles are enabled and only then start replacing elements
 if(findPosX(document.getElementById('stylesheetTest')) == -999) {
  replaceSelects();
 }
}
function replaceSelects() {
 //get all the select fields on the page
    selects = document.getElementsByTagName('select');
 
 //cycle trough the select fields
   for(var i=0; i < selects.length; i++) { 
	   	if(selects[i].id=="country" ||
			selects[i].id=="bill_state_id" ||
			selects[i].id=="ship_country_id" ||
			selects[i].id=="ship_state_id" ||
			selects[i].id=="emailid" ||
			selects[i].id=="sStore" || 
			selects[i].id=="sTime" || 
			selects[i].id=="sMonth" || 
			selects[i].id=="sDay" || 
			selects[i].id=="sYear"
			){
			continue;
		}
	  //create and build div structure
	  var selectArea = document.createElement('div');
	  var left = document.createElement('div');
	  var right = document.createElement('div');
	  var center = document.createElement('div');
	  var button = document.createElement('a');
	  
	  //start basti
	  //var text = document.createTextNode(selectText);
	  var text = document.createTextNode(selects[i].options[selects[i].selectedIndex].text);
	  //end basti
	  
	  center.id = "mySelectText"+i;
	  //button.href="javascript:showOptions("+i+")";
	  if(selects[i].id=="Sort" || selects[i].id=="cc_type" || selects[i].id=="cc_month" || selects[i].id=="cc_year" || selects[i].id=="cc_start_month" || selects[i].id=="cc_start_year"){
		 selectArea.className = "selectAreawhite";
		 left.className = "left1";
		 right.className = "right1";
		 center.className = "center1";
	  }
	  else
	  {
		selectArea.className = "selectArea";
		left.className = "left";
		right.className = "right";
		center.className = "center";
	  }
	  
	  right.appendChild(button);
	  center.appendChild(text);
	  selectArea.appendChild(left);
	  selectArea.appendChild(right);
	  selectArea.appendChild(center);
  
  //start basti
  if(document.addEventListener)
  {
   //these are for Mozilla
   eval("selectArea.addEventListener('click', function (e) { e.stopPropagation(); if(isAnyOptionVisible() != "+i+" && isAnyOptionVisible() >= 0) { showOptions(isAnyOptionVisible());} showOptions("+i+"); }, true)");
   document.addEventListener('click', function (e) { showOptions(isAnyOptionVisible());}, false);
  }
  else if(document.attachEvent)
  {
   //these are for IE
   eval("selectArea.attachEvent('onclick', function (e) { if (!e) var e = window.event; e.cancelBubble = true; if(isAnyOptionVisible() != "+i+" && isAnyOptionVisible() >= 0) { showOptions(isAnyOptionVisible());} showOptions("+i+"); } )");
   document.attachEvent('onclick', function (e) { showOptions(isAnyOptionVisible());});
  }
  //end basti
  
  //hide the select field
        selects[i].style.display='none';
 
  //insert select div
  selects[i].parentNode.insertBefore(selectArea, selects[i]);
 
  //build & place options div
  var optionsDiv = document.createElement('div');
  if(selects[i].id=="Sort" || selects[i].id=="cc_type" || selects[i].id=="cc_month" || selects[i].id=="cc_year" || selects[i].id=="cc_start_month" || selects[i].id=="cc_start_year"){
   optionsDiv.className = "optionsDivInvisiblenew";
  }
  else
  {
    optionsDiv.className = "optionsDivInvisible";
  }
  optionsDiv.id = "optionsDiv"+i;
  optionsDiv.style.left = findPosX(selectArea) + 'px';
  optionsDiv.style.top = findPosY(selectArea) + 22 + 'px';
 
 
  //get select's options and add to options div
  for(var j=0; j < selects[i].options.length; j++) {
   var optionHolder = document.createElement('p');
   var optionLink = document.createElement('a');
   var optionTxt = document.createTextNode(selects[i].options[j].text);
 
   //start basti
   //optionLink.href = "javascript:showOptions("+i+"); selectMe('"+selects[i].id+"',"+j+","+i+");";
   optionLink.href = "javascript:selectMe('"+selects[i].id+"',"+j+","+i+");";
   //end basti
   
   optionLink.appendChild(optionTxt);
   optionHolder.appendChild(optionLink);
   optionsDiv.appendChild(optionHolder);
  }
 
  //insert options div
  document.getElementsByTagName("body")[0].appendChild(optionsDiv);
 } 
} 
function showOptions(g) {
  elem = document.getElementById("optionsDiv"+g);
  if(!elem)
  {
   return;
  }
 
  if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";}
  else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}
  
  if(elem.className=="optionsDivInvisiblenew") {elem.className = "optionsDivVisiblenew";}
  else if(elem.className=="optionsDivVisiblenew") {elem.className = "optionsDivInvisiblenew";}
}
 
//start basti
function isAnyOptionVisible()
{
 var g=0;
 
 while(elem = document.getElementById("optionsDiv"+g))
 {
  if(elem.className=="optionsDivVisible" || elem.className=="optionsDivVisiblenew" )
   return g;
  g++;
 }
 
 return -1;
}
//end basti
 
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]);
if(selectFieldId=='Sort'){
document.getElementById('sortsearch').submit();
}
}
 
function findPosY(obj) {
 var posTop = 0;
 while (obj.offsetParent) {
  posTop += obj.offsetTop;
  obj = obj.offsetParent;
 }
 return posTop;
}
function findPosX(obj) {
 var posLeft = 0;
if(obj)
{
 while (obj.offsetParent) {
  posLeft += obj.offsetLeft;
  obj = obj.offsetParent;
 }
}
 return posLeft;
}
window.onload = init;