function toggleContTabs(div) {
	switch (div) {
	case "fav":
		$("#favTab").removeClass().addClass('tabOn');
		$("#disTab").removeClass().addClass('tab');
		$("#blogTab").removeClass().addClass('tab');
		$("#fav").show();
		$("#blog").hide();
		$("#discuss").hide();
		break;
	case "blog":
		$("#favTab").removeClass().addClass('tab');
		$("#disTab").removeClass().addClass('tab');
		$("#blogTab").removeClass().addClass('tabOn');
		$("#fav").hide();
		$("#blog").show();
		$("#discuss").hide();
		break;
	case "discuss":
		$("#favTab").removeClass().addClass('tab');
		$("#disTab").removeClass().addClass('tabOn');
		$("#blogTab").removeClass().addClass('tab');
		$("#fav").hide();
		$("#blog").hide();
		$("#discuss").show();
		break;
	}
}

function toggleSuggestTabs(div) {
	switch (div) {
	case "mem":
		$("#memTab").removeClass().addClass('tabOn');
		$("#specTab").removeClass().addClass('tab');
		$("#grpTab").removeClass().addClass('tab');
		$("#mem").show();
		$("#spec").hide();
		$("#grp").hide();
		break;
	case "spec":
		$("#memTab").removeClass().addClass('tab');
		$("#specTab").removeClass().addClass('tabOn');
		$("#grpTab").removeClass().addClass('tab');
		$("#mem").hide();
		$("#spec").show();
		$("#grp").hide();
		break;
	case "grp":
		$("#memTab").removeClass().addClass('tab');
		$("#specTab").removeClass().addClass('tab');
		$("#grpTab").removeClass().addClass('tabOn');
		$("#mem").hide();
		$("#spec").hide();
		$("#grp").show();
		break;
	}
}

function getProfile() {
	var id = $("#profileList").val();
	if (id != null) {
		$("#bopProfile").load("/profile.php?id="+id);
	}
}

