var MSG_NOTHING_SELECTED_ITEMS = 'MSG_NOTHING_SELECTED_ITEMS';

function sp2Detect(par){
	var mv=navigator.appMinorVersion;
	if(/sp2/i.test(mv)){
		return true;
	}else{
		return false;
	}
}

function sb_checkHref( href )
{
	var cuid_str = "_c_u_i_d_=" + Math.random();
	var s = href;
	if(s.indexOf("&")==-1) s += ((s.indexOf("?")==-1)? '?' : '&');
	else s += '&';
	s += cuid_str;
//	alert(s);

	return s;
}

function navigation_ns( pageto)
{
	dc(1);
	document.location.href = sb_checkHref(pageto);
}

function sf(formAction, dontask)
{
	var fo = document.forms[0];
	formAction = sb_checkHref(formAction);

	if( fo == null ) location.href = formAction;
	else
	{
		if (dontask == "1")
		{
			if(validate())
			{
				fo.action = formAction;
				fo.submit();
			}

		}
		else if (fo.sb_changes && fo.sb_changes.value == "1" && dontask != '1' && confirm(LOCALE_CONFIRMATION_APPLY_CHANGES))
		{
			if(validate())
			{
				fo.action = formAction;
				fo.submit();
			}
		}
		else location.href = formAction;
	}
}

/* Verify for valid extention files for upload in a Admin part*/
function doUpload(par){
	var vfile;
	vfile=document.getElementById("sb_path_update_file");	
	if(vfile.value.substr(vfile.value.length-4,4).toLowerCase()==".zip") {
		dc(1);
		document.forms[0].submit();
	}else{
		alert(par);
	}
}

/* Get the URL site basical (http://sitebuilder/)*/
function getSiteUrl(){	
/*	var	url=document.URL;
	var	ur2=url.match(/http:\/\/(.*?)[\/]/);
	var	ur3="";
	if(ur2)	ur3=ur2[0];
	surl=ur3.substr(0,ur3.length-1); */
	return __base__URL;
}



/* Update templates in Admin part*/
function doUpdate(par) {
	var elall,el2,el3;
	el3=document.getElementById('sb_id_chenged');
	el2=document.getElementsByName('sb_temlates_id').item(0); 
	elall=document.getElementsByName('sb_all');
	var i; 
	var exist=false; 
	var i=0; 
	do { 
		if(el2.value==elall[i].value) { 
			exist=true;
		} 
		i++; 
	}while(i!=elall.length); 
	if(el3.value=='false'){
		dc(1);
		document.forms[1].submit();
	}else{
		if(exist==false){
			dc(1);
			document.forms[1].submit();
		}else{ 
			if(window.confirm(par)==true){
				dc(1);
				document.forms[1].submit();
			}
		}
	}
}

/* Delete users in a User Managment (admin part)*/
function doDelete(par, par1)
{
var str="";
	var id=document.getElementById("id_del");
	var elt=document.getElementsByName("sb_cc_obj_ids[]");
	var j=0;
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).checked==true && !elt.item(i).disabled)
		{
			str=str+elt.item(i).value+",";
			j++;
		}
	}

	if(j==0)
	{
		alert(MSG_NOTHING_SELECTED_ITEMS);
		return false;
	}

	id.value=str;
	return confirm(j+" - "+par+ ". "+ par1);
}

/* Activate and deactivate users in a User Managment (admin part)*/
function doActivate(par)
{
	var str="";
	var id=document.getElementById("id_activate");
	var elt=document.getElementsByName("sb_cc_obj_ids[]");
	var j=0;
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).checked==true && !elt.item(i).disabled)
		{
			str=str+elt.item(i).value+",";
			j++;
		}
	}

	if(j==0)
	{
		alert(MSG_NOTHING_SELECTED_ITEMS);
		return false;
	}

	id.value=str;
	return true;
}

/* before retrieve ftp host IPs (admin part)*/
function doRetrieve()
{
	var str="";
	var id=document.getElementById("id_activate");
	var elt=document.getElementsByName("sb_cc_obj_ids[]");
	var j=0;
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).checked==true && !elt.item(i).disabled)
		{
			str=str+elt.item(i).value+",";
			j++;
		}
	}

	if(j==0)
	{
		alert(MSG_NOTHING_SELECTED_ITEMS);
		return false;
	}

	id.value=str;
	return true;
}

/* Get array users selected for deleting */
function doSelectForDel(check)
{
	var ch=check.checked;
	var elt=document.getElementsByTagName("input");
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).type.toLowerCase()=='checkbox' && !elt.item(i).disabled) elt.item(i).checked=ch;
	}
}

/* Validate submit in navigation */
function validate()
{	
	var no_save="";
	try
	{
		no_save=document.getElementById("sb_no_save").value;
	}
	catch(e) {}

	if(no_save=="") return true;
	else return false;
}

