// $Id: ustyle_switch.js 659 2007-11-08 12:07:49Z secretr $
//ustyleSetActiveStyle() is based on  Paul Sowden's styleswitcher.js - http://www.alistapart.com/stories/alternate/
//Support and Ideas: secretr - www.free-source.net
function ustyleCheckeAjax(loadL) {
    /* <![CDATA[ */
	if(typeof sendInfo == 'undefined') {
		document.write('<script type="text/javascript" src="'+loadL+'"></script>');
	}
	/* ]]> */
}

function ustyleSetActiveStyle(title) {
	var i, a;
	
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("rel").indexOf("alt") != -1) {
			a.disabled = true;
			
			if(a.getAttribute("title") == title) {
				a.disabled = false;
			}
			//alert(a.disabled + ' - ' + a.getAttribute("title"));
		}
	}
}

function ustyleFixPage(title, optfix) {
	var a, el;
	a = document.getElementsByTagName('a');
	for (var i=0; i<a.length; i++){
		var anchor = a[i];
		//className 
		var elAttribute = String(anchor.getAttribute('id'));
		var activeEl = 'ustyle_' + title;
		if(elAttribute.match('ustyle_')) {
			if(activeEl == elAttribute)
				anchor.className = 'ustyle_link_active';//anchor.setAttribute("class", 'ustyle_link_active');
			else
				anchor.className = 'ustyle_link';//anchor.setAttribute("class", 'ustyle_link');
			//alert(elAttribute + ' - ' + anchor.className);
		}
	}
	if(optfix) {
		var f = document.forms["dataform"];
		if(!f) return;
		
		for (var i=0; i<f.elements.length; i++){
			var sel = f.elements[i];
			var elName = String(sel.name);
			if(elName.match('user_ustyle_css')) {
				var opt = sel.options;
				
				for (var b=0; b<opt.length; b++){
					if(opt[b].value != title)
						opt[b].selected = false;
					else 
						opt[b].selected = true;
					
				}
				return;
			}
		}
	}
	
}

function ustyleSetActiveAjaxStyle(title, url, optfix, d) {
	ustyleSetActiveStyle(title);
    var dobj = new Date();
    var rndget = dobj.getTime(); //IE6 cache issue - AGAIN!!! Sick of it
	url=url+'?style.' + title + '.' + rndget;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() { 
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) { 
				if(d) alert(req.responseText);
			}
		};
	} else {
		if(d)
            alert("XMLHTTPRequest failed");
		return null;
	}
	req.open('GET', url);
	req.send(null);
	ustyleFixPage(title, optfix);
}