//	by Aleksandar Dulovic - August 06, 2006

// showhide
function showhide(x) {
	document.getElementById(x).style.display = (document.getElementById(x).style.display=='block') ? 'none' : 'block';
}
function justshow(x) {
	document.getElementById(x).style.display = 'block';
}
function justhide(x) {
	document.getElementById(x).style.display = 'none';
}

// image resizing
function calculateWidth(w,h,Directory,file){
	var nh = document.getElementById('new_'+file+'_height_'+Directory).value;
	var nw = Math.round(w * nh / h);
	document.getElementById('new_'+file+'_width_'+Directory).value = nw;
}
function calculateHeight(w,h,Directory,file){
	var nw = document.getElementById('new_'+file+'_width_'+Directory).value;
	var nh = Math.round(h * nw / w);
	document.getElementById('new_'+file+'_height_'+Directory).value = nh;
}

// confirm deletions
function confirmDelete(){
	if (confirm("Are you sure you want to delete this record?"))
		return true ;
	else
		return false ;
}

// table highlighting
function HILITE() {
if (document.getElementById && document.createTextNode) {
		var myTABLE = document.getElementsByTagName("table");
		for (var i=0; i<myTABLE.length; i++) {
			var myROW = myTABLE[i].getElementsByTagName("tr");
			for (var j=1; j<myROW.length; j++) {
				var myClass;
				myROW[j].onmouseover = function(){
					myClass = this.className;
					this.className = 'colorme';
					return false;
				}
				myROW[j].onmouseout  = function(){
					this.className = myClass;
					return false;
				}
			}
		}
	}
}

// niftycube
var niftyOk=(document.getElementById && document.createElement && Array.prototype.push);
var niftyCss=false;
String.prototype.find=function(what){
	return(this.indexOf(what)>=0 ? true : false);
}
var oldonload=window.onload;
if(typeof(NiftyLoad)!='function') NiftyLoad=function(){};
if(typeof(oldonload)=='function')
    window.onload=function(){oldonload();AddCss();NiftyLoad()};
else window.onload=function(){AddCss();NiftyLoad()};