function navigation_new_win(to_submit, to_nonsubmit, pagefrom, pagefromurl)
{
	var check_result = check_form('', '', pagefrom, 'on');
	if(check_result == 1)
	{
		return false;
	}else if(check_result == 3)
	{
	//	alert(to_submit);
		window.open(sb_checkHref(to_nonsubmit), 'site_preview', "title=no, menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes");
	}else if(check_result == 2)
	{
//		window.open(to_nonsubmit, 'site_preview', "title=no, menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes");
	}
	else if(check_result == 0)
	{
		//window.open(to_nonsubmit, 'site_preview', "title=no, menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes");
	}
}


// return:
//		 0	-	true
//		 1	-	false
//		 2 - submit
//		 3 - nonsubmit	
function check_form(to_submit, to_nonsubmit, pagefrom, showpreview)
{
	if (document.forms.length == 0)
	{
		return 3;
	}
	if (document.forms.length == 1)
	{
		if (document.forms[0].sb_changes && document.forms[0].sb_changes.value == "1")
		{
//			msgAnswer = MsgBox(LOCALE_CONFIRMATION_APPLY_CHANGES, vbYesNoCancel)
//				alert('msgAnswer='+msgAnswer);

//			if (confirm(LOCALE_CONFIRMATION_APPLY_CHANGES))
			var useraction = '';
			var answer;
			Dialog( sb_checkHref(__base__URL+'/wizard/applychangesdlg.php'), function(useraction){ answer = useraction; ResponceToAnswer(answer, to_submit, to_nonsubmit, pagefrom, showpreview)}, useraction);
		}
		else
		if (document.forms[0].sb_submit && document.forms[0].sb_submit.value == "1")
		{
			if (to_submit != '')
				document.forms[0].mode.value=to_submit;
			else
				document.forms[0].mode.value=pagefrom;

			if (pagefrom != '')
				document.forms[0].sb_pageid.value=pagefrom;
			dc(1);
			document.forms[0].submit();
		}
		else
		{
//			navigation_ns ( to_nonsubmit );
			return 3;
		}
	}
	if (document.forms.length > 1)
	{
		if( !validate() )
				{
					//navigation_ns ( to_nonsubmit );
					return 3;
				}else{
					alert (LOCALE_APPLY_CHANGES_YOURSELF);
					return 1/*false*/;
				}
	}
	return 0/*true*/;
}

function ResponceToAnswer(answer,to_submit, to_nonsubmit, pagefrom, showpreview)
{
	
	if(answer == "yes")
	{
		if(null!=document.forms[0].sb_after_submit_url)
		{
			if( !validate() )
			{
				return 1/*false*/;
			}
			document.forms[0].sb_after_submit_url.value = sb_checkHref(to_nonsubmit);
		}
		else
		{
			if (showpreview =='on')
			{
				showpreview = document.forms[0].show_preview.value=showpreview;
/*
				if (to_submit == '')
				{
				window.open(to_nonsubmit, 'site_preview', "title=no, menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes");					
				}
*/
			}
			if (to_submit != '')
			{
				document.forms[0].mode.value=to_submit;
			}
			else {
				document.forms[0].mode.value=pagefrom;
			}
			if (pagefrom != '')
			{
				document.forms[0].sb_pageid.value=pagefrom;
			}
			if( !validate() )
			{
				return 1/*false*/;
			}
			try{
				var sb_save=document.forms[0].sb_edit_save.value
			}catch(e){
			}
			if(sb_save==1) {
				doSave(false);
				dc(1);
				document.forms[0].submit();
				return 2/*true*/;
			}
		}
		dc(1);
		document.forms[0].submit();
		return 2;
	}
	else
	{
		if (answer == "no")
		{
			if (to_nonsubmit != '')
			{
				navigation_ns ( to_nonsubmit );
			}
			else {
				document.forms[0].sb_changes.value = "";
				window.open(sb_checkHref(__base__URL+'/wizard/index.php?mode=preview'), 'site_preview', "title=no, menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes");
			}
			return 3;
		}
		else 
		{
			return 4;
		}
	}
}

