function activateSMS(){
	document.getElementById('sms_activation').style.display = 'block';
}

function enable_form(f,img){
	var elementos = f.elements.length;

	for (var i=0; i<elementos; i++) {
		if (f.elements[i].id == "sms_activation_btn") {
			continue;
		}
		f.elements[i].disabled = false;
		f.elements[i].style.backgroundColor = '#ffffff';
	}

	if (document.getElementById('date1') && document.getElementById('date2')) {
		document.getElementById('date1').style.display = 'block';
		document.getElementById('date2').style.display = 'block';
	}

	if (f.opt_sel[0].checked) {
		disable_fields_subscribe(document.subscribe,1);
	}
	else {
		disable_fields_subscribe(document.subscribe,0);
	}

	//document.getElementById('save_changes').innerHTML = '<img src="images/pages/subscribe/btn_save_changes.jpg" border="0" alt="Save Changes" title="Save Changes" onclick="send_subscription(document.subscribe);" style="cursor: pointer;">';
}

function disable_form(f,img){
	var elementos = f.elements.length;

	for (var i=0; i<elementos; i++){
		if (f.elements[i].id == "sms_activation_btn") {
			continue;
		}
		f.elements[i].disabled = true;
		f.elements[i].style.backgroundColor = '#eeeeee';
		f.elements[i].style.fontColor = '#000000';
	}

	// disable the date buttons
	if (document.getElementById('date1') && document.getElementById('date2')) {
		document.getElementById('date1').style.display = 'none';
		document.getElementById('date2').style.display = 'none';
	}
	//document.getElementById('save_changes').innerHTML = '<img src="images/pages/subscribe/btn_edit_subscription.jpg" border="0" alt="Edit Subscription Button" title="Edit Subscription Button" onclick="enable_form(document.subscribe,this);" style="cursor: pointer;">';
}

var checkZip = false;
var validZip = false;
var validZipChecked = true;
var validZipCheckedForm = null; // Form to send to function below when zip code is checked
var validZipCheckedFunction = null; // Function to execute when zip code is checked

