var path = "http://"+document.domain+"/";

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		//alert('Please provide a valid email address');
		return false;
	}
	return true;
}

function reg_val(f){
	/*var e = checkEmail(f.uname.value);
	if(!e){
		alert("Email is incorect");
		return false;
	}*/
	
	return true;
}

function onerror_v(e){
	
}

window.onerror = onerror_v;

var stringer = "";
function getUrl2Tab(){
	var t = document.createElement("INPUT");
	t.value = stringer;//window.location; //document.getElementById('russ').value;//
	var tab = parseUrl(t.value);
	return tab;
}

function getHashSite(){
	var myURL = getUrl2Tab();
	if(myURL.hash != ""){
		myURL.hash = myURL.hash.replace("#","");
		var tab = myURL.hash.split("=");
		switch(tab[0]){
			case "alt":
				getAlertContent(tab[1], function(val){
					o = eval(val);
					if(typeof o == 'object'){
						o = o[0];
						showAlert(o.title,o.text);
					}
				});
				break;
			case "msgval":
				getMessageContent(tab[1], function(val){
					o = eval(val);
					if(typeof o == 'object'){
						o = o[0];
						showAlert(o.title,o.text);
						if(typeof o.form_data == 'object'){
							formSetValueInChild(Base64.decode(o.form_name), o.form_data);
						}
					}
				});
				break;
		}
	}
}

function formSetValueInChild(fn, fd){
	var foq = document.forms.length;
	for(var f=0;f<foq;f++){
		if(document.forms[f].name == fn){
			for(var i in fd){
				var obj = document.forms[f][i];
				if(typeof obj == 'object'){
					switch(obj.type.toLowerCase()){
						case 'text':
							obj.value = Base64.decode(fd[i]);
							break;
						case 'checkbox':
							obj.checked = true;
							break;
					}
					
					if(obj.tagName.toLowerCase() == 'textarea')
						obj.value = Base64.decode(fd[i]);
				}
				
			}
			
		}
	}
}

function getAlertContent(aid, fun){
	post="aid="+aid;
	system_i.push_last(new Array(path+"get_alertcontent.php",post,function(val){
		fun(val);
	} ));
}
function getMessageContent(aid, fun){
	post="";
	system_i.push_last(new Array(path+"get_validatecontent.php",post,function(val){
		fun(val);
	} ));
}

function parseUrl(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(\?[^#]+)*(#.+)?$/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                select:RegExp.$7,
                hash:RegExp.$8};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",select:"",hash:""};
    }
}