function fontSizer(font,reload) {

	if (font) {
		Set_Cookie('orgFontSize', font, 30, '/', '', '' );
		window.location.reload();
	} else {
		if ( Get_Cookie( 'orgFontSize' ) ) {
			fontSizer(parseInt(Get_Cookie('orgFontSize')),false);
		}
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function check_length_all(){
	var countable = new Array();

	for (var i=0; i < countable.length; i++) {
		check_length(document.getElementById(countable[i]));
	};
}

function check_length(obj) { 
	var maxwords = 200;
	rem = document.getElementById(obj.name+'_remaining');
	var char_count = obj.value.length;
	var fullStr = obj.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var ary = cleanedStr.split(" ");

	var len = ary.length -1; 
	rem.innerHTML = maxwords - len; 
	if (len > maxwords) { 
		alert("Message in '" + obj.name + "' limited to " + maxwords + " words."); 
		ary = ary.slice(0,maxwords-1); 
		obj.value = ary.join(" "); // truncate additional words  
		rem.innerHTML = 0; 
		return false; 
	} 
	return true; 
}

function check_length_300(obj) { 
	var maxwords = 300;
	rem = document.getElementById(obj.name+'_remaining');
	var char_count = obj.value.length;
	var fullStr = obj.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var ary = cleanedStr.split(" ");

	var len = ary.length -1; 
	rem.innerHTML = maxwords - len; 
	if (len > maxwords) { 
		alert("Message in '" + obj.name + "' limited to " + maxwords + " words."); 
		ary = ary.slice(0,maxwords-1); 
		obj.value = ary.join(" "); // truncate additional words  
		rem.innerHTML = 0; 
		return false; 
	} 
	return true; 
}

function EntryForm(part) {
	
	$(".entry-form").hide();
	$("#form-part"+part).show();
	
	$("#myMenu li.active").removeClass();
	$("#myMenu #part"+part).addClass("active");
	
	if (part == 'video') {
		$("#shortlisted").hide();
	} else {
		$("#shortlisted").show();	
	}
	
	$('html').animate({scrollTop:0}, 'slow'); 
	
}

CURR = 0;
function saveEntryYE(cur, next, hide) {
	
	var required = {"step": [{},{'title':1,'firstname':1,'surname':1,'dob[day]':1,'dob[month]':1,'dob[year]':1,'address':1,'telno':1,'mobile':1,'email':1},{'background':1,'businessdescription':1,'yourmarket':1,'pricingstrategy':1,'developmentneeds':1,'personalqualities':1},{'businessname':1,'businessaddress':1,'businessregion':1,'businesspostcode':1,'businesstelno':1,'businessmobile':1,'businessemail':1,'businesswebsite':1,'businesscommenced[day]':1,'businesscommenced[month]':1,'businesscommenced[year]':1,'partners':1,'employeesft':1,'employeespt':1,'saleslast12':1,'salesnext12':1,'costslast12':1,'costsnext12':1,'incomelast12':1,'incomenext12':1,'expenselast12':1,'expensenext12':1},{'thefuture':1},{'whyme':1,'source':1,'signature':1,'declarationconfirm':1}]};
	
	errors = [];

	CURR = cur;
	$('#entryform :input').each(function(){
		if($(this).attr('type') != "hidden")
		{
			
			isRequired = eval( "required.step["+CURR+"]['"+$(this).attr('id')+"']")
			if(typeof(isRequired) && isRequired )
			{
				isTicked = $('#declarationconfirm').is(':checked');
				if( $(this).val() == '' || (CURR == 5 && $(this).attr('id')=='declarationconfirm' && !isTicked ) )
				{
					errors[errors.length] = $(this).attr('id');
					$(this).parent().addClass('error')
				}
			}
		}
	});
	//console.log( isTicked  )
	//console.log(errors.toString()); 
	
	if (errors.length) {
		$("#errorAlert").show();
		$('html').animate({scrollTop:0}, 'slow'); 
		for(i in errors)
		{
			$('#'+errors[i]).bind('keydown', function() {
  			$(this).parent().removeClass('error')
			});
			
			$('#'+errors[i]).bind('blur', function() {
  			if( $(this).val() != '' )
				{
					$(this).parent().removeClass('error')
				}
				else if(! $(this).parent().hasClass('error') )
				{
					$(this).parent().addClass('error')
				}
			});
		}
	}
	else
	{
		$("#errorAlert").hide()
		if(next == 99)
		{
			SubmitEntryYE()
		}
		else
		{
			saveEntry(next, hide)
		}
	}
			
}
function saveEntry(next, hide) {
	
	
	var data = $("#entryform").serialize();  
    
	//console.log(data);
	if (hide != 1)
		$("#saveAlert").show().pause(5000).fadeOut();
	
	$.post("/save_comp_entry.php", data, function(data,status) {
		// put save successful message up
		
		if (data.entryid) {
			$("#entryID").attr("value",data.entryid); // set the entryID for future saves
		}
		
		if (data.submitted == '1') {
			window.location='/awards/entry-form/complete/';
		}
	}, "json");
	
	if (next) {
		EntryForm(next);
	}

}
function SubmitEntryYE() {

    	var data = $("#entryform").serialize();
    	data += "&submit=1";
    
    	$.post("/save_comp_entry.php", data ,function(data,status){
    		// put save successful message up
    		$("#saveAlert").show().pause(5000).fadeOut();
    		
    		if (data.entryid) {
    			$("#entryID").val(data.entryid); // set the entryID for future saves
    		}
    		
    		if (data.submitted == '1') {

    				window.location='/awards/complete-ye/';

    			
    		}
    	}, 'json');

}

function SubmitEntry(next) {
    
    $("#errorAlert").hide();
    var errors = 0;
    
    var fname = $("#firstname").val();
    var sname = $("#surname").val();
    var address = $("#address").val();
    var email = $("#email").val();
    var bname = $("#businessname").val();
    var baddress = $("#businessaddress").val();
    var bpcode = $("#businesspostcode").val();
    var bmail = $("#businessemail").val();
    var btel = $("#businesstelno").val();
    var bweb = $("#businesswebsite").val();
    var bcomm = $("select[name=businesscommenced[day]").val()+"/"+$("select[name=businesscommenced[month]").val()+"/"+$("select[name=businesscommenced[year]").val();
    var struct = $("#businessstructure").val();
    var bdesc = $("#businessdescription").val();
    var bg = $("#background").val();
    var edu = $("#educationtraining").val();
    var market = $("#yourmarket").val();
    var inno = $("#innovation").val();
    var comp = $("#competitionsource").val();
    var means = $("#winningmeans").val();
    var sig = $("#signature").val();
    var dec = $("input[name=declarationconfirm]:checked").val();
    
    if (fname==''||sname==''||address==''||email==''||bname==''||baddress==''||bpcode==''||bmail==''||btel==''||bweb==''||bcomm==''||struct==''||bdesc==''||bg==''||edu==''||market==''||inno==''||comp==''||means==''||sig==''||dec=='undefined') {
        errors = 1;
    }
    
    if (errors > 0) {
        $("#errorAlert").show();       
    } else {
    
    	var data = $("#entryform").serialize();
    	
    	data += "&submit=1";
    
    	$.post("/save_comp_entry.php", data ,function(data,status){
    		// put save successful message up
    		$("#saveAlert").show().pause(5000).fadeOut();
    		
    		if (data.entryid) {
    			$("#entryID").val(data.entryid); // set the entryID for future saves
    		}
    		
    		if (data.submitted == '1') {
    			if (next) {
    				window.location='/awards/complete-ye/';
    			} else {
    				window.location='/awards/entry-form/complete/';
    			}
    			
    		}
    	}, 'json');
     }

}

function EntryPrint(id) {
	saveEntry('',1);
	
	if (id) {
		id = '?competitionID='+id;
	}
	
	window.open ("/awards/entry-form/print/"+id,"mywindow","status=1,resize=1,scrollbars=1"); 
}

function castVote(comp, user, entry) {
	
	$(".vote").hide();
	
	data = "compID="+comp+"&entryID="+entry+"&userID="+user;
	$.post("/cast_vote.php", data, function(data, status) {
		if (data == 'success') {
			$(".vote-cast").show().pause(3000).fadeOut('slow');
		} else {
			$(".vote").show();
		}
	});
	
}

function showHelpPanel(id) {
	
	var obj = $("#"+id).position();
	$("#help-panel").css("left",obj.left-315).css("top",obj.top - 265);
    
    $.post("/common/ajax/help.php","widget="+id.substring(5), function(data, status) {
        $("#help-title").text(data.title);
        $("#help-instructions").html(data.help);
        $("#help-panel").show();
    },'json');
	
	/*switch (id) {
	case "help_tour":
		$("#help-title").text("Business Kit Tour");
		$("#help-instructions").html("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pulvinar ullamcorper sem. Cras auctor tortor vel nunc. Phasellus posuere tempus neque. ");
		$("#help-panel").show();
		break;
	case "help_news":
		$("#help-title").text("Latest News");
		$("#help-instructions").html("This is all the latest News and Events from Livewire. Have you got a story for us - why not send it in?");
		$("#help-panel").show();
		break;
	case "help_quote":
		$("#help-title").text("Inspirational Quotes");
		$("#help-instructions").html("What are other successful people saying about running a business? These are quotes from Livewire users and Awards Winners - why not send us an email with your own?");
		$("#help-panel").show();
		break;
	case "help_blog":
		$("#help-title").text("Featured Blogs");
		$("#help-instructions").html("These are a selection of blogs from the Social Network. Make sure you tell the world what you are up to by using the blogging tool.");
		$("#help-panel").show();
		break;
	case "help_rated":
		$("#help-title").text("Highly Rated Content");
		$("#help-instructions").html("This is the content that users have rated so gives a good idea of what you need to read first. You can rate the content at the bottom of each article in the Business Library using the star rating system. You can also leave comments as well!");
		$("#help-panel").show();
		break;
	case "help_cloud":
		$("#help-title").text("Tag Cloud");
		$("#help-instructions").html("These are some of the most searched on and used pages of the website - this is a good start if you are not sure what you are looking for!");
		$("#help-panel").show();
		break;
	case "help_find":
		$("#help-title").text("Can't Find");
		$("#help-instructions").html("If you can't find what you are looking for try using the Discussion Forum to ask your question - there are many people who may be able to help. If not there is also a seach tool and the Cloud Tag box.");
		$("#help-panel").show();
		break;
	case "help_feed":
		$("#help-title").text("News Feeds");
		$("#help-instructions").html("These feeds come from other websites and provide an information window to the world of business. You can choose which feeds you get - and click on the title to take you to the article.");
		$("#help-panel").show();
		break;
	case "help_mycontent":
		$("#help-title").text("My Content");
		$("#help-instructions").html("This box keeps track of your content including your posts on the Forum and any Blogs you post on the Social Network. You can also add any of your favourite pages from the Business Library.");
		$("#help-panel").show();
		break;
	case "help_suggested":
		$("#help-title").text("Suggested Content");
		$("#help-instructions").html("This content is suggested to you automatically based on where you live (for the members) and at what stage of business you register as. If you need to change the 'business stage', click on 'edit your profile' on the top right of the page.");
		$("#help-panel").show();
		break;
	case "help_inspire":
		$("#help-title").text("Inspiration");
		$("#help-instructions").html("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pulvinar ullamcorper sem. Cras auctor tortor vel nunc. Phasellus posuere tempus neque. ");
		$("#help-panel").show();
		break;
	case "help_login":
		$("#help-title").text("Login");
		$("#help-instructions").html("Login here to be able to access the premium content in the 'My Business Kit' area. Registering with LiveWIRE totally free and offers a huge amount of advice on starting a business.");
		$("#help-panel").show();
		break;
	case "help_poll":
		$("#help-title").text("Poll");
		$("#help-instructions").html("Each week LiveWIRE wants your opinion on enterprise and business issues. Please make sure you come back regularly to voice your opinion!");
		$("#help-panel").show();
		break;
	case "help_premium":
		$("#help-title").text("Premium Content");
		$("#help-instructions").html("The premium content section provides you with some fantastic information to help get you off the ground with your business. For example you can view the hugely popular PDF booklets on Business Planning, Market Research and keeping things legal.");
		$("#help-panel").show();
		break;
	case "help_editor":
		$("#help-title").text("Page Editor");
		$("#help-instructions").html("You can use the page editor tool to choose exactly what information you want to appear on the 'My Business Kit' page. Just tick the box next to the tool you want to have on your page. That way you can get the content relevant to your business. If you are not sure what a tool does, just click on the '?' in the box");
		$("#help-panel").show();
	case "help_section_links":
		$("#help-title").text("Business Library Links");
		$("#help-instructions").html("Links to valuable resources for starting your own business.");
		$("#help-panel").show();
		break;
	}*/
    
	
}

function hideHelpPanel() {
	$('#help-panel').hide();
}

function disableAddBtn() {
	$("#addBtn").hide();
}

function animPanel(panel) {
    
    $(".jqpanel").hide();
    $("#"+panel+" p.buttons a, #"+panel+" p.buttons a span").addClass("nobg");
    $("#"+panel+" .info").children().hide();
    $("#"+panel+" p.buttons").css("bottom","202px");
    
    $("#"+panel+" img").hide().css("left","-162px").show().animate({"left": "0"}, "slow", "linear", function() {
         $("#"+panel+" p.buttons a, #"+panel+" p.buttons a span").removeClass("nobg");
         if ($.browser.msie) {
            $("#"+panel+" .info h3, #"+panel+" .info ul").show();
            
            $("#"+panel+" p.buttons").show().animate({"bottom":"20px"},"slow","easeOutBounce");
         } else {
            $("#"+panel+" .info h3, #"+panel+" .info ul").fadeIn(1000,function() {
                $("#"+panel+" p.buttons").show().animate({"bottom":"20px"},"slow","easeOutBounce");
            });
         }
    });
    $("#"+panel+" .redbar").show();
    $("#"+panel).show();
    
}