﻿var agt=navigator.userAgent.toLowerCase();

var is_major = parseInt(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function onContent(f) {//(C)webreflection.blogspot.com
    var a = onContent, b = navigator.userAgent, d = document, w = window, c = "onContent", e = "addEventListener", o = "opera", r = "readyState",
s = "<scr".concat("ipt defer src='//:' on", r, "change='if(this.", r, "==\"complete\"){this.parentNode.removeChild(this);", c, ".", c, "()}'></scr", "ipt>");
    a[c] = (function(o) { return function() { a[c] = function() { }; for (a = arguments.callee; !a.done; a.done = 1) f(o ? o() : o) } })(a[c]);
    if (d[e]) d[e]("DOMContentLoaded", a[c], false);
    if (/WebKit|Khtml/i.test(b) || (w[o] && parseInt(w[o].version()) < 9)) (function() { /loaded|complete/.test(d[r]) ? a[c]() : setTimeout(arguments.callee, 1) })();
    else if (/MSIE/i.test(b)) d.write(s);
};

function captureEnter(e){ 	
	var code;
	if (!e)var e = window.event;	
	if (e.keyCode){		
		code = e.keyCode;	
	}else if (e.which){		
		code = e.which;	
	}
	return code==13;
}

 
	 

String.format = function(){	
	var s = arguments[0];	
	for (var i = 0;i<arguments.length-1;i++){		
		var reg = new RegExp("\\{"+i+"\\}","gm");			
		s = s.replace(reg,arguments[i+1]);
	}
	return s;
}

String.prototype.format = function(){	
	var s = this;
	for (var i = 0;i<arguments.length;i++){		
		var reg = new RegExp("\\{"+i+"\\}","gm");					
		s = s.replace(reg,arguments[i]);
	}
	return s;
}

function list(o,inside){
	var s = "";
	if (typeof(o)=="undefined")s="undefined";
	if (o==null)s="NULL";
	for(var i in o){
		s+= ( ((inside)?("\t"):("")) + i + "=" + o[i] + "\n");
		if (typeof(o[i])=="object" && o[i]!=null){
			s+=list(o[i],1);
		}
	}
	return s;
}

function alertToPopup(o){
	var s = list(o);
	var w = window.open("","","");
	
	w.document.open();
	w.document.write( "<xmp>"+s+"</xmp>");
	w.document.close();
}

function AjaxProOutput(func){
	return function (ajaxResponse){

		if (ajaxResponse.error!=null){
			alert ("AJAX ERROR:\n\n" + list(ajaxResponse.error));
		}		
		
		if (ajaxResponse.value!=null){
			//var str = list(ajaxResponse.value);
			//alert ("AJAX VALUE:\n\n" + str);
			func (ajaxResponse.value);
		}
	}
}

function setHtml(id,html,toAppend,flashElementId){
	if (id=="")return;
	var o = $(id);
	if (o){
		html = html.replace(/\\r\\n/gi,"");
		o.innerHTML = (toAppend)?(o.innerHTML+html):(html);
		window[flashElementId] = $(flashElementId);
	}
}

function getFlashCode (url,id,w,h,bgcolor, wmode){
	if (is_ie){
		bgcolor=bgcolor ? '<param name="bgColor" value="'+bgcolor+'" />' : "";
		wmode=wmode ? '<param name="wmode" value="'+wmode+'" />' : "";

		str = '<object type="application/x-shockwave-flash" \
			classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" \
			codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"\
			name="' + id + '" id="' + id + '" \
			style="width:'+w+'px;height:'+h+'px;">\
			<param name="movie" value="'+url+'" />\
			<param name="allowScriptAccess" value="sameDomain" />\
			'+bgcolor+'\
			'+wmode+'\
			</object>';

	}else{
		str = '<embed swLiveConnect="true" quality=best type="application/x-shockwave-flash" menu=false  ' +
			' name="' + id + '" id="' + id + '" ' +
			' src="' + url + '" ' +
			' wmode="'+ wmode +'" ' +
			' bgcolor="' + bgcolor + '" ' +
			' width=' + w + ' height=' + h +
			' swLiveConnect=true ' +
			' allowScriptAccess="sameDomain" ' +
			' type="application/x-shockwave-flash" ' +
			' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" ></embed>';


	}
	if(!window[id])window[id] = $(id);
	//alert (str);
	return str;
}


function addFlashInside (containerId, url,id,w,h,bgcolor, wmode){
	var str = getFlashCode(url,id,w,h,bgcolor, wmode);
	setHtml(containerId,str,false,id);
}

function addFlash(url,id,w,h,bgcolor, wmode){
	var str = getFlashCode(url,id,w,h,bgcolor, wmode);

	document.write (str);
}

function replaceClass(id,str1,str2){
	if (id=="")return;
	var o = $(id);
	if (o){
		o.className = (o.className!="")?(o.className.replace (str1,str2)):(str2);
	}
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\s*' + escape(name) + '=([^;\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
}

function $waitUntil(waitUntil, onComplete, delay) {
    if (waitUntil()) {
        onComplete();
        return;
    } else {
        setTimeout(function() {
            $waitUntil(waitUntil, onComplete, delay || 100);
        });
    }
}
