/*
+----------------------------------------------------------------+
|																							|
|	WordPress 2.7 Plugin: WP-Polls 2.40										|
|	Copyright (c) 2008 Lester "GaMerZ" Chan									|
|																							|
|	File Written By:																	|
|	- Lester "GaMerZ" Chan															|
|	- http://lesterchan.net															|
|																							|
|	File Information:																	|
|	- Polls Javascript File															|
|	- wp-content/plugins/wp-polls/polls-js.js									|
|																							|
+----------------------------------------------------------------+
*/

// Trim White Spaces
function trim(strText) { 
	// this will get rid of leading spaces 
	while (strText.substring(0,1) == ' ') 
		strText = strText.substring(1, strText.length);
	// this will get rid of trailing spaces 
	while (strText.substring(strText.length-1,strText.length) == ' ')
		strText = strText.substring(0, strText.length-1);
   return strText;
}

// Check Email
function is_valid_email(email) {
	var email = trim(email);
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

// Check Form Field Is Empty
function isEmpty(value){
	if (trim(value) == "") {
		return true;
	}
	return false;
}

// Variables
var polls = new sack(polls_ajax_url);
var poll_id = 0;
var poll_answer_id = "";
var poll_fadein_opacity = 0;
var poll_fadeout_opacity = 100;
var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);
var is_being_voted = false;
var has_errors = false;

jQuery(document).ready(

	function() {
		
		if(document.getElementById('poll_user_email')) {
			
			poll_user_email_elmt = document.getElementById('poll_user_email');
			poll_submit = document.getElementById('poll_submit');
			
			poll_extra_fields_elmt = document.getElementById('poll_extra_fields');
			
			poll_user_email_elmt.value = "Votre Email";
			
		}
		
		
	}


);


function backToEmailInput() {
		
	poll_extra_fields_elmt.innerHTML = "";
	poll_submit.style.height = poll_submit_height;
	poll_extra_fields_elmt.appendChild(poll_user_email_elmt);
	is_being_voted = false;
	
}

function createErrorMsgElement(error_msg) {
	
	var error_msg_element = document.createElement("div");
	error_msg_element.innerHTML = "<div class='message' style='float: left'><span>" + error_msg + "</span></div><input type='button' id='poll_error_ok_button' value=''/>"
	
	return error_msg_element;
	
}

function showErrorMsg(error_msg, show_bt) {
	
	poll_extra_fields_elmt.innerHTML = "";
	poll_submit_height = poll_submit.style.height; 
	poll_submit.style.height = "0px"
	poll_extra_fields_elmt.appendChild(createErrorMsgElement(error_msg));
	
	var poll_error_ok_button = document.getElementById('poll_error_ok_button');
	
	if(!show_bt) poll_error_ok_button.style.display = "none";
	
	 sIFR.replace(VAG, {
	    selector: '#poll_extra_fields div.message span',
		wmode: 'transparent',
		css: { 
			 
			'.sIFR-root' :  {'color': '#ffffff','text-transform':'uppercase', 'letter-spacing': '1'},
			'.accentfix' :  {'font-size': '1', 'leading': '-1'},
			'a' : {'color': '#FFFFFF', 'text-decoration' : 'none'},
			'a:hover' : {'color': '#EEEEEE'}
		
		
		},
		preventWrap: false,
		fitExactly: true,
		filters: {
	        DropShadow: {
	          knockout: false
	          ,distance: 0
	          ,color: '#330000'
	          ,strength: 2
	        }
	      },
	     tuneWidth : 10,
	     offsetLeft : 2
	  });
	
	poll_error_ok_button.onclick = backToEmailInput;
	
	has_errors = true;
	
}

