var http = new Array(), lloaded = ''; // don't do anything to this
function request (url, str, elm) {
         var tmp = Math.floor(new Date().getTime()/1000);
         http[tmp] = createHTTP();
         if (!http[tmp]) { return false; };
         if (http[tmp].overrideMimeType) {
            http[tmp].overrideMimeType('text/html');
         }
         if (elm != null) {
            http[tmp].onreadystatechange = function() { setContent(elm,tmp); };
         }
         http[tmp].open('POST', url, true);
         http[tmp].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         http[tmp].setRequestHeader("Content-length", str.length);
         http[tmp].setRequestHeader("Connection", "close");
         http[tmp].send(str);
}
function createHTTP () {
         if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
          } else if (window.ActiveXObject) {
            try {
               req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
               try {
                 req = new ActiveXObject("Microsoft.XMLHTTP");
               } catch (e) { alert('ERROR: Cannot create XMLHTTP instance.'); return false; }
            }
         }
         return req;
}
function setContent (elm,tmp) {
         if (http[tmp].readyState == 4) {
            if (http[tmp].status == 200) {
               result = http[tmp].responseText;
               var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig, match;
               while (match = re.exec(result)) {
                  try {
                     eval(match[1]);
                   } catch(err) {
                     alert(String(err));
                  }
               }
               elm.innerHTML = result;
               window.status = "OK - Request sucessful. (Request ID: "+tmp+")";
             } else {
               window.status = "Error: "+http[tmp].status+" (Request ID: "+tmp+")";
               alert('There was a problem with the request.\r\n Error Code: '+http[tmp].status+" (Request ID: "+tmp+")");
            }
         }
         delete running;
}
function loadPage (str) {
         if (typeof(running) == "undefined") {
            running = true;
            if (str == null) { str = "Home"; }
            if ((elm = document.getElementById("page_data")) != null) {
               var url = readConf('base_path')+readConf('cont_gen');
               request(url, "page="+str, elm);
             } else {
               alert("Could not get content element!");
            }
         }
}
function readConf (str) {
         return (typeof(conf[str])!="undefined"?conf[str]:false);
}
function genRand () {
         num = Math.ceil(Math.random()*readRand('count'));
         if ((elm = document.getElementById("random_image")) != null) {
            elm.src = (readRand(num,'img')==false?readRand('noimg'):readRand(num,'img'));
            elm.style.width = (readRand('img_width')!=false?readRand('img_width'):'100px');
            elm.style.height = (readRand('img_height')!=false?readRand('img_height'):'100px');
         }
         if ((elm = document.getElementById("random_text")) != null) {
            elm.innerHTML = (readRand(num,'text')==false?readRand('notext'):readRand(num,'text'));
         }
}
function readRand (id,str) {
         return (str!=null?((typeof(rand[id])!="undefined")&&(typeof(rand[id][str])!="undefined")?rand[id][str]:false):typeof(rand[id])!="undefined"?rand[id]:false);
}
function parseURL () {
         if (window.location.hash) {
            hash = window.location.hash.replace(/#/,"");;
            if (lloaded != hash) {
               lloaded = hash;
               loadPage(hash);
            }
          } else {
            home = (readConf('home_page')!=false?readConf('home_page'):"Home");
            if (lloaded != home) {
               lloaded = home;
               window.location.hash = '#'+home;
               loadPage(home);
            }
         }
}
function init () {
         parseURL(); genRand();
         window.setInterval(function(){parseURL();},200);
         window.setInterval(function(){genRand();},readRand('speed')*1000);
}

function submit_form_by_id(tag, opt) {
		var str;
		var form = document.getElementById(tag);
		for (cnt=0;form.elements[cnt];cnt++)
		{
			if ((form.elements[cnt].type == "radio") || (form.elements[cnt].type == "checkbox"))
			{
				if (form.elements[cnt].checked != true)
					continue;
			}

			if (str) {
				str=str+"&";
				str=str+form.elements[cnt].name+"="+form.elements[cnt].value;
			} else {
				str=form.elements[cnt].name+"="+form.elements[cnt].value;
			}
		}
		//alert(str);
		if (opt[0] == '?')
			request ('ajax-core.php'+opt, str);
		else	
			request('ajax-core.php?'+opt, str);
			
}



