/*
userreg.js -
jquery scripts for the userspace
*/

//setup some constants
var docUrl = document.URL.substring(0,(document.URL.lastIndexOf('/')));//get the site URL for accessing files.
var lineCount = 0;
var maxLineCount = 10;

function doAdmUserList(i) {

ajaxurl = docUrl+"/../wp-content/plugins/UserReg/adm-ajax.php";

jQuery.ajax({
type:"POST",
url: ajaxurl,
data: "id="+i+"&fn=admlist",
dataType:"html",
success:function(html){
jQuery('#adm_user_list').html(html);
}
});//end ajax call
}//end function


function doGetDetails(i) {
if (jQuery('.cocontent').length > 0) {jQuery('.cocontent').slideUp(300,function(){jQuery(this).remove();});}
ajaxurl = docUrl+"/../wp-content/plugins/UserReg/adm-ajax.php";
jQuery.ajax({
type:"POST",
url:ajaxurl,
data: "id="+i+"&fn=fetchinfo",
dataType:"html",
success:function(html) {
jQuery('#'+i).after('<li class=\"cocontent\"></li>');
jQuery('.cocontent').hide();
jQuery('.cocontent').html(html).slideDown(300);
}//end success call
});//end ajax call
}//end function



jQuery(window).load(function() {//stuff to do once the actual page has loaded.
if (jQuery('.ur_fset').length > 0) {
var str = getQuerystring('vis');
if (str.length > 0) {
var sArr = new RegExp(str+"$");

$('.ur_fset').each(function() {if ($(this).attr('id').match(sArr)) {$(this).show();}else{$(this).hide();}});//end fset iteration

}//end if
}
});

jQuery(document).ready(function($) {
$('.help').hide();
$('dt').css('float','left');
$('input,select,textarea').hover(function() {
var pos = $(this).position();
posleft = pos.left + $(this).width()+22+"px";
postop = pos.top+"px";
$('.help').fadeOut();
$(this).parent()
		 .next('dd.help')
		 .prepend('<span class=\"helparrow\"></span>')	
		 .css('position','absolute')
		 .css('left',posleft)
		 .css('top',postop)
		 .css('width','280px')
		 .css('font-size','1em')
		 .css('text-align','left')
		 .css('padding','3px')
		 .css('z-index','100')
		 .fadeIn();
$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); 
$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase()); 
if ($.browser.msie || $.browser.opera) {
//if the user's running msie then it won't to the background shadow so we'll have to add it manually
$(this).parent().next('dd.help').append('<div id=\"shadowheader\"></div><div id=\"shadowbody\"></div><div id=\"shadowfooter\"></div>');
$('#shadowbody').css('height',$(this).parent().next('dd.help').height()+"px").css('z-index','-1');
$('#shadowfooter').css('top',$(this).parent().next('dd.help').height()+"px").css('z-index','-1');
}
		 },		 
		 function() {$('#shadowheader, #shadowbody, #shadowfooter').remove();$(this).parent().next('dd.help').fadeOut(300,function() {$(this).removeShadow();});
});//end focus


