var CurrentPage=1;
var MaxPages=4;

var result;

function CheckBoxClick(value, id, version) {

 var c=0;
 
 var Components = ((version == 2) ? ComponentsV2 : ComponentsV3);
 
	if (value=='portal' && $("#"+id).attr("checked")) {
		$("#v"+version+"-module-check-forum").attr("checked","true");
		c++;
	}
	
	if (value=='userblogs' && $("#"+id).attr("checked")) {
		$("#v"+version+"-module-check-forum").attr("checked","true");
		c++;
	}
	
	var wasPortal = 0;
	var wasUserBlogs = 0;
	if (value=='forum' && !$("#v"+version+"-module-check-forum").attr("checked")) {
		wasPortal = $("#v"+version+"-module-check-portal").attr("checked");
		wasUserBlogs = $("#v"+version+"-module-check-userblogs").attr("checked");
		$("#v"+version+"-module-check-portal").removeAttr("checked");
		$("#v"+version+"-module-check-userblogs").removeAttr("checked");
	
	}
 
	for (var key in Components) {
		if($("#v"+version+"-module-check-"+key+"").attr('checked')) {
			c++;
		}
	}
 
	if (c==0) {
		$("#"+id).attr("checked", "true");
	}
 
	if (wasPortal && value=='forum' && c==0) {
		$("#v"+version+"-module-check-portal").attr("checked","true");
	}
	
	if (wasUserBlogs && value=='forum' && c==0) {
		$("#v"+version+"-module-check-userblogs").attr("checked","true");
	}
 
 var main=$("#mainComponent").get(0);
 var saveCurrentValue=main.options[main.selectedIndex].value;

 main.options.length=0;

 var i=0;

 for (key in Components) {
	if ($("#v"+version+"-module-check-"+key+"").attr("checked")) {
		if (key==saveCurrentValue) {
			main.options[i++]=new Option(Components[key],key,false,true);
		} else {
			main.options[i++]=new Option(Components[key],key,false,false);
		}
	}
 }
 
 return true;
}

function versionChange(version, makeCheck) {
//	alert(version);
	if(makeCheck) {
		$('#version-check-v' + version).attr('checked', true);
	}
	
	var disabledComponents = ((version == 2) ? ComponentsV3 : ComponentsV2);
	for (compId in disabledComponents) {
		$('#v'+((version == 2) ? 3 : 2)+'-module-check-'+compId).attr('disabled', 1);
	}
	var enabledComponents = ((version == 3) ? ComponentsV3 : ComponentsV2);
	for (compId in enabledComponents) {
		$('#v'+version+'-module-check-'+compId).removeAttr('disabled');
	}
	
	var main=$("#mainComponent").get(0);
	
	main.options.length=0;
	
	var i=0;
	
	for (key in enabledComponents) {
		if ($("#v"+version+"-module-check-"+key+"").attr("checked")) {
			main.options[i++]=new Option(enabledComponents[key],key,false,false);
		}
	}
	$('#modules-v'+version).show();
	$('#modules-v'+(version == 2 ? 3 : 2)).hide();
	
	$('#langs_v'+version).show();
	$('#langs_v'+(version == 2 ? 3 : 2)).hide();
	$('#langs_v'+version).removeAttr('disabled');
	$('#langs_v'+(version == 2 ? 3 : 2)).attr('disabled', true);
	
	if (version == 2) {
		$('#themeSelector').show();
		$('#themeSelector').removeAttr('disabled');
		
		$('#themeSelectorV3').hide();
//		$('#themeSelectorV3').attr('disabled', true);
	}
	else if(version==3) {
		$('#themeSelector').hide();
		$('#themeSelector').attr('disabled', true);
		
//		$('#themeSelectorV3').removeAttr('disabled');
		$('#themeSelectorV3').show();
	}
	
	
	if (version == 2) {
		setSrc();
	}
	else {
//		setSrcV3();
		themeSetAdvAll(1);
	}
}