function navigation( to_submit, to_nonsubmit, pagefrom)
{
	var check_result = check_form(to_submit, to_nonsubmit, pagefrom);
	
	if(check_result == 1)
	{
		return false;
	}else if(check_result == 3)
	{
		navigation_ns ( to_nonsubmit );
	}else if(check_result == 2)
	{
	}
	
	/*if (document.forms.length == 0)
	{
		navigation_ns ( to_nonsubmit );
		return;
	}*/

/*	if (document.forms.length == 1)
	{
		if (document.forms[0].sb_changes && document.forms[0].sb_changes.value == "1")
		{
			if (confirm(LOCALE_CONFIRMATION_APPLY_CHANGES))
			{
				document.forms[0].mode.value=to_submit;
				document.forms[0].sb_pageid.value=pagefrom;
				if( !validate() )
				{
					return false;
				}
				
				try{
					var sb_save=document.forms[0].sb_edit_save.value
				}catch(e){
				}

				if(sb_save==1) {
					doSave();
					return true;
				}
				document.forms[0].submit();
//				return;
			}
			else
			{
				navigation_ns ( to_nonsubmit );
			}
		}
		else {
			navigation_ns ( to_nonsubmit );
		}
	}
	if (document.forms.length > 1)
	{
		if( !validate() )
				{
					navigation_ns ( to_nonsubmit );
					return;
				}else{
					alert (LOCALE_APPLY_CHANGES_YOURSELF);
					return false;
				}
	}
	*/
	return true;

}

/* Apply action when MouseOver event fired in a Upper menu */
function doOver(table){
		var img_tool;
		img_tool=table.getElementsByTagName('img').item(0);
		var src_img=img_tool.src;
		src_img=src_img.substr(0,src_img.length-4)+'_over'+src_img.substr(src_img.length-4,4);
		img_tool.src=src_img;
}


/* Apply action when MouseOut event fired in a Upper menu */
function doOut(table){
		var img_tool;
		img_tool=table.getElementsByTagName('img').item(0);
		var src_img=img_tool.src;
		src_img=src_img.replace('_over','');
		img_tool.src=src_img;
}

function SetStatusMessage (message, icon, needalert)
{
	var stat_msg = document.getElementById('status_message');
	var old_icon = document.getElementById('stat_img');
	if(null==stat_msg || null==old_icon) return;
	var src='/skins/'+__sb_skin+'/images/empty.gif';
	old_icon.src = '/skins/'+__sb_skin+'/images/empty.gif';

	if (message != '' && message != 'undefined')
	{
		if (needalert && needalert != '' && needalert != 'undefined')
		{
			alert(message);
		}
		if (message.length > 55)
		{
			message = message.substr(0, 55)+'...';
		}

		if (!document.all)
		{
			switch (icon) {
				case 'MB_OK':
					old_icon.src='/skins/'+__sb_skin+'/images/icon_info.gif';
					stat_msg.innerHTML = message;
					break;
				case 'MB_STOP':
					old_icon.src='/skins/'+__sb_skin+'/images/icon_stop.gif';
					stat_msg.innerHTML = message;
				break;
				default:
					old_icon.src = '/skins/'+__sb_skin+'/images/empty.gif'
					stat_msg.innerHTML = '';
				break;
			}
		}
		else
		{
		switch (icon) {
			case 'MB_OK':
				document.getElementById('stat_img_stop').style.visibility = "hidden";
				document.getElementById('stat_img_ok').style.visibility = "visible";
				stat_msg.innerHTML = message;
				break;
			case 'MB_STOP':
				document.getElementById('stat_img_ok').style.visibility = "hidden";
				document.getElementById('stat_img_stop').style.visibility = "visible";
				stat_msg.innerHTML = message;
				break;
			default:
				document.getElementById('stat_img_stop').style.visibility = "hidden";
				document.getElementById('stat_img_ok').style.visibility = "hidden";
				stat_msg.innerHTML = '';
			break;
		}
		}
	}
	else
	{
		if (!document.all)
		{
			old_icon.src = '/skins/'+__sb_skin+'/images/empty.gif'
			stat_msg.innerHTML = '';
		}
		else
		{
			document.getElementById('stat_img_stop').style.visibility = "hidden";
			document.getElementById('stat_img_ok').style.visibility = "hidden";
			stat_msg.innerHTML = '';
		}
	}

}

function validLink(temp){
	var str=temp;
	while(str.match(/http:(.*?)\/preview/)!= null){
		str = str.replace(/http:(.*?)\/preview/,".");
	}
	return str;
}

/* Set in textual field value Site Identity */
function setIdent(idt){
	var	ident=idt;
	var	sb_ident=document.getElementsByName("sb_site_identity").item(0);
	if(sb_ident) sb_ident.value=ident;
}



	function setBaseUrl(SB_b) {
		document.write("<BASE href='"+getSiteUrl()+SB_b+"'/>");
	}


	var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) >= 4))
	var dialogWin = new Object()