function check_zip(){
	if(checkZip){
		if(!validZip){
			alert("Please enter a valid location for your holiday and shabbat times.");
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

function send_subscription(f){
	ret = false;
	ret = email_test(f.emailAddress,"Please enter a valid e-mail address") &&
		/*generic_test(f.fullName,"Please enter your name") &&*/
		(!f.password_flag.checked || generic_test(f.password,"Your password must be at least 4 characters long.",4)) &&
		checkCellPhone(f);

	// Check passwords
	if (ret && f.password_flag.checked && f.password.value != f.password2.value) {
		ret = false;
		f.password.value = '';
		f.password2.value = '';
		f.password.focus();
		alert('The two passwords you entered must match.');
	}
	if(!ret) {
		return false;
	}
	// Check if unsubscribe from all lists
	var arLists = document.getElementsByName("member_lists[]");
	var unsubscribeAll = true;
	for(var i=0;i<arLists.length;i++) {
		if(arLists[i].type=="checkbox" && arLists[i].checked) {
			unsubscribeAll = false;
			break;
		}
	}
	// Check if unsubscribe from all lists end
	// Check extra fields for update
	if(!unsubscribeAll && f.updateAccount && f.updateAccount.value) {
		ret = generic_test(f.address,"Please enter your address") &&
			generic_test(f.state,"Please enter your state/province") &&
			generic_test(f.fullName,"Please enter your name") &&
			generic_test(f.city,"Please enter your city") &&
			generic_test(f.zip,"Please enter your zip");
	}

	// If still OK and location box is showing (checkZip == true)...
	if (ret && checkZip) {

		// If zip code radio box is set, check zip code only
		if (f.opt_sel[0].checked) {

			// make sure the country/state/city pulldowns are disabled
			disable_fields_subscribe(document.subscribe,1);

			// Check the zip code

			if (!validZipChecked) { // Zip code check in progress
				validZipCheckedForm = f;
				validZipCheckedFunction = send_subscription;
				return true;
			}
			if (check_zip(f)) {
				f.submit();
				window.setTimeout('disable_form(document.subscribe, 0);', 1000);
				return true;
			}

			return false;
		}

		// country/state/city radio button must be checked

		// make sure the zip code fields are disabled
		disable_fields_subscribe(document.subscribe,0);

		// get lat and lon
		var ajax = newAjax();
		var url = "ajaxGeo.php";
		var country = '0';

		if (f.shabbatCountry) {
			country = f.shabbatCountry.options[f.shabbatCountry.selectedIndex].value;
		}

		var state = '0';
		if (f.shabbatState) {
			state = f.shabbatState.options[f.shabbatState.selectedIndex].value;
		}

		var city = '0';
		if (f.shabbatCity) {
			city = f.shabbatCity.options[f.shabbatCity.selectedIndex].value;
		}

		var zip = f.shabbatZip.value;
		if (ajax) {
			ajax.open("POST", url, true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.responseText == "-1" || ajax.responseText == ",") {
						alert("Please enter a valid location in the 'SET YOUR LOCATION AND TIME ZONE' section.");
						return false;
					}
					else {
						var geoVars = ajax.responseText.split(",");
						var lat = geoVars[0];
						var lon = geoVars[1];
						f.shabbatLat.value = lat;
						f.shabbatLong.value = lon;
						f.submit();
						window.setTimeout('disable_form(document.subscribe);', 1000);
					}
				} // end ajax state
			} //end ajax function
			ajax.send("country=" + country + "&state=" + state + "&city=" + city);
		} // end if ajax
	}
	else {
		if (ret) {
			f.submit();
			window.setTimeout('disable_form(document.subscribe);', 1000);
			return true;
		}
	}

	return ret;
}

function save_location_to_session(f){

	ret = true;

	// If zip code radio box is set, check zip code only
	if (f.opt_sel[0].checked) {

		// make sure the country/state/city pulldowns are disabled

		// Check the zip code

		if (!validZipChecked) { // Zip code check in progress
			validZipCheckedForm = f;
			validZipCheckedFunction = save_location_to_session;
			return true;
		}
		if (check_zip(f)) {
//			f.submit();
			AMcal.set_location($F('loca_shabbatZip'), null, null, null, null);
			return true;
		}

		return false;
	}

	// country/state/city radio button must be checked

	// get lat and lon
	var ajax = newAjax();
	var url = "ajaxGeo.php";
	var country = '0';

	if (f.shabbatCountry) {
		country = f.shabbatCountry.options[f.shabbatCountry.selectedIndex].value;
	}

	var state = '0';
	if (f.shabbatState) {
		state = f.shabbatState.options[f.shabbatState.selectedIndex].value;
	}

	var city = '0';
	if (f.shabbatCity) {
		city = f.shabbatCity.options[f.shabbatCity.selectedIndex].value;
	}

	var tz= '0';
	if (f.shabbatTZ) {
		tz = f.shabbatTZ.options[f.shabbatTZ.selectedIndex].value;
	}

	var zip = f.shabbatZip.value;
	if (ajax) {
		ajax.open("POST", url, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.responseText == "-1" || ajax.responseText == ",") {
					alert("Please enter a valid location in the 'SET YOUR LOCATION AND TIME ZONE section.");
					return false;
				}
				else {
					var geoVars = ajax.responseText.split(",");
					var lat = geoVars[0];
					var lon = geoVars[1];
//					f.shabbatLat.value = lat;
//					f.shabbatLong.value = lon;
					//AMcal.set_location(null, $F('loca_shabbatCountry'), $F('loca_shabbatState'), $F('loca_shabbatCity'), $F('loca_shabbatTZ'));
					AMcal.set_location(null, country, state, city, tz);
					AMcal.hide_location();
				}
			} // end ajax state
		} //end ajax function
		ajax.send("country=" + country + "&state=" + state + "&city=" + city);
	} // end if ajax

	return ret;
}

function checkCellPhone(f){

	var ret = false;
	var checkBoxes = f.elements['member_lists[]'];
	var countCheckBoxes = checkBoxes.length;
	var smsFlag = false;

	for (var i=0; i < countCheckBoxes; i++) {
		if (checkBoxes[i].getAttribute('flag') == "sms" && checkBoxes[i].checked == true) {
			smsFlag = true;
			break;
		}
	}

	if (smsFlag == true) {
		ret = checkCellPhoneStruct(f.smsPhone) &&
			check_select(f.smsCarrier, "Please choose your cellphone carrier");
	}
	else {
		ret = true;
	}

	return ret;
}