function filterCookie()
{
	var c = document.cookie;
	var cs = c.split('; ');
	var res = '';
	for (var i=0;i<cs.length;i++)
	{
		crow = cs[i].split('=');
		if (crow.length >=2 && crow[0] == 'PHPSESSID')
		{
			res += crow[0] + '=' + crow[1] + '; ';
		}
	}
	return res;
}

function CheckPage(page) {

       $("#indicator").show();
       $("#posterrors").hide;

       var formparams=$("#registrationForm input[@type=text]").serialize();
       formparams=formparams+"&"+$("#registrationForm input[@type=file]").serialize();
       formparams=formparams+"&"+$("#registrationForm input[@type=checkbox][@checked]").serialize();
       formparams=formparams+"&"+$("#registrationForm input[@type=password]").serialize();
       formparams=formparams+"&"+$("#registrationForm input.hiddenThemeId").serialize();

       result=true;

       $.ajax({
          type: "POST",
          cookie: filterCookie(),
          async: false,
          url: "process.php",
          data: "action=RegistrationAJAX&page="+page+"&"+formparams,
          redirectURL: 'index_original.php?view=RegistrationAJAX&rand=' + Math.floor(1000000 * Math.random()),
          error: function(){
                $("#indicator").hide();
				alert('Getting data from server fail! Try again. ');
				result=false;
			},
          success: function(msg){
              $("#indicator").hide();
				if (msg.length>0) {
					if (msg.indexOf('<!--no errors-->') != 0) {
						result=false;
						document.location.href="#errors";
						$("#pageerrors").html(msg);
							$("#pageerrors").slideDown("fast");
					}
				} else {
					result=false;
					alert('Empty server response! Try again.');
				}
			}
		});

        return result;
}

function Buttons(lang) {
		lang = lang || 'en';
       if (CurrentPage==1) {
          $('#prevbut').hide();
       } else {
          $('#prevbut').show();
       }
       if (CurrentPage==MaxPages) {
          $('#nextbut').attr("src",siteURL+"/pix/"+lang+"/but-make-portal.jpg");
       } else {
          $('#nextbut').attr("src",siteURL+"/pix/"+lang+"/but-next.jpg");
       }

       $("#pageerrors").hide();
}

function Next(lang)
{
	if (CurrentPage<=MaxPages) {
		if (CheckPage(CurrentPage)) {
			$('#page'+CurrentPage).hide();
			CurrentPage++;
			if (CurrentPage > MaxPages) {
				$("#pageerrors").hide();
				$("#page"+MaxPages).hide();
				$("#wizbuttons").hide();
				$("#indicator-fin").show();
				$('#registrationForm').submit();

			} else {
				$('#page'+CurrentPage).show();
				Buttons(lang);

				document.location.href="#topform";
			}
		} 	
	}
};

function Back(lang)
{
	if (CurrentPage>1)  {
		$('#page'+CurrentPage).hide();
		CurrentPage--;
		$('#page'+CurrentPage).show();
		Buttons(lang);
		document.location.href="#topform";
	}
}

$("document").ready(function() { 
	Buttons(GlobalLang); 
});

function themeSelectorMode(active, passive) {
	if (active == 'Advanced') {
		themeAdvShow();
	}
	if (active == 'Simple') {
		themeSetAdvBySmpl(document.getElementById('themeSelectorV3Select'));
	}
	$('#themeSelectorV3' + passive).hide();
	$('#themeSelectorV3' + active).show();
}

function themeSetAdvBySmpl(select) {
	var curThemeId = select.options[select.selectedIndex].value;
	themeSetAdvAll(curThemeId, 1);
}

function themeSetAdvAll(themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	themeSetAdvForum(themeId, fromSimple);
	themeSetAdvWiki(themeId, fromSimple);
	themeSetAdvBlog(themeId, fromSimple);
	themeSetAdvGallery(themeId, fromSimple);
}

function themeSetAdvForum(themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	themeSetAdv('Forum', themeId, fromSimple);
}

function themeSetAdvWiki(themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	themeSetAdv('Wiki', themeId, fromSimple);
}

