var messages = new Array();
var ArrTextTextarea = new Array();
var ArrTextInput = new Array();

var color_grey_field = '#999999';
var color_field_input = '#999999';
var color_field_textarea = '#999999';


ArrTextInput['list_search'] = 'Name Search';

$(document).ready(function () {
   for(elm_id in ArrTextInput){
  		blur_text_input(elm_id);
		$("#"+ elm_id).click(function () { 
				click_text_input(this.id);
		});		
		
		$("#"+ elm_id).blur(function () { 
				blur_text_input(this.id);
		});

	}
	
   for(elm_id in ArrTextTextarea){
  		blur_text_textarea(elm_id);
		$("#"+ elm_id).click(function () { 
				click_text_textarea(this.id);
		});		
		
		$("#"+ elm_id).blur(function () { 
				blur_text_textarea(this.id);
		});

	}
});	

function before_submit(){
   for(elm_id in ArrTextInput){
   	click_text_input(elm_id);
   }
   
   for(elm_id in ArrTextTextarea){
   	click_text_textarea(elm_id);
   }
}

function after_error_validate(){
   for(elm_id in ArrTextInput){
   	blur_text_input(elm_id);
   }
   
   for(elm_id in ArrTextTextarea){
   	blur_text_textarea(elm_id);
   }
}

function click_text_input(el_id){
	if(el_id == undefined || ArrTextInput == undefined || ArrTextInput[el_id] == undefined){
		return;
	}
	
	if($('#' + el_id).val() == ArrTextInput[el_id]){
		$('#' + el_id).val('');
	}
	$('#' + el_id).css('color', color_field_input);
	return;
}		

function blur_text_input(el_id){
	if(el_id == undefined || ArrTextInput == undefined || ArrTextInput[el_id] == undefined){
		return;
	}	
	if($('#' + el_id).val() == ''){
		$('#' + el_id).val(ArrTextInput[el_id]);
		$('#' + el_id).css('color', color_grey_field);
		return;			
	}else{
		$('#' + el_id).css('color', color_field_input);
	}	
}	
function click_text_textarea(el_id){
	if(el_id == undefined || ArrTextTextarea == undefined || ArrTextTextarea[el_id] == undefined){
		return;
	}	
	if($('#' + el_id).val() == ArrTextTextarea[el_id]){
		$('#' + el_id).val('');
	}
	$('#' + el_id).css('color', color_field_textarea);
	return;
}		

