function validateEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

		 return true					
	}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s) {   
	var i;
    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    	}
    // All characters are numbers.
    return true;
	}

function trim(s) {   
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
	}

function stripCharsInBag(s, bag) {   
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    	}
    return returnString;
	}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}

function popPrintRes() {
	URL = "/calendar/includes/elementcreative/print_reservations.php?id="+cal_id+"&date="+cal_date+"&time="+cal_time;
	window.open(URL, 'printres','toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=500,height=500');
	}

var cal_id;
var cal_name;
var cal_date;
var cal_time;
var cal_location;
function initRes() {
	// Snag these for use all over the place
	cal_id = $F('cal_id'); 
	cal_name = $F('cal_name');
	cal_date = $F('cal_date');
	cal_time = $F('cal_time');
	cal_location = $F('cal_location');
	}

function showResList() {
	getResList();
	new Effect.Appear('res-list-wrap', {from: 0.0, to: 1.0, duration: 1.0});
	}

function getResList() {
	new Ajax.Request('includes/elementcreative/process_reservations.php', {
			parameters: {type:'getList',id:cal_id,date:cal_date},
			onSuccess: function(transport){
     			results = transport.responseText;
				$('res-list').replace('<div id="res-list">' + results + '</div>');	
				}
			});
	}

function hideResList() {
	new Effect.Fade('res-list-wrap', {from: 1.0, to: 0.0, duration: 1.0});
	$('res-list-confirm').replace('<div id="res-list-confirm"></div>');
	}

function showCancelForm() {
	new Effect.Appear('cancel-form-wrap', {from: 0.0, to: 1.0, duration: 1.0});
	$('cancel-form-confirm').replace('<div id="cancel-form-confirm"><p>All fields are required.</p></div>');
	$('cancel-form').show();
	}
	
function hideCancelForm() {
	new Effect.Fade('cancel-form-wrap', {from: 1.0, to: 0.0, duration: 1.0});
	}	

function showResForm() {
	new Effect.Appear('res-form-wrap', {from: 0.0, to: 1.0, duration: 1.0});
	$('res-form-confirm').replace('<div id="res-form-confirm"><p>All fields are required.</p></div>');
	$('res-form').show();
	}
	
function hideResForm() {
	new Effect.Fade('res-form-wrap', {from: 1.0, to: 0.0, duration: 1.0});
	}	

function updateSpots() {
	new Ajax.Request('includes/elementcreative/process_reservations.php', {
			parameters: {type:'getAvail',id:cal_id,date:cal_date},
			onSuccess: function(transport){
     			results = transport.responseText;
     			leftRes = maxRes - results;
     			if(leftRes > 0) {
     				if(leftRes == 1) {
     					$('spots-left').replace('<b id="spots-left">Only 1 spot left:</b>');
     					}
					else {
						$('spots-left').replace('<b id="spots-left">' + leftRes + ' spots left:</b>');
						}
					}
				else {
					$('spots-left').replace('<b id="spot-left">You got the last spot!</b>');
					$('btn-res-form').hide();
					}
					
				if($('spots-left')) {	
					new Effect.Highlight('spots-left',{duration: 2});	
					}
				}
  			});
	}	

function deleteRes(rid) {
	new Ajax.Request('includes/elementcreative/process_reservations.php', {
			parameters: {type:'delete',rid:rid,name:cal_name,date:cal_date,time:cal_time,location:cal_location},
			onSuccess: function(transport){
     			results = transport.responseText;
				$('res-list-confirm').replace('<div id="res-list-confirm">' + results + '</div>');
								
				getResList();
				updateSpots();
				}
			});
	//alert("This will delete rid:" + rid);
	}


function procResForm() {
	var err = "";
	if(trim($F('fname')) == "") {
		err += "first name\n";
		}
	
	if(trim($F('lname')) == "") {
		err += "last name\n";
		}	
	
	if(checkInternationalPhone($F('phone'))) {
		}
	else {		
		err += "telephone number\n";
		}
		
	if(validateEmail($F('email'))) {
		}
	else {
		err += "email address\n";		
		}	


	if(err != "") {
		alert("Please provide the following information:\n" + err);
		return false;
		}
	
	else {
		new Ajax.Request('includes/elementcreative/process_reservations.php', {
			parameters: $('res-form').serialize(true),
			onSuccess: function(transport){
     			results = transport.responseText;
     			//alert(results);
				$('res-form-confirm').replace('<div id="res-form-confirm">' + results + '</div>');
				$('res-form').hide();
				// This needs to only be called on successful data write.
				updateSpots();
				}
  			});

		}	
	}

function procCancelForm() {
	var err = "";
	
	if(checkInternationalPhone($F('cphone'))) {
		}
	else {		
		err += "telephone number\n";
		}
		
	if(validateEmail($F('cemail'))) {
		}
	else {
		err += "email address\n";		
		}	


	if(err != "") {
		alert("Please provide the following information:\n" + err);
		return false;
		}
	
	else {
		new Ajax.Request('includes/elementcreative/process_reservations.php', {
			parameters: $('cancel-form').serialize(true),
			onSuccess: function(transport){
     			results = transport.responseText;
     			//alert(results);
				$('cancel-form-confirm').replace('<div id="cancel-form-confirm">' + results + '</div>');
				$('cancel-form').hide();
				// This needs to only be called on successful data write.
				updateSpots();
				}
  			});

		}	
	}