function themeSetAdvBlog(themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	themeSetAdv('Blog', themeId, fromSimple);
}

function themeSetAdvGallery(themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	themeSetAdv('Gallery', themeId, fromSimple);
}

function themeSetAdv(component, themeId, fromSimple) {
	fromSimple = fromSimple || 0;
	if ($('#' + component.toLowerCase() + 'ThemeId_' + themeId).attr('disabled')) {
		themeId = $('#theme' + component + 'Id').attr('value');
		themeId = themeId || 1;
	}
	if (!themesV3Adv[themeId]) {
		alert('Unknown themeId: ' + themeId);
		return;
	}
	var theme = themesV3Adv[themeId];
	var name = theme['name'];

	if (typeof(theme['galleryUrl']) == 'string' && theme['galleryUrl'] != '')
	{
		name = '<a href="' + theme['galleryUrl'] + '" target="_blank">' + name + '</a>';
	}
	
	// set hidden value
	$('#theme' + component + 'Id').attr('value', themeId);
	// set page label
	$('#themeSelectorV3CurrentName' + component + '').attr('innerHTML', name);
	// set layer label
	$('#' + component.toLowerCase() + 'ThemePreviewNameId').attr('innerHTML', name);
	
	// set page preview
	
	// set layer preview
	$('#' + component.toLowerCase() + 'ThemePreviewDivId img').attr('src', theme[component.toLowerCase() + 'PreviewUrl']);
	
	// set radio checked
	$('#' + component.toLowerCase() + 'ThemeId_' + themeId).attr('checked', 1);
	
	
	
}

function themeAdvDone() {
	$('#themeSelectorV3AdvancedLayer').hide();
}

function themeAdvShow() {
	$('#themeSelectorV3AdvancedLayer').show();
}


function themeV3Preview(themeId, e, right) {
	if (!themesV3Adv[themeId]) {
		alert('Unknown themeId: ' + themeId);
		return;
	}
	var theme = themesV3Adv[themeId];
	
	if (theme.forumName != '') {
		$('#themeV3PreviewHoverForum').attr('innerHTML', '<img width="200" height="130" src="' + theme.forumPreviewUrl + '">');
	} else {
		$('#themeV3PreviewHoverForum').attr('innerHTML', '<img width="200" height="130" src="' + noThemeUrl + '">');
	}
	
	if (theme.wikiName != '') {
		$('#themeV3PreviewHoverWiki').attr('innerHTML', '<img width="200" height="130" src="' + theme.wikiPreviewUrl + '">');
	} else {
		$('#themeV3PreviewHoverWiki').attr('innerHTML', '<img width="200" height="130" src="' + noThemeUrl + '">');
	}
	
	if (theme.blogName != '') {
		$('#themeV3PreviewHoverBlog').attr('innerHTML', '<img width="200" height="130" src="' + theme.blogPreviewUrl + '">');
	} else {
		$('#themeV3PreviewHoverBlog').attr('innerHTML', '<img width="200" height="130" src="' + noThemeUrl + '">');
	}
	
	if (theme.galleryName != '') {
		$('#themeV3PreviewHoverGallery').attr('innerHTML', '<img width="200" height="130" src="' + theme.galleryPreviewUrl + '">');
	} else {
		$('#themeV3PreviewHoverGallery').attr('innerHTML', '<img width="200" height="130" src="' + noThemeUrl + '">');
	}
	
	$('#themeV3PreviewThemeName').attr('innerHTML', theme.name);
	$('#themeV3PreviewThemeRating').attr('innerHTML', theme.rating);
	
	$('#themeV3PreviewHover').show();
	
	themeV3PreviewMove(e, right);
}

function themeV3PreviewMove(e, right) {
	var posX = e.pageX-Math.round(($(document).width() - $(document.body).width())/2)-20-(right ? 454 : 0);
	var posY = e.pageY-140;
	
	$('#themeV3PreviewHover').css('left', posX);
	$('#themeV3PreviewHover').css('top', posY);
}

function themeV3PreviewHide() {
	$('#themeV3PreviewHover').hide();
}