function check_select(select,msg){
	if (select.options[select.selectedIndex].value == 0) {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function checkCellPhoneStruct(objPhone) {
	if (objPhone.value.length == 0 || objPhone.value == "") {
		alert("Please enter the mobile phone number");
		objPhone.focus();
		return false;
	}

	filter = /\(\d{3}\)\d{3}-\d{4}/;

	if (!filter.test(objPhone.value)) {
		alert("Please enter you mobile phone following the structure bellow:\n (000)000-0000");
		objPhone.focus();
		return false;
	}

	return true;
}

function show_locationform(notFixFooter){
	var mailChk = document.getElementById('MailReminder')?document.getElementById('MailReminder').checked:false;
	var smsChk = document.getElementById('SMSReminder')?document.getElementById('SMSReminder').checked:false;
	var div = document.getElementById('locationData');

	if(mailChk || smsChk){
/*
		if(IE || IE6 || IE7) {
			var top=555;
			var left = 455;
		} else {
			var top=525;
			var left = 425;
		}
		div.style.top = top+'px';
		div.style.left = left+'px';
*/
		div.style.display='block';
		checkZip = true;
	}
	else if(!mailChk || !smsChk){
		div.style.display='none';
		checkZip = false;
	}
	if(notFixFooter) {
		return true;
	}
	fixfooter();
}

function show_location_dropdown(divName, type, countryID, stateID, cityID, selectDisabled){

	// We define the div where errors should be displayed
	var errorDiv = document.getElementById('errorDisplay');
	var div = document.getElementById(divName);

	if (typeof countryID == 'undefined') var countryID = 0;
	if (typeof stateID == 'undefined') var stateID = 0;
	if (typeof cityID == 'undefined') var cityID = 0;

	document.getElementById('timezone').style.display = "none";
	document.getElementById('shabbatDST').style.display = "none";
	div.innerHTML = "<div align=\"center\" style=\"text-decoration: blink;\">Loading...</div>";

	var url = "/ajax_geolocation.html?t=" + type + "&country=" + countryID + "&state=" + stateID + "&city=" + cityID;
	var ajax = newAjax();

	if (ajax) {
		ajax.open("GET", url, true);
		ajax.onreadystatechange = function() {
			document.getElementById('city_combo').style.display = "block";

			if (ajax.readyState < 4) {
				div.innerHTML = "<div align=\"center\" style=\"text-decoration: blink;\">Loading...</div>";
			} //end if

			if (ajax.readyState == 4) {
				if (ajax.responseText == "DB ERROR") {
					errorDiv.innerHTML = "An error occurred while loading the countries. Please try again later.";
				}
				else if (ajax.responseText == "NO RECORDS") {
					if (type == 3) {
						document.getElementById('state_combo').innerHTML = "<select id=\"loca_shabbatState\" name=\"shabbatState\"><option value=\"0\" selected=\"selected\">Choose your state</option></select>";
					}

					if (type == 4) {
						document.getElementById('city_combo').innerHTML = "<select id=\"loca_shabbatCity\" name=\"shabbatCity\"><option value=\"0\" selected=\"selected\">Choose your city</option></select>";
					}
				}
				else {
					div.innerHTML = ajax.responseText;

					// Reset the city if you are changing the country
					if (type == "3") {
						document.getElementById('city_combo').innerHTML = "<select id=\"loca_shabbatCity\" name=\"shabbatCity\"><option value=\"0\" selected=\"selected\">Choose your city</option></select>";
					}

					if (!(typeof selectDisabled == 'undefined')) {
						if (document.getElementsByName('shabbatCountry')[0]) {
							document.getElementsByName('shabbatCountry')[0].disabled = true;
						}

						if (document.getElementsByName('shabbatState')[0]) {
							document.getElementsByName('shabbatState')[0].disabled = true;
						}

						if (document.getElementsByName('shabbatCity')[0]) {
							document.getElementsByName('shabbatCity')[0].disabled = true;
						}
					}
				}
			}
		}

		ajax.send('');
	}
	else {
		alert('Error');
	}
}

function changeLocationButton2(show, div, radioClicked, sLocation){
	document.getElementById(show).style.display = "block";
	document.getElementById(div).innerHTML = '';
	/*
	document.getElementById(div).innerHTML = "\
						<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\
							<tr>\
								<td width=\"8\"></TD>\
								<td align=\"left\" valign=\"button\" id=\"tdlocation\">\
								Location: "+ sLocation +"\
								</td>\
							</tr>\
						</table>";
	*/
	if (typeof radioClicked != "undefined") {
		if (radioClicked == "zip") {
			document.getElementById('locateByZip').click();
		}
		else {
			document.getElementById('locateByLoc').click();
		}
	}
	else {
		document.getElementById('locateByZip').click();
	}

//	disable_fields(1);
}

function disable_fields_subscribe(f, which) {
	if (which == 1) {
		if (typeof f.shabbatCountry != "undefined") {
			f.shabbatCountry.disabled = true;
		}

		if (typeof f.shabbatState != "undefined") {
			f.shabbatState.disabled = true;
		}

		if (typeof f.shabbatCity != "undefined") {
			f.shabbatCity.disabled = true;
		}

		f.shabbatZip.disabled = false;

		if (typeof f.shabbatTZ != "undefined") {
			f.shabbatTZ.disabled = true;
		}

		if (typeof f.shabbatDST != "undefined") {
			f.shabbatDST.disabled = true;
		}
	}
	else {
		f.shabbatZip.disabled = true;
		if (typeof f.shabbatCountry != "undefined") {
			f.shabbatCountry.disabled = false;
		}

		if (typeof f.shabbatState != "undefined") {
			f.shabbatState.disabled = false;
		}

		if (typeof f.shabbatCity != "undefined") {
			f.shabbatCity.disabled = false;
		}

		if (typeof f.shabbatTZ != "undefined") {
			f.shabbatTZ.disabled = false;
		}

		if (typeof f.shabbatDST != "undefined") {
			f.shabbatDST.disabled = false;
		}
	}
}

var zipTimeoutID = false;
var zipField = null;
//var zipArray = new Array();

function validateZip(fld) {

	var errorDiv = document.getElementById('errorDisplay');
	errorDiv.innerHTML = "&nbsp;";
	fld.className = "";

	// Set global validZip to null since we haven't validated
	validZip = false;

	if (zipTimeoutID) {
		window.clearTimeout(zipTimeoutID);
		zipTimeout = false;
	}

	if (fld.value.length == 5) {
		zipField = fld;
		validZipChecked = false;
		zipTimeoutID = window.setTimeout('validateZip2(zipField);', 250);
	}
	else {
		validZipChecked = true;
	}
}

function validateZip2(fld) {
	// We define the div where errors should be displayed
	var errorDiv = document.getElementById('errorDisplay');

	validZip = false;
	validZipChecked = false;

	if (fld.value.length != 0 && fld.value != "") {
		if (fld.value.length > 2) {
			var url = "/ajax_geolocation.html?t=1&zip=" + encode(fld.value) + '&' + (new Date()).getTime();
			var ajax = newAjax();

			if (ajax) {
				ajax.open("GET", url, true);
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 4) {
						var errorDiv = document.getElementById('errorDisplay');

						validZipChecked = true;
						switch(ajax.responseText){
							case "-1":
							errorDiv.innerHTML = "Not Found";
							validZip = false;
							errorDiv.style.color = "#FF0000";
							break;

							case "1":
							errorDiv.innerHTML = "Valid!";
							validZip = true;
							errorDiv.style.color = "#009900";
							break;

							case "DB ERROR":
							validZip = false;
							errorDiv.innerHTML = "&nbsp;";
							break;

							default:
							validZip = false;
							errorDiv.innerHTML = objReq.responseText;
							break;
						}

						if (validZipCheckedFunction != null) {
							validZipCheckedFunction(validZipCheckedForm);
							validZipCheckedFunction = null;
							validZipCheckedForm = null;
						}
					}
				}

				ajax.send('');
			}
		}
	}
	else {
		errorDiv.innerHTML = "&nbsp;";
	}
}

