function si_baseURL() {
	return '/index.php?id=';
}
function si_submitForm(form,fCallBack) {
	var els={};
	$('textarea, input, select, file', form).each(function() {
		var sName=$(this).attr('name');
		var sValue=si_getVal($(this));
		els[sName]=sValue;
	});
	var u=form.attr('action');
	//alert(u);
	$.post(u,els,
		function(data){
			//alert(data);
			fCallBack(data);
		}
	);
	return false;
}
function si_trackClicks(obj,path,dir) {
	$('a',obj).each(function() {
		$(this).click(function(){
			if (si_linkOk($(this))) {
				var sBaseURL = document.location.href.indexOf('portaldev') != -1 ? 'portaldev' : 'portal';
				sBaseURL = '/http:\/\/' + sBaseURL + '.faxserver.com\/'+path+'\/';
				sBaseURL = sBaseURL.replace(/\/\//,'/'); // replace double slash for slash
				var sUrl = $(this).attr('href');
				var sTopurl = top.location.href;
				var aTemp = sTopurl.split('#');
				var sNewurl = aTemp[0] + '#' + sUrl.replace((document.location.href.indexOf('portaldev') != -1 ? /http:\/\/portaldev.faxserver.com/ : /http:\/\/portal.faxserver.com/), "");
				//alert(sNewurl);
				switch(dir) {
					case 'in-out':
					default:	
						top.location.href = sNewurl;
						location.href = sUrl;
					break;
					case 'out-in':
						location.href = sNewurl;
						obj.location.href = sUrl;
					break;
				}
			}
		});
	});
}
function si_linkOk(a) {
	//return true;
	if (
		a.attr('href').indexOf('javascript')!=-1 || 
		a.attr('href').indexOf('#')!=-1 || 
		(typeof a.attr('onclick')!='undefined'	&& a.attr('onclick')!='') || 
		(typeof a.attr('target')!='undefined'	&& a.attr('target')!='')
	)
		return false;
	else 
		return true;
}
function si_loginFormHandler(data) {
	if(success) {
		$(si_getElement()).fadeOut("slow",function() {
			$(this).dialog( 'destroy' );
			document.location.reload();
		});
	}
	else {
		$('#wlpeMsg').hide().html(data).fadeIn("slow").oneTime(3000, function(){
			$(this).fadeOut("slow");
		});
		/*
		var m = $(data).find('#wlpeMessage');
		if (m.html()!=null) {
			$('#wlpeMsg').hide().html(data).fadeIn("slow").oneTime(3000, function(){
				$(this).fadeOut("slow");
			});
		}
		else {
			$(si_getElement()).fadeOut("fast",function() {
				$(this).html(data);
				//alert(data);
				var h = $(this).children('div:first').height();
				var w = $(this).children('div:first').width();
				$(this).animate({
					width: '+='+w+'px',
					height: '+='+h+'px',
					opacity: 1
					}, 1500
				);
			});
			si_activateAjaxForm($(si_getElement()).children('form').attr('id'),'');
		}*/
	}
}
function si_contactFormHandler(data) {
	alert('fds');
	if(success) {
		$(si_getElement()).fadeOut("slow",function() {
			$(this).dialog( 'destroy' );
			document.location.reload();
		});
	}
	else {
		$('#wlpeMsg').hide().html(data).fadeIn("slow").oneTime(3000, function(){
			$(this).fadeOut("slow");
		});
		/*
		var m = $(data).find('#wlpeMessage');
		if (m.html()!=null) {
			$('#wlpeMsg').hide().html(data).fadeIn("slow").oneTime(3000, function(){
				$(this).fadeOut("slow");
			});
		}
		else {
			$(si_getElement()).fadeOut("fast",function() {
				$(this).html(data);
				//alert(data);
				var h = $(this).children('div:first').height();
				var w = $(this).children('div:first').width();
				$(this).animate({
					width: '+='+w+'px',
					height: '+='+h+'px',
					opacity: 1
					}, 1500
				);
			});
			si_activateAjaxForm($(si_getElement()).children('form').attr('id'),'');
		}*/
	}
}
function si_activateAjaxForm(form,returnListener) {
	si_positionLabels();
	$(form+' button').click(function(){
		si_submitForm($(form),$(this).val(),returnListener);
	});
	$(form).submit(function() {
		return false;
	});
}
function si_submitFormHintput(f) {
	$('textarea, input, select', f).each(function() {
		var sTitle=$(this).attr('title');
		var sValue=si_getVal($(this));
		if(sValue==sTitle) $(this).val('');
	});
	return true;
}
function si_getElement() {
	return '#dialog';
}
function si_debugAjax(xhr) {
	for (var name in xhr) s+=name=='channel' || name=='responseText' ? '' : name+" : "+xhr[name]+"\n";
	alert(s);
}
function si_userOptions(el,lang) {
	var rel=el.attr('rel');
	switch(rel) {
		case 'login':
		si_login(el,lang);
		break;
		case 'logout':
			si_logout(el,lang);
		break;
		case 'profile':
			si_profile(el,lang);
		break;
		case 'register':
			si_register(el,lang);
		break;
	}
}
function si_login(el,lang) {
	var baseURL = si_baseURL();
	var u = baseURL+'2&lang='+lang;
	document.location.href=baseURL+'2&lang='+lang; return ;
	var title=el.attr('title');
	$(si_getElement()).load(u,
		function (responseText, status, XMLHttpRequest) {
			//var s='';
			//si_debugAjax(XMLHttpRequest);
			//$(this).html(responseText);
			si_dialog({
				modal: true,
				width: 350,
				height: 220,
				resizable: false,
				title: title
			},
			$(this));
			si_activateAjaxLoginForm('#wlpeLoginForm','wlpeUser');
		}
	);
}
function si_activateAjaxLoginForm(form,s,returnListener) {
	$('.hintput').hintput();
	$(form).labelize();
	$(form).submit(function() {
		var baseURL = si_baseURL();
		var els={};
		$('textarea, input, select', $(this)).each(function() {
			var sName=$(this).attr('name');
			var sValue=si_getVal($(this));
			els[sName]=sValue;
		});
		els.service = s;
		var u=baseURL+'30&type=ajax&service='+s;
		$.post(u,els,
			function(data){
				var success = data.indexOf(returnListener)!=-1 ? true : false;
				if(success) {
					$(si_getElement()).fadeOut("slow",function() {
						$(this).dialog( 'destroy' );
						document.location.reload();
					});
				}
				else {
					$('#wlpeMsg').hide().html(data).fadeIn("slow").oneTime(3000, function(){
						$(this).fadeOut("slow");
					});
				}
			}
		);
		return false;
	});
}
function si_profile(el,lang) {
	var baseURL = si_baseURL();
	var u=baseURL+'54';
	document.location.href=u;
	/*
	var title=el.attr('title');
	$('#dialog').load(u,
		function(responseText, textStatus, XMLHttpRequest) {
			//$(this).html(responseText);
			si_dialog({
				modal: true,
				width: 850,
				height: 500,
				resizable: true,
				title: title
			},
			$(this));
			si_activateAjaxForm('#wlpeUserProfileForm','wlpeUser');
		}
	);*/
}
function si_register(el,lang) {
	var baseURL = si_baseURL();
	var u=baseURL+'3&lang='+lang;
	document.location.href=u;
}
function si_modlinks() {	
	$("a[rel^='modlog']").click(function() {
		var r=$(this).attr('rel');
		var a=r.split(':');
		var d=typeof a[1]!='undefined' ? a[1].split(';') : new Array(500,500);
		var u=$(this).attr('href');
		//alert(u+' - '+d[0]);
		$('#dialog').load(u,
			function() {
				si_dialog({
					modal: true,
					width: parseInt(d[0]),
					height: parseInt(d[1]),
					resizable: true,
					title: $(this).attr('title')
				},
				$(this));
			}
		);
		return false;
	});
}
function si_logout() {
	var baseURL = si_baseURL();
	var els={};
	els.type = 'ajax';
	els.service = 'logout';
	$.post(baseURL+'30',els,
		function(data){
			document.location.href='/';
		}
	);
}
function si_positionLabels() {
	var max = 0;
	$("form.indent label").each(function(){
	if ($(this).width() > max)
		max = $(this).width();   
	});
	$("form.indent label").width(max);
}
function si_sizeUpFooter() {
	var max = 0;
	$(".footer_divider").each(function(){
	if ($(this).height() > max)
		max = $(this).height();
	});
	$(".footer_divider").height(max);
}
function si_dialog(opt,obj) {
	var h=obj.html();
	//obj.html(h+'fjdksfjdkls');
	opt.close=function(event,ui) {
		$(this).dialog( 'destroy' );
	};
	obj.hide().dialog(opt).fadeIn("slow");
}
function si_getVal(el) {
	var n=el.attr('name');
	var v="";
	var type=el.attr('type');
	switch(type) {
		case "checkbox":
		case "radio":
			if(el.is(':checked')) v=el.val();
		break;
		case "text":
		case "select-one":
		case "submit":
		default:
			v=el.val();
		break;
	}
	return v;
}
function si_sitemap() {
	$('ul#sitemap li a').each(function() {
		if($(this).parent('li').children('ul').length) {
			var t=$(this).text();
			$(this).html('<span class="tree">'+t+'</span>').parent('li').children('ul').css('margin-left','100px');
		}
	});
}
function si_resizeContent() {
	if(location.href.indexOf('forum')!=-1) return "";
	var screenH = screen.height;
	var screenW = screen.width;
	if(document.all) {
		availW = document.body.clientWidth;
		availH = document.body.clientHeight;
	}
	else {
		availW = innerWidth;
		availH = innerHeight;
	}
	var midH=$('#container_mid').height();
	var contH=$('#content').height();
	var a=new Array('top','mid','bot','abyss');
	var total_height=0;
	for(var i=0;i<a.length;i++) total_height+=$('#container_'+a[i]).height();
	//if(availH>total_height)
	var h=midH+(availH-(total_height+33));
	//alert(availH + ' - ' + total_height + ' h ' +h);
	$('#container_mid').height(h<midH ? midH : h);
	return true;
}