function AddCss(){
	niftyCss=true;
	var l=CreateEl("link");
	l.setAttribute("type","text/css");
	l.setAttribute("rel","stylesheet");
	l.setAttribute("href","inc/css/niftyCorners.css");
	l.setAttribute("media","screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

function Nifty(selector,options){
	if(niftyOk==false) return;
	if(niftyCss==false) AddCss();
	var i,v=selector.split(","),h=0;
	if(options==null) options="";
	if(options.find("fixed-height"))
	    h=getElementsBySelector(v[0])[0].offsetHeight;
	for(i=0;i<v.length;i++)
	    Rounded(v[i],options);
	if(options.find("height")) SameHeight(selector,h);
}

function Rounded(selector,options){
	var i,top="",bottom="",v=new Array();
	if(options!=""){
	    options=options.replace("left","tl bl");
	    options=options.replace("right","tr br");
	    options=options.replace("top","tr tl");
	    options=options.replace("bottom","br bl");
	    options=options.replace("transparent","alias");
	    if(options.find("tl")){
	        top="both";
	        if(!options.find("tr")) top="left";
	        }
	    else if(options.find("tr")) top="right";
	    if(options.find("bl")){
	        bottom="both";
	        if(!options.find("br")) bottom="left";
	        }
	    else if(options.find("br")) bottom="right";
	    }
	if(top=="" && bottom=="" && !options.find("none")){top="both";bottom="both";}
	v=getElementsBySelector(selector);
	for(i=0;i<v.length;i++){
	    FixIE(v[i]);
	    if(top!="") AddTop(v[i],top,options);
	    if(bottom!="") AddBottom(v[i],bottom,options);
    }
}

function AddTop(el,side,options){
	var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
	d.style.marginLeft="-"+getPadding(el,"Left")+"px";
	d.style.marginRight="-"+getPadding(el,"Right")+"px";
	if(options.find("alias") || (color=getBk(el))=="transparent"){
	    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
	    }
	else{
	    bk=getParentBk(el); border=Mix(color,bk);
	    }
	d.style.background=bk;
	d.className="niftycorners";
	p=getPadding(el,"Top");
	if(options.find("small")){
	    d.style.marginBottom=(p-2)+"px";
	    btype+="s"; lim=2;
	    }
	else if(options.find("big")){
	    d.style.marginBottom=(p-10)+"px";
	    btype+="b"; lim=8;
	    }
	else d.style.marginBottom=(p-5)+"px";
	for(i=1;i<=lim;i++)
	    d.appendChild(CreateStrip(i,side,color,border,btype));
	el.style.paddingTop="0";
	el.insertBefore(d,el.firstChild);
}

function AddBottom(el,side,options){
	var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
	d.style.marginLeft="-"+getPadding(el,"Left")+"px";
	d.style.marginRight="-"+getPadding(el,"Right")+"px";
	if(options.find("alias") || (color=getBk(el))=="transparent"){
	    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
	    }
	else{
	    bk=getParentBk(el); border=Mix(color,bk);
	    }
	d.style.background=bk;
	d.className="niftycorners";
	p=getPadding(el,"Bottom");
	if(options.find("small")){
	    d.style.marginTop=(p-2)+"px";
	    btype+="s"; lim=2;
	    }
	else if(options.find("big")){
	    d.style.marginTop=(p-10)+"px";
	    btype+="b"; lim=8;
	    }
	else d.style.marginTop=(p-5)+"px";
	for(i=lim;i>0;i--)
	    d.appendChild(CreateStrip(i,side,color,border,btype));
	el.style.paddingBottom=0;
	el.appendChild(d);
}

function CreateStrip(index,side,color,border,btype){
	var x=CreateEl("b");
	x.className=btype+index;
	x.style.backgroundColor=color;
	x.style.borderColor=border;
	if(side=="left"){
	    x.style.borderRightWidth="0";
	    x.style.marginRight="0";
	    }
	else if(side=="right"){
	    x.style.borderLeftWidth="0";
	    x.style.marginLeft="0";
	    }
	return(x);
}

function CreateEl(x){
	return(document.createElement(x));
}

function FixIE(el){
	if(el.currentStyle!=null && el.currentStyle.hasLayout!=null && el.currentStyle.hasLayout==false) {el.style.display="inline-block";}
}

function SameHeight(selector,maxh){
	var i,v=selector.split(","),t,j,els=[],gap;
	for(i=0;i<v.length;i++){
	    t=getElementsBySelector(v[i]);
	    els=els.concat(t);
	    }
	for(i=0;i<els.length;i++){
	    if(els[i].offsetHeight>maxh) maxh=els[i].offsetHeight;
	    els[i].style.height="auto";
	    }
	for(i=0;i<els.length;i++){
	    gap=maxh-els[i].offsetHeight;
	    if(gap>0){
	        t=CreateEl("b");t.className="niftyfill";t.style.height=gap+"px";
	        nc=els[i].lastChild;
	        if(nc.className=="niftycorners")
	            els[i].insertBefore(t,nc);
	        else els[i].appendChild(t);
        }
    }
}

function getElementsBySelector(selector){
	var i,j,selid="",selclass="",tag=selector,tag2="",v2,k,f,a,s=[],objlist=[],c;
	if(selector.find("#")){ //id selector like "tag#id"
	    if(selector.find(" ")){  //descendant selector like "tag#id tag"
	        s=selector.split(" ");
	        var fs=s[0].split("#");
	        if(fs.length==1) return(objlist);
	        f=document.getElementById(fs[1]);
	        if(f){
	            v=f.getElementsByTagName(s[1]);
	            for(i=0;i<v.length;i++) objlist.push(v[i]);
	            }
	        return(objlist);
	        }
	    else{
	        s=selector.split("#");
	        tag=s[0];
	        selid=s[1];
	        if(selid!=""){
	            f=document.getElementById(selid);
	            if(f) objlist.push(f);
	            return(objlist);
	            }
	        }
	    }
	if(selector.find(".")){      //class selector like "tag.class"
	    s=selector.split(".");
	    tag=s[0];
	    selclass=s[1];
	    if(selclass.find(" ")){   //descendant selector like tag1.classname tag2
	        s=selclass.split(" ");
	        selclass=s[0];
	        tag2=s[1];
	        }
	    }
	var v=document.getElementsByTagName(tag);  // tag selector like "tag"
	if(selclass==""){
	    for(i=0;i<v.length;i++) objlist.push(v[i]);
	    return(objlist);
	    }
	for(i=0;i<v.length;i++){
	    c=v[i].className.split(" ");
	    for(j=0;j<c.length;j++){
	        if(c[j]==selclass){
	            if(tag2=="") objlist.push(v[i]);
	            else{
	                v2=v[i].getElementsByTagName(tag2);
	                for(k=0;k<v2.length;k++) objlist.push(v2[k]);
	                }
	            }
	        }
	    }
	return(objlist);
}

function getParentBk(x){
	var el=x.parentNode,c;
	while(el.tagName.toUpperCase()!="HTML" && (c=getBk(el))=="transparent")
	    el=el.parentNode;
	if(c=="transparent") c="#FFFFFF";
	return(c);
}

function getBk(x){
	var c=getStyleProp(x,"backgroundColor");
	if(c==null || c=="transparent" || c.find("rgba(0, 0, 0, 0)"))
	    return("transparent");
	if(c.find("rgb")) c=rgb2hex(c);
	return(c);
}

function getPadding(x,side){
	var p=getStyleProp(x,"padding"+side);
	if(p==null || !p.find("px")) return(0);
	return(parseInt(p));
}

function getStyleProp(x,prop){
	if(x.currentStyle)
	    return(x.currentStyle[prop]);
	if(document.defaultView.getComputedStyle)
	    return(document.defaultView.getComputedStyle(x,'')[prop]);
	return(null);
}

function rgb2hex(value){
	var hex="",v,h,i;
	var regexp=/([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
	var h=regexp.exec(value);
	for(i=1;i<4;i++){
	    v=parseInt(h[i]).toString(16);
	    if(v.length==1) hex+="0"+v;
	    else hex+=v;
	    }
	return("#"+hex);
}

function Mix(c1,c2){
	var i,step1,step2,x,y,r=new Array(3);
	if(c1.length==4)step1=1;
	else step1=2;
	if(c2.length==4) step2=1;
	else step2=2;
	for(i=0;i<3;i++){
	    x=parseInt(c1.substr(1+step1*i,step1),16);
	    if(step1==1) x=16*x+x;
	    y=parseInt(c2.substr(1+step2*i,step2),16);
	    if(step2==1) y=16*y+y;
	    r[i]=Math.floor((x*50+y*50)/100);
	    r[i]=r[i].toString(16);
	    if(r[i].length==1) r[i]="0"+r[i];
	    }
	return("#"+r[0]+r[1]+r[2]);
}

//	Challenge Handshake Authentication Protocol
var hex_chr = "0123456789abcdef";
function rhex(num){
	str = "";
	for(j = 0; j <= 3; j++)
	str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
		hex_chr.charAt((num >> (j * 8)) & 0x0F);
	return str;
}

function str2blks_MD5(str){
	nblk = ((str.length + 8) >> 6) + 1;
	blks = new Array(nblk * 16);
	for(i = 0; i < nblk * 16; i++) blks[i] = 0;
	for(i = 0; i < str.length; i++)
		blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
		blks[i >> 2] |= 0x80 << ((i % 4) * 8);
		blks[nblk * 16 - 2] = str.length * 8;
	return blks;
}

function add(x, y){
	var lsw = (x & 0xFFFF) + (y & 0xFFFF);
	var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
	return (msw << 16) | (lsw & 0xFFFF);
}

function rol(num, cnt){
	return (num << cnt) | (num >>> (32 - cnt));
}

function cmn(q, a, b, x, s, t){
	return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t){
	return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t){
	return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t){
	return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t){
	return cmn(c ^ (b | (~d)), a, b, x, s, t);
}

function MD5(str){
	x = str2blks_MD5(str);
	var a =	1732584193;
	var b = -271733879;
	var c = -1732584194;
	var d =	271733878;
	for(var i = 0; i < x.length; i += 16){
		var olda = a;
		var oldb = b;
		var oldc = c;
		var oldd = d;
		a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
		d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
		c = ff(c, d, a, b, x[i+ 2], 17,	606105819);
		b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
		a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
		d = ff(d, a, b, c, x[i+ 5], 12,	1200080426);
		c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
		b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
		a = ff(a, b, c, d, x[i+ 8], 7 ,	1770035416);
		d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
		c = ff(c, d, a, b, x[i+10], 17, -42063);
		b = ff(b, c, d, a, x[i+11], 22, -1990404162);
		a = ff(a, b, c, d, x[i+12], 7 ,	1804603682);
		d = ff(d, a, b, c, x[i+13], 12, -40341101);
		c = ff(c, d, a, b, x[i+14], 17, -1502002290);
		b = ff(b, c, d, a, x[i+15], 22,	1236535329);
		a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
		d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
		c = gg(c, d, a, b, x[i+11], 14,	643717713);
		b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
		a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
		d = gg(d, a, b, c, x[i+10], 9 ,	38016083);
		c = gg(c, d, a, b, x[i+15], 14, -660478335);
		b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
		a = gg(a, b, c, d, x[i+ 9], 5 ,	568446438);
		d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
		c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
		b = gg(b, c, d, a, x[i+ 8], 20,	1163531501);
		a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
		d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
		c = gg(c, d, a, b, x[i+ 7], 14,	1735328473);
		b = gg(b, c, d, a, x[i+12], 20, -1926607734);
		a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
		d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
		c = hh(c, d, a, b, x[i+11], 16,	1839030562);
		b = hh(b, c, d, a, x[i+14], 23, -35309556);
		a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
		d = hh(d, a, b, c, x[i+ 4], 11,	1272893353);
		c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
		b = hh(b, c, d, a, x[i+10], 23, -1094730640);
		a = hh(a, b, c, d, x[i+13], 4 ,	681279174);
		d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
		c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
		b = hh(b, c, d, a, x[i+ 6], 23,	76029189);
		a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
		d = hh(d, a, b, c, x[i+12], 11, -421815835);
		c = hh(c, d, a, b, x[i+15], 16,	530742520);
		b = hh(b, c, d, a, x[i+ 2], 23, -995338651);
		a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
		d = ii(d, a, b, c, x[i+ 7], 10,	1126891415);
		c = ii(c, d, a, b, x[i+14], 15, -1416354905);
		b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
		a = ii(a, b, c, d, x[i+12], 6 ,	1700485571);
		d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
		c = ii(c, d, a, b, x[i+10], 15, -1051523);
		b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
		a = ii(a, b, c, d, x[i+ 8], 6 ,	1873313359);
		d = ii(d, a, b, c, x[i+15], 10, -30611744);
		c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
		b = ii(b, c, d, a, x[i+13], 21,	1309151649);
		a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
		d = ii(d, a, b, c, x[i+11], 10, -1120210379);
		c = ii(c, d, a, b, x[i+ 2], 15,	718787259);
		b = ii(b, c, d, a, x[i+ 9], 21, -343485551);
		a = add(a, olda);
		b = add(b, oldb);
		c = add(c, oldc);
		d = add(d, oldd);
	}
	return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}

function doCHAP(){
	var usr = document.getElementById('userid');
	if(!usr){return};
	if(!usr.value) {showError(usr,'Enter your ID'); return false}
	var psw = document.getElementById('password');
	if(!psw){return};
	if(!psw.value) {showError(psw,'Enter your password'); return false}
	var sess = document.getElementById('session').value;
	if(!sess){return};
	psw.value = MD5(MD5(psw.value)+sess);
	return true;
}

function showError(obj,message){
	alert(message);
	obj.focus();
	return false;
}

// masking
function mask(str,textbox,loc,delim){
	var locs = loc.split(',');
	for (var i = 0; i <= locs.length; i++){				// Begins a loop through the array of locations.
		for (var k = 0; k <= str.length; k++){			// Begins a nested loop through each character to check if the delimiter is already there.
			if (k == locs[i]){							// Begins a conditional statement that checks to see what character number we are holding.
				if (str.substring(k, k+1) != delim){	// Begins a nested conditional statement if the character we're holding is the correct character location, and checks to see if it is the same character as the delimiter.
					str = str.substring(0,k) + delim + str.substring(k,str.length) // if the character is not the same as the delimiter, it will cut the value in half and insert the delimiter
				}
			}
		}
	}
	textbox.value = str
}

// check if it has a flash map
function hasFlashMap(OPT) {
	// make sure all divs inside flas_res are hidden
	var children = document.getElementById("flash_res").childNodes;
	for(var i=0; i < children.length; i++) {
		document.getElementById(children[i].id).style.display = 'none';
	}
	// now show the selected one or abort if it doesnt exist
	if (document.getElementById("flash_res_"+OPT) == null) {
		return;
	} else {
		document.getElementById("flash_res_"+OPT).style.display = 'block';
	}
}

// update flash map link
function updateFlashMapLink() {
	var A = document.getElementById("Area").value;
	var L = document.getElementById("flash_res_"+A).href;
	var T = document.getElementById("Actividad").value;
	var F = document.getElementById("Fecha").value;
	var H = document.getElementById("Hora").value;
	var P = document.getElementById("Personas").value;
	document.getElementById("flash_res_"+A).href = L + "&T=" + T + "&F=" + F + "&H=" + H + "&P=" + P;
//	alert(document.getElementById("flash_res_"+A).href);
}

// ON LOAD
window.onload=function(){
	Nifty("a.shadow_link","tr big transparent");
}