function parseUrlFromString(data) {
    var e=/((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(\?[^#]+)*(#.+)?/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}



function setNewUrl(url){
	window.location.href = url;
	stringer = url;
	setTimeout(function(){ getHashSite(); },100);
	return false;
}

function showAlert(title, content){
	OpedDialog({title:Base64.decode(title),
	        info:Base64.decode(content),
	        img:"",
	        width:"290",
	        height:"174",
	        maxHeight:"400",
	        button:{
	            "button_cancel":"close"
	        }

	        });
	//alert(Base64.decode(title)+":\n\n"+Base64.decode(content));
}

function OpedDialog(data){
        var dil = CreateDialogBox();

        if(typeof data == "object"){
            if(!data.img){
                $(dil).find("div.head img").css("visibility","hidden")
            }

            var wind = $(dil).find(".pop_form_B");

            if(data.title != "")
                wind.find(".head").get(0).innerHTML = data.title;

            var con = wind.find("div.content").get(0);
            
            $(con).css("position","relative");
            if(data.height == 'auto'){
            	data.height = 600;
            	data.maxHeight = 600;
            }
            if(data.height != 'auto' && data.height != '')
            	$(con).css("height",data.height+"px");

            if(data.info && data.info != ""){
                $(con).find('.value').get(0).innerHTML = data.info;
                if($(con).height() < 60)
                    $(con).css("height","60px");
                if(data.maxHeight && $(con).height() > data.maxHeight)
                    $(con).css("height",data.maxHeight+"px").css("overflow-y","scroll");
            }
            else{
                $(con).css("height","60px");
            }
            
            $(con).css("width",data.width+"px");
            $(wind).css("width",data.width+"px");
            
            $(wind).css("left",(($(window).width()-data.width)/2)+"px");
            $(wind).css("top",(($(window).height()-data.height)/2)+"px"); 

            $(con).find("div.bg").css("width",data.width+"px");
            $(con).find("div.bg").css("height",data.height+"px");

            if(data.button && typeof data.button == "object"){
                for (var i in data.button){
                    if(data.button[i] == 'close')
                    	$("#"+i).get(0).onclick = function(e){
			               if(dil.parentNode)
			                        dil.parentNode.removeChild(dil);
			               return false;
			            };
			        else
                    	$("#"+i).get(0).onclick = data.button[i];
                }
            }

            var but =  wind.find(".close_popup").get(0);
            but.onclick = function(e){
               if(dil.parentNode)
                        dil.parentNode.removeChild(dil);
            };
        }

        $("#popup").get(0).appendChild(dil);
        
        return dil;
    }

    function CreateDialogBox(){
        var box = '<div class="pop_form_B">'
		+'<div class="pop_bg_B" id="pop_bg_B"></div>'
		+'<div class="pop_form_B" id="pop_form_B">'
		+'<div class="head">Edit Profile</div>'
		+'<div class="close_popup"><a href="#" onclick="return false;"></a></div>'
		
		+'<div class="content">'
		+'	<div class="bg">'
		+'		<div class="w1"></div>'
		+'		<div class="w2"></div>'
		+'		<div class="cb"></div>'
		+'		<div class="w3"></div>'
		+'		<div class="w4"></div>'
		+'		<div class="cb"></div>'
		+'	</div>'
				
		+'	<div class="value" style="padding:8px; height:159px;">'
			
		+'	</div><?/*padding*/?>'
		+'</div> <?/*content*/?>'
		
		+'</div>'
		+'</div>';
	
	    var pop = $("#popuptemp").get(0);
	    pop.innerHTML += box;
	    return pop.lastChild;
	}
    
	function formSubmit(o,a){
		var f = findParent(o,'FORM');
		if(f != false){
			if(typeof a == 'string')
				f.action = a;
			f.submit();
		}
		return false;
	}
			
	function findParent(o,tagName){
		var par = o.parentNode;
		if(typeof par == undefined || par == null) return false;
		if(par.tagName != tagName)
			return findParent(par, tagName);
		else
			return par;
	}
	
	var Base64 = {
	 
		// private property
		_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
	 
		// public method for encoding
		encode : function (input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = Base64._utf8_encode(input);
	 
			while (i < input.length) {
	 
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
	 
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
	 
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
	 
				output = output +
				this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	 
			}
	 
			return output;
		},
	 
		// public method for decoding
		decode : function (input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
	 
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	 
			while (i < input.length) {
	 
				enc1 = this._keyStr.indexOf(input.charAt(i++));
				enc2 = this._keyStr.indexOf(input.charAt(i++));
				enc3 = this._keyStr.indexOf(input.charAt(i++));
				enc4 = this._keyStr.indexOf(input.charAt(i++));
	 
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
	 
				output = output + String.fromCharCode(chr1);
	 
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
	 
			}
	 
			output = Base64._utf8_decode(output);
	 
			return output;
	 
		},
	 
		// private method for UTF-8 encoding
		_utf8_encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	 
			for (var n = 0; n < string.length; n++) {
	 
				var c = string.charCodeAt(n);
	 
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
	 
			}
	 
			return utftext;
		},
	 
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
	 
			while ( i < utftext.length ) {
	 
				c = utftext.charCodeAt(i);
	 
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
	 
			}
	 
			return string;
		}
	 
	}
	
window.onload = function(){
	stringer = window.location.href;
	getHashSite();
}

function prevObj(o){
	if(!o.previousSibling) return o;
	if(o.previousSibling.nodeName == "#text") //FF
		return prevObj(o.previousSibling);
	return o.previousSibling;
}

function nextObj(o){
	if(o.nextSibling.nodeName == "#text") //FF
		return nextObj(o.nextSibling);
	return o.nextSibling;
}

function showStruct(o){
	t = "";for(var i in o){t += i+" = "+o[i]+"\n";}alert(t);
}
	
/*
window.onbeforeunload=function(e){ //dziala pod FF ale IE niedziala
	e.returnValue="Any return string here forces a dialog to appear when user leaves this page";
}

window.onbeforeunload=function(){ //dziala pod FF i IE
	return "Any return string here forces a dialog to appear when user leaves this page";
}
window.onunload = function(e){
	alert('dd');
}
*/

function deleteAlert(code, _this){
	getAlertContent(code, function(val){
		o = eval(val);
		if(typeof o == 'object'){
			o = o[0];
			showAlertW(o.title,o.text,_this);
			}
	});
	return false;
}

function showAlertW(title, content,obj){
	OpedDialog({title:Base64.decode(title),
	        info:Base64.decode(content),
	        img:"",
	        width:"290",
	        height:"174",
	        maxHeight:"400",
	        button:{
	            "button_cancel":"close",
	            "button_accept":function(){
	            	setNewUrl(obj);
	            	return false;
	            }
	        }

	        });
}
