var browser=navigator.appName;
var IE = false;
if(browser == 'Microsoft Internet Explorer') { IE = true; }

/*
if (window.attachEvent) {window.attachEvent('onload', checkSelectBoxes);}
else if (window.addEventListener) {window.addEventListener('load', checkSelectBoxes, false);}
else {document.addEventListener('load', checkSelectBoxes, false);} 
*/

/*
window.onload = function() {
	checkSelectBoxes();
}
*/

function checkSelectBoxes() {
	var selectBoxContainer = document.getElementById('tx_attributeselectbox_container');
	//alert(document.getElementById('tx_attributeselectbox_tagInput').value);
	if(selectBoxContainer) {
		var selectBoxes = document.getElementById(selectBoxContainer.id).getElementsByTagName('select');
		for(var selPosition=0;selPosition < selectBoxes.length;selPosition++) {
			var selectBox = selectBoxes[selPosition];
			var selectIndex = selectBox.selectedIndex;
			if(selectIndex == 0 || document.getElementById('tx_attributeselectbox_tagInput').value == '') {
				document.getElementById('tx_attributeselectbox_tagSubmit').disabled=true;				
				break;
			}
			else {
				if(selPosition == selectBoxes.length -1) {
					document.getElementById('tx_attributeselectbox_tagSubmit').disabled=false;
					//alert('All boxes checked');	
				}
				
			}
			
		}
	}
}

function checkInputBoxVal() {
	checkSelectBoxes();
}

function setAttributes(paramName, paramValue, containerId) {

	//datafant
	//alert(paramName + ' : ' + paramValue + ' : ' + containerId);
	//alert(location.search);
	//alert(window.location + ' : ' + window.location.href + ' : ' + document.location.href);
	//document.getElementById(containerId).innerHTML = '';
	callServer(paramName, paramValue, containerId);
	
	//var tags = document.getElementById('articleListing').childNodes[0].nodeName;
	var priceTags = document.getElementById(containerId).getElementsByTagName('p');
	var keepOneTag = 0;
	for(var pricePosition=0;pricePosition < priceTags.length;pricePosition++) {
		//if(priceTags[pricePosition].id.substring(0,5) == 'price') { alert(priceTags[pricePosition].id.substring(0,5)); }
		
		if(priceTags[pricePosition].id != null && priceTags[pricePosition].id.substring(0,5) == 'price' && keepOneTag != 0) {
			//document.removeElement(priceTags[pricePosition]);
			//priceTags[pricePosition].style.display = 'none';
			priceTags[pricePosition].parentNode.removeChild(priceTags[pricePosition]);
		}
		keepOneTag++;
		/*
		else if(priceTags[pricePosition].id != null && priceTags[pricePosition].id.substring(0,5) == 'price' && keepOneTag == 0) {
		
		}		
		*/
		
	}
	//alert(tags);
}