//deal with the fieldsets
$('.ur_fset').each(function() {if ($(this).attr('id').match(/1$/)) {$(this).show();}else{$(this).hide();}});//end fset iteration
//quickly set the hight of the contaning div so it doesn't piss around on screen
//'continue' button - this will hide the current content and pull up the next fieldset. Saves on having to reload the whole page.
//TODO - add data validation check to this function so it won't proceed unless the required information is there.
$('#urform .continue, #urform input[type = submit]').live('click',function(e) {
e.preventDefault();
var isGood=true;
var errArr = new Array();
if ($('.errormsg').length > 0) {$('.errormsg').remove();}
//check the "required" fields in each of the displayed fieldsets are filled in and create a DOM element containing a notice for each one if it's not.
$(this).parent('fieldset').find('.required').each(function(){
//do textarea/text input
if (this.type =='text' || this.type =='textarea') {
var msg="This field is required";
if (!$(this).val().length) {
var w = $(this).width()+"px";
var pos = $(this).position();
var posleft = (pos.left+$(this).width())+"px";
var postop = pos.top+"px";
var cVar = $('.errormsg').length;
$(this).parent().append('<p class="errormsg" id="msg'+cVar+'"></p>');
var el = "#msg"+cVar;
$(el).css('position','absolute').css('left',posleft).css('top',postop).css('color','#ffffff').css('font-weight','bold').css('font-size','0.8em').css('text-align','center').html(msg).end();
isGood = false;
}} else if (this.type=="select-one") {var msg = "Please select an option";if (!$(this).val().length) {$(this).prev('dt').css('padding-top','15px');
	var w = $(this).width()+"px";
var pos = $(this).position();
var posleft = (pos.left+$(this).width())+"px";
var postop = pos.top+"px";
	var cVar = $('.errormsg').length;
	$(this).parent().prepend('<p class="errormsg" id="msg'+cVar+'"></p>');	
	var el = "#msg"+cVar;
	$(el).css('position','absolute').css('left',posleft).css('top',postop).css('color','#ffffff').css('font-weight','bold').css('font-size','0.8em').html(msg);
	isGood = false;
}} else if (this.type == "checkbox") {
var cid = $(this).attr('name');
var s = $('input[name='+cid+']:checked').size();
if (s == 0) {
//if the length of the checkbox array is zero then we need to prompt the user to add something.
	var w = $(this).width()+"px";
var pos = $(this).position();
var posleft = (pos.left+$(this).width())+"px";
var postop = pos.top+"px";
	var cVar = $('.errormsg').length;
	$(this).parent().prepend('<p class="errormsg" id="msg'+cVar+'"></p>');
	var el = "#msg"+cVar;
	$(el).css('position','absolute').css('left',posleft).css('top',postop).css('color','#ffffff').css('font-weight','bold').html("Please choose at least 1");
	isGood = false;
}} else if (this.type == "select-multiple") {
var selCount = $('option:selected',this).size();
if (selCount == 0) {
	var w = $(this).width()+"px";
var pos = $(this).position();
var posleft = (pos.left+$(this).width())+"px";
var postop = pos.top+"px";
	var cVar = $('.errormsg').length;
	$(this).parent().prepend('<p class="errormsg" id="msg'+cVar+'"></p>');	
	var el = "#msg"+cVar;
	$(el).css('position','absolute').css('left',posleft).css('top',postop).css('color','#ffffff').css('font-weight','bold').css('font-size','0.8em').html("Please select at least one");
	isGood = false;
}}

});//end iteration

//if everything is cool move on to the next section - "on the fly" form checking
if (isGood) {
if (this.type == "button") {
if ($('.errormsg').length > 0) {$('.errormsg').remove();}
$(this).parent('fieldset').slideUp('fast').next('fieldset').slideDown('fast',function() {self.scrollTo(0,0)});
} else if (this.type=="submit") {
$(this).parents('form').submit();
}
}//end isgood

});//end live set

$('.back').click(function() {
$(this).parent('fieldset').slideUp('fast').prev('fieldset').slideDown('fast',function() {self.scrollTo(0,0)});
});


$('.save').click(function() {
var pDiv = $(this).parent('fieldset').attr('id');
var vis = pDiv.match(/\d{1}$/);
$('#fct').val('save');
$(this).parents('form').append('<input type="hidden" name="vis" value="'+vis+'" />');
$(this).parents('form').submit();
});

$('.preview').click(function() {
var pDiv = $(this).parent('fieldset').attr('id');
$('#fct').val('preview');
$(this).parents('form').submit();
});

$('.count').focus(function() {//setup the count flash
//clear any current instances
if ($('#counter').length > 0) {$('#counter').remove();}
//create a counter box and position it above the top left of the box
var $charCount = $(this).attr('maxlength') - $(this).val().length;
var pos = $(this).position();
posleft = pos.left-40+"px";
postop = pos.top+"px";
$(this).parent().append('<p id=\"counter\">'+$charCount+'</p>');
$('#counter').hide().css('position','absolute')
				 .css('left',posleft)
				 .css('top',postop)
				 .css('background-color','#99cc66')
				 .css('line-height','15px')
				 .css('width','40px')
				 .css('text-align','center')
				 .css('font-size','0.8em')
				 .fadeIn();
});//end focus function

