// (c) MOnk / www.getaway.lv


function popup(mylink, windowname) {
	
	if (! window.focus)return true;
	
	var href;

	if (typeof(mylink) == 'string') {
		href=mylink;
	} else P
		href=mylink.href;
		window.open(href, windowname, 'status=no, menubar=no, fullscreen=yes, scrollbars=yes, resizable');
		return false;
		
}


/***************************  BB url un img *****************************/


var response = null
var lPath

function prompt2(prompttitle, message, toFunc) {

	promptbox = document.createElement('div');
	promptbox.setAttribute ('id' , 'prompt')
	promptbox.setAttribute ('onKeyPress' , 'msKP()')
	document.getElementsByTagName('body')[0].appendChild(promptbox)
	promptbox = eval("document.getElementById('prompt').style")
	promptbox.position = 'absolute'
	promptbox.top = 200
	promptbox.left = 400
	promptbox.width = 500
	var promptWidth = 500
	var w = 1024;
	
	if (document.all || document.layers) { w = screen.availWidth; }

	promptbox.left = (w - promptWidth)/2;
	promptbox.border = 'outset 1px #bbbbbb'
	document.getElementById('prompt').innerHTML = "<table OnKeyPress='msKP(" + toFunc + ")' cellspacing='0' cellpadding='0' border='0' width='100%' ><tr valign='middle'><td height='22' style='text-indent: 2;' class='titlebar' nowrap colspan='2' width='498'>  " + prompttitle + "</td></tr></table>"
	document.getElementById('prompt').innerHTML = document.getElementById('prompt').innerHTML + "<table OnKeyPress='msKP(" + toFunc + ")' cellspacing='0' cellpadding='0' border='0' width='100%' id='suds' class='promptbox'><tr><td><br>" + message + "</td></tr><tr><td><input type='text' id='promptbox' OnKeyPress='msKP(" + toFunc + ")' onblur='this.focus()' class='promptbox'></td></tr><tr><td align='right'><br><input type='button' class='prompt' onMouseOver='this.style.border=\"1px solid #595959\"' onMouseOut='this.style.border=\"1px outset #dddddd\"' value='Paste' onClick='pasteURL()'>  <input type='button' class='prompt' value='Nodzēst' onMouseOver='this.style.border=\"1px solid #595959\"' onMouseOut='this.style.border=\"1px outset #dddddd\"' onClick='clearURL()'> <input type='button' class='prompt' value='OK' onMouseOver='this.style.border=\"1px solid #595959\"' onMouseOut='this.style.border=\"1px outset #dddddd\"' onClick='" + toFunc + "(document.getElementById(\"promptbox\").value); removePrompt(" + toFunc + ");'> <input type='button' class='prompt' value='Atcelt' onMouseOver='this.style.border=\"1px solid #595959\"' onMouseOut='this.style.border=\"1px outset #dddddd\"' onClick='removePrompt();'></td></tr></table>"


	document.getElementById("promptbox").focus();

}



function funcUrlBB(saite) {
	lPath = saite;

	if(lPath.length<=7) {
		alert('shit');
		return false;
	 } else {

		if (lPath.substring(0, 8).toLowerCase() == "https://" || lPath.substring(0, 7).toLowerCase() == "http://") {
			prompt2('Saites nosaukums', 'Norādi saites nosaukumu', 'funcUrlBBcomplete');
			return;
		} else {
			alert('Kļūdaina saite!');
		return false;
		}

	}

}



function funcUrlBBcomplete(lName) {

	if(lName.length <= 0) {

		alert('Kļūdaina saite!');
		return false;

	} else {

		var edtMsg;
		var curMsg = document.post.message.value;
		var imgBB = "[URL="+lPath+"]"+lName+"[/URL]";
		edtMsg = curMsg+imgBB;
		document.post.message.value = edtMsg;
		document.post.message.focus();

	 }

}



function funcImgBB(imgURL) {

	if(imgURL.length <= 0) {

		return false;

	 } else {

		var lead_slashes = imgURL.indexOf("//");
		var domain_start = lead_slashes + 2;
		var without_resource = imgURL.substring(domain_start, imgURL.length);
		var next_slash = without_resource.indexOf("/");
		var domain = without_resource.substring(0, next_slash);

		var imgURLgar = imgURL.length - 4;
		var imgURLgar2 = imgURL.length - 5;

		if ( (imgURL.substring(0, 8).toLowerCase() == "https://" || imgURL.substring(0, 7).toLowerCase() == "http://") && (imgURL.substring(imgURLgar, imgURL.length) == ".gif" || imgURL.substring(imgURLgar, imgURL.length).toLowerCase() == ".jpg" || imgURL.substring(imgURLgar, imgURL.length).toLowerCase() == ".png" || imgURL.substring(imgURLgar2, imgURL.length).toLowerCase() == ".jpeg") ) {

			var edtMsg;
			var curMsg = document.post.message.value;

			var imgBB = "[IMG]"+imgURL+"[/IMG]";
			edtMsg = curMsg+imgBB;
			document.post.message.value = edtMsg;
			document.post.message.focus();

		} else { alert('Kļūdaina saite!'); }

	}
}



function pasteURLposting(){

	document.post.message.focus()
	document.execCommand("Paste");

	return;

}



function pasteURL(){

	var clipData=window.clipboardData.getData("Text");
	document.getElementById('promptbox').value = clipData;
	document.getElementById('promptbox').focus();
	return;

}



function clearURL(){

	document.getElementById('promptbox').value = '';
	document.getElementById('promptbox').focus();
	return;

}



function removePrompt(toFunc){
	if (toFunc != "funcUrlBBcomplete") { document.getElementsByTagName("body")[0].removeChild(document.getElementById("prompt")); }
}


function callProm(bbnumber){
	if (bbnumber==14) {
	prompt2('Ievietot bildi','Norādi saiti uz bildi (formā http://www.saits.lv/bilde.jpg)', 'funcImgBB');
}

	if (bbnumber==16) {
	prompt2('Ievietot saiti', 'Norādi saiti (formā http://www.saits.lv/fails.html)', 'funcUrlBB');
}

}



function netscapeKeyPress(e) {
    if (e.which == 13)
        alert('Enter pressed');
}



function msKP(sendto) {

    if (window.event.keyCode == 13) {


    if (window.event.keyCode == 13) { funcImgBB(document.getElementById("promptbox").value); }
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("prompt"));
    }


    if (window.event.keyCode == 27) {
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("prompt"));
    }


	if (navigator.appName == 'Netscape') {
		window.captureEvents(Event.KEYPRESS);
		window.onKeyPress = netscapeKeyPress;
	}
}



function MaintainFocus() {

	document.getElementById("promptbox").focus()

}



function getVar(name) {

	get_string = document.location.search;
	return_value = '';

	do {

		name_index = get_string.indexOf(name + '=');

		if(name_index != -1) {

			get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);

			end_of_value = get_string.indexOf('&');

			if(end_of_value != -1) {
				value = get_string.substr(0, end_of_value);
			} else {
				value = get_string;
			}

			if(return_value == '' || value == '') {
				return_value += value;
			} else {
				return_value += ', ' + value;
			}
		}
	
	} while(name_index != -1)


	space = return_value.indexOf('+');

	while(space != -1) {

		return_value = return_value.substr(0, space) + ' ' +
		return_value.substr(space + 1, return_value.length);

		space = return_value.indexOf('+');

	}

	return(return_value);

}
