
//return array day, month, yeear
function splitDate(myDate){
	var dates = myDate.split("-");
	
	return dates;
}

/*------------------------------
			FORM REGISTRATION
------------------------------*/
// CUSTOM FUNCTION : funcCall[provinces]
function provinces(){
	if($("#province").val() != "QC")	return true;
	else	return false;
}
// CUSTOM FUNCTION : funcCall[postalCodes]
function postalCodes(){
	if ($("#postalCode").val().length == 6 && $("#postalCode").val().search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return false;
	//else if ($("#postalCode").val().length == 7 && $("#postalCode").val().search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return false;
	else return true;
}
// CUSTOM FUNCTION : funcCall[phones]
function phones(){
	if ($("#phone").val() == "") return true;
	else{
		var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
		return !(re.test($("#phone").val()));
	}
}
// CUSTOM FUNCTION : funcCall[phones]
function mobile(){
	if ($("#mobile").val() == "" || $("#mobile").val() == "(___) ___-____") return false;
	else{
		var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
		return !(re.test($("#phone").val()));
	}
}
// CUSTOM FUNCTION : funcCall[birthdays_exists]
function birthdays_age(){		
	if ($("#birthday").val() == "" || $("#birthday").val() == "__-__-____") return true;
	else{
		var date = splitDate($("#birthday").val());
		d = date[0];
		m = date[1];
		y = date[2];
		if (m < 1 || m > 12) return true;
		else if (y < 1900 || y > 2100) return true;
		else if (d < 1 || d > 31) return true;

		var now = new Date();
	// Find the differences
		YearDiff = now.getFullYear() - y;
		MonthDiff = now.getMonth() - m;
		DayDiff = now.getDay() - d;
		// If the birthday has not occured this year
		if (DayDiff < 0 && MonthDiff < 0)
			YearDiff--;
		if (YearDiff < 18 || YearDiff > 36) return true;
	}

}
// CUSTOM FUNCTION : funcCall[min_chars]
function min_chars(){		
	if ($("#description").val().length < 50 ) return true;
}

function callSuccessFunction(){
	first = document.getElementById('firstName').value;
	last = document.getElementById('lastName').value;
	email = document.getElementById('email').value;
	address = document.getElementById('address').value;
	city = document.getElementById('city').value;
	province = document.getElementById('province').value;
	phone = document.getElementById('phone').value;
	mobile = document.getElementById('mobile').value;
	postal = document.getElementById('postalCode').value;
	birthday = document.getElementById('birthday').value;
	termsConditions = document.getElementById('termsConditions').value;
	youTube = document.getElementById('youTube').value;
	description = document.getElementById('description').value;
	
//	$("#ajaxRegistrationForm").load( BASE_URL + 'ajax.registration.php', {'first' : first, 'last' : last, 'email' : email, 'address' : address, 'city' : city, 'provinve' : provinve, 'phone' : phone, 'postal' : postal, 'birthday' : birthday, 'termsConditions' : termsConditions, 'youTube' : youTube, 'description' : description});

var dataString = 'first=' + first + '&last=' + last + '&email=' + email + '&address=' + address + '&city=' + city + '&province=' + province + '&phone=' + phone + '&mobile=' + mobile + '&postal=' + postal + '&birthday=' + birthday + '&termsConditions=' + termsConditions + '&youTube=' + youTube + '&description=' + description;

	$.ajax({  
		type: "GET",  
		url: BASE_URL + 'ajax.registration.php',  
		data: dataString,  
		success: function() {  
			$("#ajaxRegistrationForm").hide();
			$('#ajaxRegistrationSuccess').fadeIn(1500);  
		}  ,
		error: function() {  
			$("#ajaxRegistrationForm").hide();
			$('#ajaxRegistrationError').fadeIn(1500);  
		}  
	});  

}

function callSuccessFunction2(){
	var first = addslashes(document.getElementById('firstName').value);
	first = first.replace(/#/g, "");
	first = first.replace(/&/g, "et");
	var last = addslashes(document.getElementById('lastName').value);
	last = last.replace(/#/g, "");
	last = last.replace(/&/g, "et");
	var email = addslashes(document.getElementById('email').value);
	var address = addslashes(document.getElementById('address').value);
	address = address.replace(/#/g, "apt.");
	address = address.replace(/&/g, "et");
	var city = addslashes(document.getElementById('city').value);
	city = city.replace(/#/g, "");
	city = city.replace(/&/g, "et");
	var province = (document.getElementById('province').value);
	var phone = (document.getElementById('phone').value);
	var mobile = (document.getElementById('mobile').value);
	var postal = (document.getElementById('postalCode').value);
	var birthday = (document.getElementById('birthday').value);
	//var termsConditions = (document.getElementById('termsConditions').checked);
	var youTube = addslashes(document.getElementById('youTube').value);
	var description = ($("#description").val());
	//document.getElementById('description2').value;
	description = description.replace(/#/g, "no.");
	description = description.replace(/&/g, "et");


//	$("#ajaxRegistrationForm").load( BASE_URL + 'ajax.registration.php', {'first' : first, 'last' : last, 'email' : email, 'address' : address, 'city' : city, 'province' : province, 'phone' : phone, 'mobile' : mobile, 'postal' : postal, 'birthday' : birthday, 'termsConditions' : termsConditions, 'youTube' : youTube, 'description' : description});

var dataString = 'first=' + first + '&last=' + last + '&email=' + email + '&address=' + address + '&city=' + city + '&province=' + province + '&phone=' + phone + '&mobile=' + mobile + '&postal=' + postal + '&birthday=' + birthday + '&youTube=' + youTube + '&description=' + addslashes(description);

	$.ajax({  
		type: "GET",  
		url: BASE_URL + 'ajax.registration2.php',  
		data: dataString,  
		success: function() {  
			$("#ajaxRegistrationForm").hide();
			$('#ajaxRegistrationSuccess').fadeIn(1500);  
		}  ,
		error: function() {  
			$("#ajaxRegistrationForm").hide();
			$('#ajaxRegistrationError').fadeIn(1500);  
		}  
	});
}

function addslashes (str) {
    // Escapes single quote, double quotes and backslash characters in a string with backslashes  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/addslashes    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday' 
    return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

/*------------------------------
		END	FORM REGISTRATION
------------------------------*/

//Visionnement
function visionnement(page, order){
	$("#ajaxVisionnement").load( BASE_URL + 'ajax.visionnement.php', {'order' : order, 'page' : page});
}
//visionemment show the loading bar
function hideVideos(){
	document.getElementById('videoHolder').innerHTML = '<div style="text-align:center"><img src="images/loading.gif" style="padding-top:100px;padding-bottom:100px;" /></div>';
}
//video voting
function vote(url){
	$.ajax({
		type: "POST",
		url: BASE_URL + 'ajax.votes.php',
		data: "url="+url,
		success: function(data) {
			$("#go_vote").hide("slow", function () {
				$("#go_vote").html(data);
				$("#go_vote").fadeIn(1500);
																					 });
	//		$("#video_actions_"+key).html("changes saved!");
	//		hideElem("#video_"+key);
		}
	});

	//$("#go_vote").load( BASE_URL + 'ajax.votes.php', {'url' : url});
}

//video voting
function voteFinalistes(fid){
	$.ajax({
		type: "POST",
		url: BASE_URL + 'ajax.votesFinalistes.php',
		data: "fid="+fid,
		success: function(data) {
			$("#go_vote").hide("slow", function () {
				$("#go_vote").html(data);
				$("#go_vote").fadeIn(1500);
																					 });
	//		$("#video_actions_"+key).html("changes saved!");
	//		hideElem("#video_"+key);
		}
	});

	//$("#go_vote").load( BASE_URL + 'ajax.votes.php', {'url' : url});
}

function selectCopy(){
	document.getElementById('bitlyurl').select();
}

function fbShare(title){
	var url = window.location.href;
	var title = document.title;
	
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'sharer','toolbar=0,status=0,width=626,height=436');
}