function callServer(paramName, paramValue, containerId) { 
	
	/*
	if(paramName[0] == 'registerChanges') {
		alert(paramName + ' : ' + paramValue);
	}
	*/
	xmlHttp = false;
	var xmlHttp;

	try { 
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try { 
				xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');		 		
			} catch (e) {
				alert('Ajax kört');
				
			}
		}
	}

	serverPage = "index.php?eID=tx_attributeselectbox" 	
	//serverPage += "&" + paramName + "=" + paramValue;		
	
	if (paramName.constructor.toString().indexOf("Array") == -1) {

		serverPage += "&" + paramName+ "=" + paramValue;
	}
	else {
		for (i=0;i<paramName.length;i++) {

			serverPage += "&" + paramName[i]+ "=" + paramValue[i];
		}
	}
	
	/*
	if(paramName[0] == 'registerChanges') {
		alert(serverPage);
	}
	*/	
	//alert(serverPage);
	xmlHttp.open("GET", serverPage, true);

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			xmlResponseValues = xmlHttp.responseText;
	//todo
	/*
	if(paramName[0] == 'registerChanges') {
		alert(xmlResponseValues);
	}
	*/				
			if(xmlResponseValues.match(":")) {
				xmlResponseValues = rtrim(xmlResponseValues, " ");
				//alert(xmlResponseValues);
				//setAttrNames(xmlResponseValues, articleUid, containerId);
				setAttrNames(xmlResponseValues, paramValue, containerId);
			}
			else if(xmlResponseValues.substring(0,5) == 'artno') {
				var artNo_Price = xmlResponseValues.split("_");
				//alert(xmlResponseValues.substring(6));
				if(artNo_Price[1] == 'Vald artikel finns ej i lager') {
					document.getElementById('tx_attributeselectbox_noArticle').className = 'noArticle';
					document.getElementById('tx_attributeselectbox_noArticle').innerHTML = artNo_Price[1];
				}
				else {
					//alert('Ajax alterInputTagValues');
					document.getElementById('tx_attributeselectbox_noArticle').className = 'hasArticle';
					document.getElementById('tx_attributeselectbox_noArticle').innerHTML = '';
					//alert('artNo_Price[1]: ' + artNo_Price[1] + ' artNo_Price[2]: ' + artNo_Price[2]);
					alterInputTagValues(artNo_Price[1], artNo_Price[2]);
				}
			}
			else {
				setInputTags(xmlResponseValues, paramValue, containerId);
				//return xmlResponseValues;
				//alert(xmlResponseValues);
			}
		
		}
		
	}
	xmlHttp.send(null);
}

function rtrim(str, chars) { 
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}


function setAttrNames(attrString, articleUid, containerId) { 
	//alert(attrString + ' : ' + articleUid + ' : ' + containerId);
	var attrNames = attrString;
	split1 = attrNames.split(":");
	split1[0] = rtrim(split1[0], " ");
	split1[1] = ltrim(split1[1], " ");
	//alert(split1[1]);
	split2 = split1[0].split(" ");
	split3 = split1[1].split(" ");
	split1[2] = ltrim(split1[2]); split1[2] = rtrim(split1[2]);
	//alert(split1[2]); 
	
	for(var position=0;position < split2.length; position++) {
		
		//split2[position] = replaceSweChars(split2[position]);
		//alert(split3[position]);
		//if(split2[position]) {
		//alert(document.getElementById(split2[position])); 
		
		if(document.getElementById('tx_attributeselectbox_' + split2[position]) == null) {


			var container = document.getElementById(containerId);
			var pLabel = document.createElement('p');
			container.appendChild(pLabel);
			//pLabel.setAttribute('id', split2[position]);
			pLabel.innerHTML = split2[position];
			//container.appendChild(pLabel);
			
			
			var newSelectBox = '';
    			if(IE) {
    				//alert(split2[position]);
    				var newSelectBox = document.createElement('<select id="tx_attributeselectbox_' + split2[position] + '" value="' + split2[position] + '" onchange="registerChanges(this, ' + containerId + ')">');
    				//newSelectBox = document.createElement('<select id="tx_attributeselectbox_' + split2[position] + '" ></select>');
		  		//tagHidden = document.createElement('<input value="' + priceUid + '" type="hidden" name="tx_commerce_pi1[artAddUid][' + articleUid + '][price_id]" id="tx_attributeselectbox_tagHidden" />');
		  		//alert('IE : ' + IE);
		  
    			}	
    			else {			
				newSelectBox = document.createElement('select');
				newSelectBox.setAttribute('value', split1[2]);
				newSelectBox.setAttribute('id', 'tx_attributeselectbox_' + split2[position]);
				newSelectBox.setAttribute('onchange', "registerChanges(this, '" + containerId + "')");
			
			}
			//newSelectBox.innerText = split2[position];



			container.appendChild(newSelectBox);
			//pLabel.appendChild(newSelectBox);
			
			//NYTT 20090331 start
			
			var emptyOption = document.createElement('option');
			emptyOption.setAttribute('selected', 'selected');
			emptyOption.innerHTML = '';			
			newSelectBox.appendChild(emptyOption);			
			
			//NYTT 20090331 slut
			
			
			/*	
			var newOption = document.createElement('option');
			newOption.setAttribute('value', split1[2]);
			newOption.innerHTML = split3[position];			
			newSelectBox.appendChild(newOption);
			*/

			var newOption = document.createElement('option');
			//newOption.text = split3[position];
			newOption.innerHTML = split3[position];
			newOption.setAttribute('value', split1[2]);
			document.getElementById('tx_attributeselectbox_' + split2[position]).appendChild(newOption);			
			//alert('Does Exist'); 
			
			callServer('priceUid', articleUid, containerId);
		
		}
		else {
			
			
			var optionNodes = document.getElementById('tx_attributeselectbox_' + split2[position]).childNodes;
			var valueExist = 0;
			for(var optionPos = 0;optionPos < optionNodes.length;optionPos++) {
				//alert(optionNodes[optionPos].text);
				//if(optionNodes[optionPos].text == split3[position]) {
				if(optionNodes[optionPos].innerHTML == split3[position]) {
					valueExist = 1;
				}
			}
			if(!valueExist) {
				
				var newOption = document.createElement('option');
				//newOption.text = split3[position];
				newOption.innerHTML = split3[position];
				newOption.setAttribute('value', split1[2]);
				document.getElementById('tx_attributeselectbox_' + split2[position]).appendChild(newOption);			
				//alert('Does Exist'); 
			}
		}
		
	
	}
	
} 