function blur_text_textarea(el_id){
	if(el_id == undefined || ArrTextTextarea == undefined || ArrTextTextarea[el_id] == undefined){
		return;
	}		
	if($('#' + el_id).val() == ''){
		$('#' + el_id).val(ArrTextTextarea[el_id]);
		$('#' + el_id).css('color', color_grey_field);
		return;
	}	
}	
	function show_preloader() {
		ctrl1 = document.getElementById('preloader_container');
		ctrl1a = document.getElementById('preloader_content');
		ctrl2 = document.getElementById('preloader_background');

		if (ctrl1 && ctrl1a && ctrl2) {
			ctrl1a.style.left = (document.documentElement.clientWidth/2 - 100)+'px';
			ctrl1a.style.top = document.documentElement.clientHeight/2 - 75+'px';
			document.body.style.cursor = 'wait';
			ctrl1.style.display = 'block';
			ctrl1a.style.display = 'block';
			ctrl2.style.display = 'block';
		}
	};

	function hide_preloader() {
		ctrl1 = document.getElementById('preloader_container');
		ctrl2 = document.getElementById('preloader_background');
		if (ctrl1 && ctrl2) {
			document.body.style.cursor = 'auto';
			ctrl2.style.display = 'none';
			ctrl1.style.display = 'none';
			ctrl1a.style.display = 'none';
		};
	};
	loadingAnimation = new Image();
	loadingAnimation.src= base_url + "images/loadingAnimation.gif";
	
	
	/*MouseHoverText - MouseOutText*/
	var RecipientVOXid = 'Type in the VoxID or Email Address for the recipient of your anonymous VOX message here';
	var SenderEmail = 'Type in the Your Email Address here';
	var VOXTemplate = 'Using the drop down menu provided on the right, select a pre-written VOX Template to help you compose your VOX message';
	var ClickReadVox = 'Click here to read the VOX';	
	var ClickRenameLabel = 'Click here to give this VoxID a user friendly name';	
	var agreeTermsConditions = 'Click this question mark (?) to read the Terms & Conditions';	
	var Notify_comments = 'Click this box to allow replies to be sent to you from the recipient of your VOX message';	
	var Widget_Email = 'VOXopolis will never disclose your email address, your email is only used to route replies back to you through VOXopolis. Your email address is always secure and confidential';	
	var Widget_Read_More = '<b>Why do you need my email address?</b> <br><br>The VOXopolis service allows you to send an anonymous message, or VOX, while your anonymity remains private and secure. VOXopolis asks for your email address to make sure that this is indeed yours.  VOXopolis requires this confirmation before sending your VOX.  Supplying your email also allows the recipient of your VOX to respond back to you to seek additional information and/or to thank you.  VOXopolis will keep your email address private and secure at all times. <br><br><b>VOXtip:</b> For an added level of anonymity, use an email address that does not contain any indentifying information.';	
	
	var Widget_Read_More_status = 0;
	function MouseHoverText(evt, msg, min_width){
	  var BlockMouseHoverText = document.getElementById("BlockMouseHoverText");

	  if(BlockMouseHoverText){
	    $(BlockMouseHoverText).width("");


	    var posTop = evt.clientY -5 + $(window).scrollTop();
	    var posLeft = evt.clientX +15 + $(window).scrollLeft();
	    
	    $(BlockMouseHoverText).css('display', 'block');
	    BlockMouseHoverText.innerHTML = msg;
	    
	    if(min_width == undefined || min_width == '' || min_width <= 0){
		    if ($(BlockMouseHoverText).width()>300) {
		      $(BlockMouseHoverText).width(300);
		    }
	    } else {
//		    if ($(BlockMouseHoverText).width() < min_width) {
//		      	var padLeftRight = 10;
//				var documentWidth = $(document).width();
//				var cur_width =   documentWidth -  posLeft;
//     				
//		    	if( documentWidth < min_width){
//		    		min_width = documentWidth - padLeftRight;
//		    	}
//		    	if( cur_width < min_width){
//		    		posLeft = documentWidth - min_width - padLeftRight;
//		    		if(posLeft < padLeftRight){
//		    			posLeft = padLeftRight;
//		    			min_width = min_width - padLeftRight;
//		    		}
//	
//		    		$(BlockMouseHoverText).css('left', posLeft);
//		    	}
//		    	$(BlockMouseHoverText).width(min_width);
//		    }
	    }
	    
	    $(BlockMouseHoverText).css('top', posTop);
	    $(BlockMouseHoverText).css('left', posLeft);
	    	    
	  }

	}

	function MouseOutText(){
		var BlockMouseHoverText = document.getElementById("BlockMouseHoverText");
		if(BlockMouseHoverText){
			BlockMouseHoverText.style.display = "none";
		}
	}	
	
	/*END MouseHoverText - MouseOutText*/
	
	function show_preloader_bg(){
		var Width = $(document).width();
		var Height = $(document).height();

		$('#iframe_container').css('width',  Width);
		$('#iframe_container').css('height', Height);
		$('#preloader_background').css('height', Height);
		$('#iframe_container').css('display', 'block');		
		
		$('body').css('cursor', 'wait');
		$('#preloader_background').css('display', 'block');
	}
	
	function hide_preloader_bg(){
		$('body').css('cursor', 'auto');
		$('#iframe_container').css('display', 'none');
	}
	
	/*show_error_block - hide_error_block*/
	
	function show_info_block(el_id, text, id_position) {
		if(el_id == undefined || text == undefined){
			return false;
		}
		
		var Width = $(document).width();
		var Height = $(document).height();
		
		//ShowErrBlock
		$("div.info_content").html('');
		$("<p class='info_rec'>"+ text +"</p>").appendTo($("div.info_content"));

		show_preloader_bg();
		
		$('#info_block_container').css('display', 'block');
		$('#info_block_content').css('display', 'block');			
		
		
		if(id_position == undefined || id_position == '' ){
			var info_block_content_width = $('#info_block_content').width();
			var info_block_content_height = $('#info_block_content').height();	
				
			$('#info_block_content').css('left', (Width/2 - info_block_content_width/2) +'px');
			$('#info_block_content').css('top', ($(window).height()/2 - info_block_content_height/2) +'px');	
		} else {
			var pos = $('#' + id_position).offset();
			$('#info_block_content').css('left', pos.left +'px');
			$('#info_block_content').css('top',  pos.top +'px');	
		}
			
	};
	
	function show_spec_block(el_id, id_position) {
		if(el_id == undefined){
			return false;
		}
		
		var Width = $(document).width();
		var Height = $(document).height();
		
		//ShowErrBlock
		$("#" + el_id).appendTo($("#spec_block_content"));
		show_preloader_bg();
		
		$('#spec_block_container').css('display', 'block');
		$('#spec_block_content').css('display', 'block');			
		
		
		if(id_position == undefined || id_position == '' ){
			var spec_block_content_width = $('#spec_block_content').width();
			var spec_block_content_height = $('#spec_block_content').height();	
				
			$('#spec_block_content').css('left', (Width/2 - spec_block_content_width/2) +'px');
			$('#spec_block_content').css('top', ($(window).height()/2 - spec_block_content_height/2) +'px');	
		} else {
			var pos = $('#' + id_position).offset();
			$('#spec_block_content').css('left', pos.left +'px');
			$('#spec_block_content').css('top',  pos.top +'px');	
		}
			
	};
	
	
	function hide_spec_block() {
        show_spec_block('spec_block_container_cleaner');
		$('#spec_block_container').css('display', 'none');
		$('#spec_block_content').css('display', 'none');
		$('#preloader_background').css('display', 'none');
		
		hide_preloader_bg();
				
	};	
	
	function show_error_block(el_id, ElToErrors, id_position) {
		if(el_id == undefined || ElToErrors == undefined){
			return false;
		}
		
		var Width = $(document).width();
		var Height = $(document).height();
		
		//ShowErrBlock
		$("div.error_content").html('');
		$("<dl id='" +el_id+ "'></dl>").appendTo($("div.error_content"));
		
		for(elm_id in ElToErrors){
			if(ElToErrors[elm_id] != undefined && ElToErrors[elm_id] !=''){
				var el_dt = $("<dt></dt>")
				el_dt.appendTo($("#" + el_id));
				$("<div></div>").appendTo(el_dt);
                $("<p class='error_rec'></p>").appendTo(el_dt).text(ElToErrors[elm_id]);
			}
		}

		show_preloader_bg();
		
		$('#error_block_container').css('display', 'block');
		$('#error_block_content').css('display', 'block');			
		
		
		if(id_position == undefined || id_position == '' ){
			var error_block_content_width = $('#error_block_content').width();
			var error_block_content_height = $('#error_block_content').height();	
				
			$('#error_block_content').css('left', (Width/2 - error_block_content_width/2) +'px');
			$('#error_block_content').css('top', ($(window).height()/2 - error_block_content_height/2) +'px');	
		} else {
			var pos = $('#' + id_position).offset();
			$('#error_block_content').css('left', pos.left +'px');
			$('#error_block_content').css('top',  pos.top +'px');	
		}
			
	};
	
	function hide_error_block() {
		
		$('#error_block_container').css('display', 'none');
		$('#error_block_content').css('display', 'none');
		$('#preloader_background').css('display', 'none');
		
		hide_preloader_bg();
				
	};	
	
	function hide_info_block() {
		
		$('#info_block_container').css('display', 'none');
		$('#info_block_content').css('display', 'none');
		$('#preloader_background').css('display', 'none');
		
		hide_preloader_bg();
				
	};
	/*END show_error_block - hide_error_block*/
	
	/*change_template_feedback*/
	function change_template_feedback(id, nochange_color){
		if(id == undefined){
			id = '';
		}
		var num = $("#template_feedback" + id).val();
		var subject = '';
		var feedback = '';
		
		var color_text;
		if(num !='' && ArrTemplateFeedback[num]){
			subject = ArrTemplateFeedback[num]['subject'];
			feedback = ArrTemplateFeedback[num]['feedback'];
			color_text = '#999999';
		} else {
			color_text = '#999999';
		}
		
		if(nochange_color != true){
			$("#template_feedback" + id).css('color', color_text);
		}
		
		$("#subject" + id).val(subject);
		$("#subject" + id).click();
		$("#subject" + id).blur();
		
		$("#feedback" + id).val(feedback);
		$("#feedback" + id).click();
		$("#feedback" + id).blur();		
	}		
	/*END change_template_feedback*/
	
	/*change_template_feedback*/
	function change_template_feedback_subject(id, nochange_color){
		if(id == undefined){
			id = 'subject';
		}
		var num = $("#" + id).val();
		var feedback = '';
		
		var color_text;
		if(num !='' && ArrTemplateFeedback[num]){
			feedback = ArrTemplateFeedback[num]['feedback'];
			color_text = '#999999';
		} else {
			color_text = '#999999';
		}
		
		if(nochange_color != true){
			$("#" + id).css('color', color_text);
		}
		
		$("#feedback").val(feedback);
		$("#feedback").click();
		$("#feedback").blur();		
	}		
	/*END change_template_feedback*/
	
	
	function change_color_select(id){
		if(id == undefined){
			return;
		}
		var el_value = $("#" + id).val();
		var color_text;
		
		if(el_value !=undefined && el_value !=''){
			color_text = '#999999';
		} else {
			color_text = '#999999';
		}
		$("#" + id).css('color', color_text);
	}	
	
	
	
	function change_text_size(text_size, el_id){
		if(text_size == undefined){
			text_size = '12px';
		}	
		
		if(el_id == undefined){
			el_id = 'voxtips_content';
		}
		
		$("#" + el_id).css('font-size', text_size);
		$("#" + el_id +" p").css('font-size', text_size);
		$("#" + el_id +" span").css('font-size', text_size);
		return;
	}	
	

	function validation_voxtips(){
		before_submit();
		var ArrRequireds = new Array();
		var ElToErrors = new Array();
    	var error = 0;
    	var el_form;
    	
    	ArrRequireds['name_voxtips'] = {'text': 'Field "Name" is required'};
		ArrRequireds['email'] = {'text': 'Field "Email address" is required'};
		
		for(el_form in ArrRequireds){
			if(!$('#'+el_form).val()){
				ElToErrors[el_form] = ArrRequireds[el_form]['text'];
				//$("#error_" + el_form).html(ArrRequireds[el_form]['text']);
				error = 1;
			} else {
				//$("#error_" + el_form).html('');
			}
		}

		
		if(error == 0){
			return true;
		}
		after_error_validate();
		show_error_block('error_voxtips', ElToErrors);
		return false;
	}
	
	function getAssocArrayLength(tempArray) {
	   var result = 0;
	   for ( tempValue in tempArray ) {
	      result++;
	   }
		
	   return result;
	}
		
	
	
	/* colorpicker */
  
  function set_color_input(el){
  	var id = el.attr('id');
  	$('#' + id + '_mini_color').css('background-color', el.val());
  	$('#' + id + '_maket').css('background', el.val());
  	//$('#' + id + '_maket').css('background-color', el.val());
  	$('#' + id + '_font').css('color', el.val());  	
  	
  	$('.' + id + '_maket').css('background', el.val());
  	
/*  	$('.' + id + '_maket').css('background-color', el.val());*/
  	$('.' + id + '_font').css('color', el.val());
  	$('.' + id + '_border').css('border', "2px solid " + el.val());
  	return;
  }	
	
	/* widget */
	
	function Widget_MouseHoverText(evt, msg, no_hide_select){
		if($("#from_email").val() != ArrTextInput['from_email']){
			return false;
		}
		
	  var width_max = 470;
	  var BlockMouseHoverText = document.getElementById("BlockMouseHoverText");

	  if(BlockMouseHoverText){
	    $(BlockMouseHoverText).width("");
	    var posTop = evt.clientY +10;
	    var posLeft = 15;
	    
	    if(no_hide_select != true){
	    	$('#template_feedback').css('visibility', 'hidden');
	    }
	    
	    $(BlockMouseHoverText).css('padding', '5px');
	    $(BlockMouseHoverText).css('display', 'block');
	    
	    BlockMouseHoverText.innerHTML = msg;
	    
	    if ($(BlockMouseHoverText).width()>width_max) {
	      $(BlockMouseHoverText).width(width_max);
	    }

	    $(BlockMouseHoverText).css('top', posTop);
	    $(BlockMouseHoverText).css('left', posLeft);
	    	    
	  }

	}

	function Widget_MouseOutText(){
		$('#template_feedback').css('visibility', 'visible');
		MouseOutText();
	}	