function sendActivateSMS(tmp, f) {
	ret = false;
	ret = generic_test(tmp.value, "SMS activation code can't be empty.");
	if (!ret) {
		return false;
	}
	else {
		f.code.value = tmp.value;
		tmp.value = '';
		f.submit();
	}
	return true;
}

function subscribeValid(f, t, id, mode){
	var val;

	if (mode == 1) {
		var ret = email_test(f.emailAddress, "Please enter a valid e-mail address");
		val = f.emailAddress.value;
	}
	else if ( mode == 2 ) {
		var ret = checkCellPhoneStruct(f.smsPhone);
		val = f.smsPhone.value;
	}

	if (ret) {
		var ajax = newAjax();
		var url = "ajax_subscribe.html?t=" + t + "&v=" + val + "&id=" + id + "&mode=" + mode;
		if (ajax) {
			ajax.open("GET", url, true);
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {
					if (ajax.responseText == "EMAIL EXIST") {
						alert(f.emailAddress.value + ' already exists, please enter a different address\nor log in or retrieve your password below.');
						f.emailAddress.focus();
						f.emailAddress.select();
						return false;
					}
					if (ajax.responseText=="CELLPHONE EXIST") {
						alert('That cellphone number is already in use on this system. \nIf you created another account on this system please use the Forgot Password function \nto retrieve the password.');
						f.smsPhone.value = "";
						f.smsPhone.focus();
						f.smsPhone.select();
						return false;
					}
				}
			}
			ajax.send("");
		}
		else {
			alert("Error");
		}

		return true;
	}

	return false;
}