function setInputTags(priceUid, articleUid, containerId) {
	if(document.getElementById('tx_attributeselectbox_tagHidden') == null) {
	 //alert('IE : ' + IE);
		var container = document.getElementById(containerId);
		
		

    var noArticle = document.createElement('div');	
   	noArticle.setAttribute('id', 'tx_attributeselectbox_noArticle');
 		noArticle.setAttribute('className', 'hasArticle');

		
		container.appendChild(noArticle);
    var tagHidden = '';

    if(IE) {
		  tagHidden = document.createElement('<input value="' + priceUid + '" type="hidden" name="tx_commerce_pi1[artAddUid][' + articleUid + '][price_id]" id="tx_attributeselectbox_tagHidden" />');
		  //alert('IE : ' + IE);
		  
    }	
    else {
    				
		  tagHidden = document.createElement('input');	
		  tagHidden.setAttribute('value', priceUid);
		  tagHidden.setAttribute('type', 'hidden');
		  tagHidden.setAttribute('name', 'tx_commerce_pi1[artAddUid][' + articleUid + '][price_id]');
		  tagHidden.setAttribute('id', 'tx_attributeselectbox_tagHidden');		  
	  }	  
    container.appendChild(tagHidden);

		var pLabel = document.createElement('p');
		pLabel.setAttribute('style', 'padding-top:20px;');
		container.appendChild(pLabel);
		//pLabel.setAttribute('id', split2[position]);
		pLabel.innerHTML = 'Antal';
    
    var tagInput = '';
    
    if(IE) {
      tagInput = document.createElement('<input class="com-input-qty" size="3" name="tx_commerce_pi1[artAddUid][' + articleUid + '][count]" value="" type="input" onkeyup="checkInputBoxVal()" id="tx_attributeselectbox_tagInput" />');
    }
    else {
    
		  tagInput = document.createElement('input');
		  tagInput.setAttribute('class', 'com-input-qty');
		  tagInput.setAttribute('size', '3');
		  tagInput.setAttribute('name', 'tx_commerce_pi1[artAddUid][' + articleUid + '][count]');
		  tagInput.setAttribute('value', '');
  		  tagInput.setAttribute('type', 'input');
  		  tagInput.setAttribute('id', 'tx_attributeselectbox_tagInput');
  		  tagInput.setAttribute('onkeyup', 'checkInputBoxVal()');
    }	  

		container.appendChild(tagInput);	
		
			
		var tagSubmit = '';
    				
		if(IE) {
      tagSubmit = document.createElement('<input value="Lägg i varukorg" type="submit" id="tx_attributeselectbox_tagSubmit" disabled="true" />');
    }
    else {
    
      tagSubmit = document.createElement('input');
		  tagSubmit.setAttribute('value','Lägg i varukorg');
		  tagSubmit.setAttribute('type','submit');
		  tagSubmit.setAttribute('id', 'tx_attributeselectbox_tagSubmit');
		  tagSubmit.setAttribute('disabled', 'true');
		}
		container.appendChild(tagSubmit);
	}	
}