$('.count,#product,.tenlist').blur(function() {$('#counter').fadeOut();});//end blur

$('.count').keyup(function(ev) {
if ($(this).val().length > $(this).attr('maxlength')) {
//stop the user from entering any more letters
$(this).val($(this).val().substr(0,$(this).attr('maxlength')));
return false;
}
$('#counter').html(($(this).attr('maxlength') - $(this).val().length));});//end keyup function

//do the same for the multiselect box
$('#product').focus(function() {
if ($('#counter').length > 0) {$('#counter').remove();}
//get the number of selected items to display
var selCount = $('option:selected',this).size();
//create a counter box and position it above the top left of the box
var pos = $(this).position();
posleft = pos.left-40+"px";
postop = pos.top+"px";
$(this).parent().append('<p id=\"counter\">'+selCount+'</p>');
$('#counter').hide().css('position','absolute').css('margin','0px').css('left',posleft).css('top',postop).css('background-color','#99cc66').css('line-height','15px').css('width','40px').css('text-align','center').fadeIn();
});//end product select

$('#product > option').click(function() {
var selCount = $('#product > option:selected').size();
if (selCount > 5) {
//if there are already 5 prevent any more frome being checked by resetting the list and activiating the currently selected ones
$(this).attr('selected',false);
selCount--;
}
$('#counter').html(selCount) 
});//end product click

$('.tenlist').focus(function() {
var ct = $(this).val().match(/(\n|\r)/g);
if (ct) {
lineCount = ct.length;
} else {
lineCount = 0;
}
if ($('#counter').length > 0) {$('#counter').remove();}
var pos = $(this).position();
posleft = pos.left-40+"px";
postop = pos.top+"px";
$(this).parent().append('<p id=\"counter\">'+lineCount+'</p>');
$('#counter').hide().css('position','absolute').css('margin','0px').css('left',posleft).css('top',postop).css('background-color','#99cc66').css('line-height','15px').css('width','40px').css('text-align','center').fadeIn();
});

$('.tenlist').keyup(function(ev) {
var ct = $(this).val().match(/(\n|\r)/g);
if (ct.length > 0) {
$('#counter').html(ct.length);
	if (ct.length > 10) {
	$(this).val($(this).val().substr(0,$(this).val().length-1));//keep removing the last character that anyone adds...
	$('#counter').html('10');//auto jump out at 10
	}
}
});

$('.threelist').focus(function() {
var ct = $(this).val().match(/(\n|\r)/g);
if (ct) {
lineCount = ct.length;
} else {
lineCount = 0;
}
if ($('#counter').length > 0) {$('#counter').remove();}
var pos = $(this).position();
posleft = pos.left+"px";
postop = (pos.top - 23)+"px";
$(this).parent().append('<p id=\"counter\">'+lineCount+'</p>');
$('#counter').hide().css('position','absolute').css('margin','0px').css('left',posleft).css('top',postop).css('background-color','#99cc66').css('line-height','15px').css('width','40px') .css('text-align','center').fadeIn();

});//end threelist

$('.threelist').keyup(function(ev) {
var ct = $(this).val().match(/(\n|\r)/g);
if (ct.length > 0) {
$('#counter').html(ct.length);
	if (ct.length > 3) {
	$(this).val($(this).val().substr(0,$(this).val().length-1));//keep removing the last character that anyone adds...
	$('#counter').html('3');//auto jump out at 10
	}
}
});

//admin section 

if ($('#adm_user_list').length > 0) {

//setup the ajax stuff for retrieving the list data
$('.view').click(function() {var i = $(this).attr('id');doAdmUserList(i);});//end "view" click

$('.adm_user_ul > li strong').live('click',function() {
var t = $(this).parent().attr('id');
doGetDetails(t);

});//end "strong" click


}//end "admin" if



});//end document ready
