// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var draggable_dx;
var draggable_dy;

function init_groups(){	
	update_groups_markers();
	
	GEvent.addListener(map, 'zoomed', function() {
		update_groups_markers();
	});
	
	GEvent.addListener(map, 'moveend', function() {
		update_groups_markers();
	});
}

function update_groups_markers(){
	map.clearOverlays();
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var url = '/groups/update_markers?ne=' + northEast.toUrlValue() + '&sw=' + 
			southWest.toUrlValue() + '&zoom=' + map.getZoom();

	new Ajax.Request(url, {asynchronous:true, evalScripts:false});
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	} else {
		return [null, null];
	}
}

// determines the number of days in a month given the month and the year
function number_of_days_in_month(gyear, gmonth) { 
	days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
	if(gmonth == 2 && gyear % 4 == 0)
	    return 29;
	else
	    return days_in_month[gmonth - 1];
}

// populates the day select depending on the number of days in a specific month
function populate_birth_date_days(element, year, month) {
	days = number_of_days_in_month(year, month);
	if(element != null)
		element.innerHTML = "";
	for(var i = 1; i <= days; i++)
		element.insert("<option value=" + i + ">" + i + "</option>");
}

function email_is_valid(email) {
	return (!email.blank() && /^([^@\s]+)@((?:[-a-z0-9]+\.)+(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$/.test(email));
}

/* This function is used on the enterprise signup page. On the Add direct reports form, each direct
report is added by filling First name, Last name and Email and then clicking Add; if the last filled
details are not added by clicking submit, than we'll add them here */
function add_unsubmitted_direct_report(first_name, last_name, email) {
    if(!email.blank())
    {
	    $('direct_reports').insert("<input type='hidden' name='direct_reports_names[]' value='" + first_name + " " + last_name + "' />");
	    $('direct_reports').insert("<input type='hidden' name='direct_reports_emails[]' value='" + email + "' />");
    }
}

function show_preview(header, message, footer) {
    var hWndPreviewWindow = window.open('','Preview','width=600,height=350,scrollbars=yes,resizable=yes,status=0');
    hWndPreviewWindow.document.open();
	hWndPreviewWindow.document.writeln('<html><head><title>Email to Most Senior Executive</title></head>');
	hWndPreviewWindow.document.writeln('<body>');
	hWndPreviewWindow.document.writeln("<p>" + header + "</p>");
	hWndPreviewWindow.document.writeln("<p>" + message + "</p>");
	hWndPreviewWindow.document.writeln("<p>" + footer + "</p>");
	hWndPreviewWindow.document.writeln('<p><a href="javascript:window.close()">close preview</a></p>');
	hWndPreviewWindow.document.writeln('</body></html>');
	hWndPreviewWindow.document.close();
 }

function submit_service_inquiry(){
	var form = document.forms[0];
	if (form.first_name.value.length < 1 ||
	form.last_name.value.length < 1 ||
	form.company.value.length < 1 ||
	form.phone.value.length < 1 ||
	form.email.value.length < 1 ||
	!/(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}/.test(form.phone.value) ||
	!/^([^@\s]+)@((?:[-a-z0-9]+\.)+(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$/.test(form.email.value)) {
		alert("Please fill out all the required fields. Make sure email and phone number are valid.");
		return false;
	}
	form.submit();
}

// enable checker for video encoding status
function enable_periodic_encoding_check() {
		  new PeriodicalExecuter(function() {
			new Ajax.Request('/videos/ajax_check_encoding', {asynchronous:true, evalScripts:true})
		  }, 10)
};