function alterInputTagValues(articleUid, priceUid) {
	if(IE) {

		document.getElementById('tx_attributeselectbox_tagHidden').name = 'tx_commerce_pi1[artAddUid][' + articleUid + '][price_id]';
		document.getElementById('tx_attributeselectbox_tagHidden').value = priceUid;
		document.getElementById('tx_attributeselectbox_tagInput').name = 'tx_commerce_pi1[artAddUid][' + articleUid + '][count]';
		//alert(document.getElementById('tx_attributeselectbox_tagHidden').name);
		//alert('tagHidden: ' + document.getElementById('tx_attributeselectbox_tagHidden').name);
		//alert('ie');
//todo1
//alert('articleUid:' + articleUid + ' priceUid: ' + priceUid);

    	}	
	else{
		document.getElementById('tx_attributeselectbox_tagHidden').setAttribute('name', 'tx_commerce_pi1[artAddUid][' + articleUid + '][price_id]');
		document.getElementById('tx_attributeselectbox_tagHidden').setAttribute('value', priceUid);
		document.getElementById('tx_attributeselectbox_tagInput').setAttribute('name', 'tx_commerce_pi1[artAddUid][' + articleUid + '][count]');
	}
}

function registerChanges(selectedElement, containerId) {

	checkSelectBoxes();
	
	//alert('registerChanges');
	var SelectBoxes = document.getElementsByTagName('select');
	var valueList = '';
	var paramName = new Array();
	var paramValue = new Array();
	paramName[0] = 'registerChanges';
	//alert(selectedElement.value);
	paramValue[0] = selectedElement.value;
	
	//NYTT 20090331 start
	
	/*
	if(paramValue[0] == '') {
		document.getElementById('tx_attributeselectbox_tagSubmit').disabled=true;
		//callServer(paramName, paramValue, containerId);
		return;
	}
	else {
		document.getElementById('tx_attributeselectbox_tagSubmit').disabled=false;
	}
	*/
	//NYTT 20090331 slut
	
	for(var selPosition=0;selPosition < SelectBoxes.length;selPosition++) {
		var selectBox = SelectBoxes[selPosition];
		var selectIndex = selectBox.selectedIndex;
		//alert(selectIndex);
		selectId = selectBox.id;
		//alert(selectId.substring(0,21));

		if(selectId.substring(0,21) == 'tx_attributeselectbox') {
			paramName[selPosition + 1] = selectId.substring(22);
			paramValue[selPosition + 1] = selectBox.options[selectIndex].text;

			//valueList += selectBox.options[selectIndex].text + ',';
		}
		
	}
	if(paramValue[0] != '') {
		callServer(paramName, paramValue, containerId);	
	}
	



	//alert(paramName + ' : ' + paramValues);
	
	/*
	var selectTag = document.getElementById(selectedElement.id)
	selIndex = selectTag.selectedIndex;
	alert(selectTag.options[selIndex].text);
	*/
}

function replaceSweChars(attrString) { 

	attrString.replace(/Ã¥/g, 'a');
	attrString.replace(/Ã…/g, 'A');

	attrString.replace("ä", "a");
	attrString.replace("Ä", "A");
	
	attrString.replace("ö", "o");
	attrString.replace("Ö", "O"); 

	alert('replaceSweChars' + attrString);

	return attrString;

} 

function witeSelectTags() {
	//<select id=>
}