/*
	function ShowApplyChangesDialog(url, width, height, returnFunc, args) 
	{
		if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) 
		{
			// Initialize properties of the modal dialog object.
			dialogWin.returnFunc = returnFunc
			dialogWin.returnedValue = ""
			dialogWin.args = args
			dialogWin.url = url
			dialogWin.width = width
			dialogWin.height = height
			dialogWin.name = (new Date()).getSeconds().toString()
			if (Nav4) 
			{
				// Center on the main window.
				dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width) / 2)
				dialogWin.top = window.screenY + ((window.outerHeight - dialogWin.height) / 2)
				var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width + ",height=" + dialogWin.height
			} 
			else 
			{
				// The best we can do is center in screen.
				dialogWin.left = (screen.width - dialogWin.width) / 2
				dialogWin.top = (screen.height - dialogWin.height) / 2
				var attr = "left=" + dialogWin.left + ",top=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width + ",height=" + dialogWin.height
			}
				// Generate the dialog and make sure it has focus.
				dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr)
				dialogWin.win.focus()
		}
		else
		{
			dialogWin.win.focus()
		}
	}


// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.
function deadend() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()
		return false
	}
}

// Since links in IE4 cannot be disabled, preserve 
// IE link onclick event handlers while they're "disabled."
// Restore when re-enabling the main window.
var IELinkClicks

// Disable form elements and links in all frames for IE.
function disableForms() {
	IELinkClicks = new Array()
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = true
			}
		}
		IELinkClicks[h] = new Array()
		for (i = 0; i < frames[h].document.links.length; i++) {
			IELinkClicks[h][i] = frames[h].document.links[i].onclick
			frames[h].document.links[i].onclick = deadend
		}
	}
}

// Restore IE form elements and links to normal behavior.
function enableForms() {
	for (var h = 0; h < frames.length; h++) {
		for (var i = 0; i < frames[h].document.forms.length; i++) {
			for (var j = 0; j < frames[h].document.forms[i].elements.length; j++) {
				frames[h].document.forms[i].elements[j].disabled = false
			}
		}
		for (i = 0; i < frames[h].document.links.length; i++) {
			frames[h].document.links[i].onclick = IELinkClicks[h][i]
		}
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() {
	if (Nav4) {
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = deadend
	} else {
		disableForms()
	}
	window.onfocus = checkModal
}
// As dialog closes, restore the main window's original
// event mechanisms.
function unblockEvents() {
	if (Nav4) {
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = null
		window.onfocus = null
	} else {
		enableForms()
	}
}

// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
function checkModal() {
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()	
	}
}
*/

function checkSymbol (value, symbol)
{
	  var success = /[\']/.test(value);
	  return success;
}

function checkFS(){
	try{
		var ff=document.getElementsByName("fullscreen").item(0);
	if(editor.fullscreen==true){
		ff.value="true";
	}else{
		ff.value="false";
	}
	}catch(e){
	}
}

function checkFS1(){
	try{
		var ff=document.getElementsByName("fullscreen").item(0);
	if(editor.fullscreen==true){
		editor.fFullscreen();	
	}
	ff.value="false";
	}catch(e){
	}
}

function checkPublishKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if (charCode == 13) //Delete button
	{
		dc(1);
		document.pub_form.cmd.value=1;document.pub_form.submit();
	}
}

function validateUpload( fileInputID, noUploadMsg, noImageMsg )
{
	var vfile = document.getElementById( fileInputID );
	if(!vfile || vfile.value=='')
	{
		alert(noUploadMsg);
		return false;
	}
	else
	if(vfile.value.substr(vfile.value.length-4,4).toLowerCase()==".gif" 
	|| vfile.value.substr(vfile.value.length-4,4).toLowerCase()==".png" 
	|| vfile.value.substr(vfile.value.length-4,4).toLowerCase()==".jpg" 
	|| vfile.value.substr(vfile.value.length-4,4).toLowerCase()==".bmp"
	|| vfile.value.substr(vfile.value.length-5,5).toLowerCase()==".jpeg")
	{
		return true;
	}
	else
	{
		alert(noImageMsg);
		return false;
	}
}

function AdminIPkeypress(evt, command)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode == 13) //Delete button
	{
		dc(1);
		document.ftp_hosts_form.cmd.value=command;document.ftp_hosts_form.submit();
	}
}

function isDigit(val)
{
	if (parseInt(val) > 0)
	{
		return true;
	}
	return false;
}

function verifySetupParams(f1)
{
	if (f1.sb_dbname.value == "") 
	{
		alert("Incorrect or empty DataBase Name!");
		f1.sb_dbname.focus();
		f1.sb_dbname.select();
		return false;
	}

	if (f1.sb_dbhost.value == "") 
	{
		alert("Incorrect or empty Data Base Host Name!");
		f1.sb_dbhost.focus();
		f1.sb_dbhost.select();
		return false;
	}

	if (f1.sb_dbuser.value == "") 
	{
		alert("Incorrect or empty DataBase User Name!");
		f1.sb_dbuser.focus();
		f1.sb_dbuser.select();
		return false;
	}

	return true;
}