var n;
var p;
var p1;
function ValidatePhone(){
	p = p1.value;

	if (p.length == 3) {
		pp = p;
		d4 = p.indexOf('(');
		d5 = p.indexOf(')');

		if (d4 == -1) {
			pp = "(" + pp;
		}

		if (d5 == -1) {
			pp = pp + ")";
		}

		document.subscribe.smsPhone.value = "";
		document.subscribe.smsPhone.value = pp;
	}

	if (p.length > 3) {
		d1 = p.indexOf('(');
		d2 = p.indexOf(')');
		if (d2 == -1) {
			l30 = p.length;
			p30 = p.substring(0, 4);
			p30 = p30 + ")";
			p31 = p.substring(4, l30);
			pp = p30 + p31;
			document.subscribe.smsPhone.value = "";
			document.subscribe.smsPhone.value = pp;
		}
	}
	if (p.length > 5) {
		p11 = p.substring(d1 + 1, d2);
		if (p11.length > 3) {
			p12 = p11;
			l12 = p12.length;
			l15 = p.length;
			p13 = p11.substring(0, 3);
			p14 = p11.substring(3, l12);
			p15 = p.substring(d2 + 1, l15);
			document.subscribe.smsPhone.value = "";
			pp = "(" + p13 + ")" + p14 + p15;
			document.subscribe.smsPhone.value = pp;
		}

		l16 = p.length;
		p16 = p.substring(d2 + 1, l16);
		l17 = p16.length;
		if (l17 > 3 && p16.indexOf('-') == -1) {
			p17 = p.substring(d2 + 1, d2 + 4);
			p18 = p.substring(d2 + 4, l16);
			p19 = p.substring(0, d2 + 1);
			pp = p19 + p17 + "-" + p18;
			document.subscribe.smsPhone.value = "";
			document.subscribe.smsPhone.value = pp;
		}
	}

	setTimeout(ValidatePhone, 100);
}

function getIt(m){
	n = m.name;
	p1 = m;
	ValidatePhone();
}

function toggle_password_display(e) {
	var pass1row = document.getElementById('password1row');
	var pass2row = document.getElementById('password2row');
	var passflagrow = document.getElementById('password_flag_row');

	if (e.checked) {
		if (MOZ || OPERA) {
			pass1row.style.visibility = 'visible';
			pass2row.style.visibility = 'visible';
			pass1row.style.display = '';
			pass2row.style.display = '';
		} else {
			pass1row.style.display = '';
			pass2row.style.display = '';
		}
	} else {
		if (MOZ || OPERA) {
			pass1row.style.visibility = 'collapse';
			pass2row.style.visibility = 'collapse';
		} else {
			pass1row.style.display = 'none';
			pass2row.style.display = 'none';
		}
		e.form.password.value = '';
		e.form.password2.value = '';
	}
}

