/* DolarTimes.com - Calculator UI   */

/* Global Vars */
var _enabled = false;  // hack-one time button button initialization
var _urlCount = 1;
var _dtquery= "";  // TODO: use in the "use" function
var _customCssTitle;
var _dtCssTitle = 'dt-calc-style';
var _codeDiv = "";


/* Stylesheet mod functions */
function useCustomCss(customUrl)
{
	var code = document.getElementById('dt-widget-code');
	if (customUrl) // use the external style sheet
	{		
		if (code!=null)
		{
			var extStyle = document.getElementById('ccs-stylesheet-url');
			if (extStyle!=null)
			{
				extStyle=extStyle.value;			
				var id = _codeDiv.match(/id=[\d]+/);
				code.innerHTML= _codeDiv.replace(/id=[\d]+/,id[0]+"&style="+ extStyle);
			}
		}
	}
	else
	{
		if (code!=null)
		{		
			cssUrl = createCssUrl();
			querystring = cssUrl.match(/\?(.)+$/);
			querystring = querystring[0].slice(1,querystring[0].length);
			var id = _codeDiv.match(/id=[\d]+/);
			code.innerHTML= _codeDiv.replace(/id=[\d]+/,id[0]+"&"+ querystring);
		}
	}
}

function addCustomStylesheet(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('link');
	s.setAttribute('type','text/css');
	s.setAttribute('title', _customCssTitle);
	s.setAttribute('rel','alternate stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}

function createCssUrl() {
	var url = 'http://localhost:84/calculators/on-your-site/calc-css.php';
	var divs = new Array('ccp-bg-input','ccp-title-input','ccp-text-input','ccp-link-input','ccp-border-input','ccp-bg2-input','ccp-border2-input');
	var qp = new Array('bg=','ttl=','txt=','lnk=','bd=','bg2=','bd2=');
	var querystring = '?';
	var i = divs.length;
	while(i--) {
		var el = document.getElementById(divs[i]).value;
		if (el!=null && validColor(el) ) {
			querystring+=qp[i]+el+"&";
		}
	}
	querystring = querystring.substring(0, querystring.length-1);
	return url+querystring;
}

function revertColors(revertButton, previewButton) {
	var s = document.styleSheets;
	var i = s.length;
	while(i--) {
		if (s[i].title=='dt-custom-calc-style') { s[i].disabled=true;}
		if (s[i].title=='dt-calc-style') { s[i].disabled=false;}
	}
	toggleDisable(revertButton);
	toggleDisable(previewButton);
}

function previewColors() {
	if (cssUrl = createCssUrl()) {
		disableAllBtns();		
		changeBtnTxt('calc-customization-preview-1','Loading');
		_customCssTitle = 'dt-custom-calc-style'+_urlCount.toString();
		_urlCount++;
		addCustomStylesheet(cssUrl);
		var t = setTimeout("swapStyleSheets('true','calc-customization-preview-1')",1200);
	} // do nothing on invalid url
}

function previewStyleSheet() {  
	var cssUrl = document.getElementById('ccs-stylesheet-url').value;
	if (!isValidUrl(cssUrl)) {  return false;	}
	disableAllBtns();
	changeBtnTxt('calc-customization-preview-2','Loading');
	_customCssTitle = 'dt-custom-calc-style'+_urlCount.toString();
	_urlCount++;
	addCustomStylesheet(cssUrl);
	var t = setTimeout("swapStyleSheets('true','calc-customization-preview-2')",1200,'JavaScript');
}
// disable the old css, and enable the new one
function swapStyleSheets(useCustom, disabledBtn) {
	var s = document.styleSheets;
	var i = s.length;
	if (useCustom) { // enable new css
		while(i--) {
			if (s[i].title==_customCssTitle) { s[i].disabled=false;}
			else { s[i].disabled=(s[i].title!="");}  // leaves the original stylesheet enabled
		}
		enableAllBtnsExcept(disabledBtn);
		changeBtnTxt(disabledBtn,'Preview'); // change button back to 'Preview'
	}
	else { // reverting to standard
		//alert('swapping false'+disabledBtn);
		while(i--) {
			if (s[i].title==_dtCssTitle) { s[i].disabled=false;}
			else { s[i].disabled=(s[i].title!="");}
		}
		enableAllBtnsExcept('calc-customization-revert-1','calc-customization-revert-2');
	}

}

/* Key press functions */ 
function refreshCssUrl(event) {  // re-enable preview button if valid url
	var btn = document.getElementById('calc-customization-preview-2');
	var cssUrl = document.getElementById('ccs-stylesheet-url').value;
	if (btn!= null)
	{
		if (isValidUrl(cssUrl))
		{
			if(btn.disabled==true) { btn.disabled=false; }
			if (event.keyCode==13) { previewStyleSheet(); }
		}
		else { // disable
			if(btn.disabled==false) { btn.disabled=true; }
		}
	}
}

function colorInputChg(divId, val) {
	if (validColor(val))
	{
		var btn = document.getElementById('calc-customization-preview-1');
		if (btn!= null)
		{
			if(btn.disabled==true) {
				btn.disabled=false;
			}
		}
		var el = document.getElementById(divId);
		el.style.backgroundColor = "#"+val;
	}
}


/*  Utility Functions */
function toggleHidden() {
	if (!_enabled)
	{
		enableAllBtnsExcept('calc-customization-revert-1','calc-customization-revert-2');
		_enabled=true;
		var code = document.getElementById('dt-widget-code');
		if (code!=null)
		{
			_codeDiv = code.innerHTML;
		}

	}
	for (var i = 0; i<arguments.length; i++) {
		var el = document.getElementById(arguments[i]);
		if (el!=null) {		
			if(el.className=="hidden") {
				el.className="";
			}
			else {
				el.className="hidden";
			}
		}
	}
}

function toggleDisable() {
	for (var i = 0; i<arguments.length; i++) {
		var el = document.getElementById(arguments[i]);
		if (el!=null) {		
			if(el.disabled==true) {
				el.disabled=false;
			}
			else {
				el.disabled=true;
			}
		}
	}
}
// initialization of UI
function enableAllBtnsExcept() {
	var btns = document.getElementsByTagName('button');
	var l = btns.length;
	while(l--) {
		btns[l].disabled=(isIdInArray(btns[l].id,arguments));
	}
}

function disableAllBtns() {
	var btns = document.getElementsByTagName('button');
	var l = btns.length;
	while(l--) {
		btns[l].disabled=true;
	}
}

function isIdInArray(btnId, args) {
	var i = args.length;
	while (i--) {
		if (args[i]==btnId) {
			// alert('checking for btnId: '+btnId+' in arg: '+args[i]);
			return true;
		}
	}
	return false;
}
function toggleSpan(spanId) {
	var el = document.getElementById(spanId);
	if (el!=null) {
		//alert(el.innerHTML);
		if (el.innerHTML=="&gt;")
		{
			el.innerHTML="&lt;";
		}
		else if (el.innerHTML=="&lt;")
		{
			el.innerHTML="&gt;";
		}
	}
}

function validColor(val) {
	if(val.length==6) { 
		var re = /^([0-9]|[a-f]){6}$/;
		var matches = val.toLowerCase().match(re);
		if (matches[0]==val.toLowerCase()) {
			return true; 
		}
	}
	return false; 
}

function isValidUrl(cssUrl) {
	return cssUrl.match(/^http:\/\/.{4}.+/);  // simple regex check for http:// + 5 chars
}

function changeBtnTxt(divId, text) {
   var el = document.getElementById(divId);
   if (el!=null)
   {
	   el.innerHTML = text;
   }
}

function focusOnElement(s) {
	var el = document.getElementById(arguments[0]);
	if (el!=null) {
		el.focus();
	}
}
