/*------------------------------------------------------------------------------
   $Id: nd_options.js 10 2010-07-18 13:46:48Z Standard $

   nd_options - Contribution for XT-Commerce http://www.xt-commerce.com
   modified by http://www.netz-designer.de

   Copyright (c) 2003 - 2010 netz-designer
   -----------------------------------------------------------------------------
   based on:
   Option Types v2 created by AvanOsch for http://Shop.CrystalCopy.nl

   Contribution based on:

   osCommerce, Open Source E-Commerce Solutions
   http://www.oscommerce.com

   Copyright (c) 2002 - 2003 osCommerce

   Released under the GNU General Public License
   ---------------------------------------------------------------------------*/

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
	var xmlHttp;	
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch (e) {}
		}
	}
	if(!xmlHttp) {
		alert("could not create XMLHttpRequestObject");
	} else {
		return xmlHttp;
	}
}

function process (params) {
	if(xmlHttp) {
		try {
			if( xmlHttp.readyState == 0 || xmlHttp.readyState == 4 ) {	
				xmlHttp.open("GET", "nd_options_processAjax.php?" + params, true);
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(null);
			}
		} catch (e) {
			alert("Error while processing the request!");
		}
	}
}

function handleRequestStateChange () {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			try {
				handleServerResponse();
			} catch (e) {
				alert("Error while reading the Request!\n" + e.toString() + xmlHttp.responseText);
			}
		} else {
			alert("Error while retrieving information!\n" + xmlHttp.statusText);
		}
	}
}

function handleServerResponse() {
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse || !xmlResponse.documentElement) {
		throw("1Fehlerhafte XML-Struktur:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror") {
		throw("2Fehlerhafte XML-Struktur:\n" + xmlHttp.responseText);
	}
	xmlRoot = xmlResponse.documentElement;
	if(rootNodeName != "response" || !xmlRoot.firstChild) {
		throw("3Fehlerhafte XML-Struktur:\n" + xmlHttp.responseText);
	}
	
	if(divProductsPriceAjax = document.getElementById("nd_products_price")) {
		newPrice = xmlRoot.getElementsByTagName("newprice")[0];
		divProductsPriceAjax.innerHTML = newPrice.firstChild.data;
	}
}

function getFormValues(name) {
	var idString = '';
	var ndForm = document.forms[name];
	for(var i = 0; i < ndForm.elements.length; i++) {
		var ndElement = ndForm.elements[i];
		if(ndElement.type == 'checkbox') {
			idString += ndElement.name + ':';
			if(ndElement.checked) {
				idString += ndElement.value;
			}
		} else if(ndElement.type == 'select-one' || ndElement.type == 'select-multiple' || ndElement.type == 'select') {
			for(var j = 0; j < ndElement.options.length; j++) {
				if(ndElement.options[j].selected) {
					idString += ndElement.name + ':';
					idString += ndElement.options[j].value;
				}
			}
		} else if (ndElement.type == 'radio') {
			if(ndElement.checked) {
				idString += ndElement.name + ':';
				idString += ndElement.value;
			}
		} else {
			idString += ndElement.name + ':';
			idString += ndElement.value;
		}
		idString += ';';
	}
	return(escape(idString));
}