// Function: When User Vote For Poll
function poll_vote(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		has_errors = false;
		poll_id = current_poll_id;
		poll_form = document.getElementById('polls_form_' + poll_id);
		poll_answer = eval("poll_form.poll_" + poll_id);
		poll_answer_id = "";
		
		//validates extra fields
		poll_user_email = poll_user_email_elmt.value;
		
		if(isEmpty(poll_user_email)) {
			
			showErrorMsg("Vous n'avez pas saisi votre adresse email.", true);
			
			return;
			
		}
		else if(!is_valid_email(poll_user_email)) {
			
			showErrorMsg("Votre adresse email est invalide.", true);
			return;
			
		}
		
		if(document.getElementById('poll_multiple_ans_' + poll_id)) {
			poll_multiple_ans = parseInt(document.getElementById('poll_multiple_ans_' + poll_id).value);
		} else {
			poll_multiple_ans = 0;
		}
		poll_multiple_ans_count = 0;
		if(poll_answer.length != null) {
			for(i = 0; i < poll_answer.length; i++) {
				if (poll_answer[i].checked) {					
					if(poll_multiple_ans > 0) {
						poll_answer_id = poll_answer[i].value + "," + poll_answer_id;
						poll_multiple_ans_count++;
					} else {
						poll_answer_id = parseInt(poll_answer[i].value);
					}
				}
			}
		} else {
			poll_answer_id = poll_answer.value;
		}
		if(poll_multiple_ans > 0) {
			if(poll_multiple_ans_count > 0 && poll_multiple_ans_count <= poll_multiple_ans) {
				poll_answer_id = poll_answer_id.substring(0, (poll_answer_id.length-1));
				poll_loading_text();
				poll_process();
			} else if(poll_multiple_ans_count == 0) {
				is_being_voted = false;
				showErrorMsg(polls_text_valid, true);
			} else {
				is_being_voted = false;
				showErrorMsg(polls_text_multiple + " " + poll_multiple_ans + ".", true);
				return;
			}
		} else {
			if(poll_answer_id > 0) {
				poll_loading_text();
				poll_process();
			} else {
				is_being_voted = false;
				showErrorMsg(polls_text_valid, true);
				return;
			}
		}
	} else {
		
		if(has_errors) {
			
			alert('Votre formulaire contient des erreurs.');
			return;
			
		}
		
		alert(polls_text_wait);
	}
}

// Function: When User View Poll's Result
function poll_result(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_result();
	} else {
		alert(polls_text_wait);
	}
}

// Function: When User View Poll's Voting Booth
function poll_booth(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_booth();
	} else {
		alert(polls_text_wait);
	}
}

// Function: Poll Fade In Text
function poll_fadein_text() {
	if(poll_fadein_opacity == 90) {
		poll_unloading_text();
	}
	if(poll_fadein_opacity < 100) {
		poll_fadein_opacity += 10;
		if(is_opera) {
			poll_fadein_opacity = 100;
			poll_unloading_text();
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.filter = 'alpha(opacity=' + poll_fadein_opacity + ')';
			} else {
				poll_fadein_opacity = 100;
				poll_unloading_text();
			}
		} else	 if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadein_opacity/100);
			} else {
				poll_fadein_opacity = 100;
				poll_unloading_text();
			}
		}
		setTimeout("poll_fadein_text()", 100); 
	} else {
		poll_fadein_opacity = 100;
		is_being_voted = false;
	}
}

// Function: Poll Loading Text
function poll_loading_text() {
	if(poll_show_loading) {
		document.getElementById('polls-' + poll_id + '-loading').style.display = 'block';
	}
}

// Function: Poll Finish Loading Text
function poll_unloading_text() {
	if(poll_show_loading) {
		document.getElementById('polls-' + poll_id + '-loading').style.display = 'none';
	}
}

// Function: Process The Poll
function poll_process() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.filter = 'alpha(opacity=' + poll_fadeout_opacity + ')';
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		setTimeout("poll_process()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("vote", true);
		polls.setVar("poll_id", poll_id);
		polls.setVar("poll_" + poll_id, poll_answer_id);
		
		polls.setVar("user_email", poll_user_email);
		
		//setting cookie to fill in email address next time
		//requires jQuery AND jQuery.cookie
		if(typeof(jQuery) != 'undefined' && typeof(jQuery.cookie) != 'undefined') {
			
			$.cookie('user_email', poll_user_email);
			
		}
		
		polls.method = 'POST';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}

// Function: Process Poll's Result
function poll_process_result() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.filter = 'alpha(opacity=' + poll_fadeout_opacity + ')';
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		setTimeout("poll_process_result()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("pollresult", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}

// Function: Process Poll's Voting Booth
function poll_process_booth() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.filter = 'alpha(opacity=' + poll_fadeout_opacity + ')';
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		setTimeout("poll_process_booth()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("pollbooth", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}

// Function: Disable Poll's Voting Booth
function poll_disable_voting(current_poll_id){
	poll_form = document.getElementById('polls_form_' + current_poll_id);
	for(i = 0; i < poll_form.length; i++){
		poll_form[i].disabled = true;
	}
}