/**
 *
 */
function isValidEmail(str) {
    if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

/**
 *
 */
function isCharacter(str){
    if (str.match(/^[A-Za-z\ \-\']+$/) == null){
        return false;
    }
    else{
        return true;
    }
}

/**
 *
 */
function addGuest(formObj){
    var fname = formObj.fname.value;
    var lname = formObj.lname.value;
    var phone = formObj.sms.value;
    var email = formObj.email.value;
    var evn_id = formObj.evn_id.value;
    var return_url = formObj.return_url.value;

    if(email != ""){
        if(!isValidEmail(email)){
            alert("Please enter a valid Email");
            formObj.email.focus();
            return;
        }
    }

    if(fname != "" && lname != ""){
        if(fname != ""){
            if(!isCharacter(fname)){
                alert("The names can only contain letters");
                formObj.fname.focus();
                return;
            }
        }

        if(lname != ""){
            if(!isCharacter(lname)){
                alert("The names can only contain letters");
                formObj.lname.focus();
                return;
            }
        }

        var xmlHttpReq = false;
        var self = this;

        if(window.XMLHttpRequest){//Mozilla/Safari
            self.xmlHttpReq = new XMLHttpRequest();
        }
        else if(window.ActiveXObject) {//IE
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', "index.php", true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function(){
            if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
                document.getElementById("ajax_content").innerHTML = self.xmlHttpReq.responseText;

                var city_id = formObj.city.value;
                displayRegistrationInfo(evn_id,return_url,city_id);

                // Google Analytics tracking
				if(pageTracker){
					pageTracker._trackPageview();
					// debug
					// alert("URL tracked");
				}

				/*
				 * Here's the a trick for AJAX-based call of Quantcast
				 */
				var objXmlHttp_qoptions = null;
				try{objXmlHttp_qoptions = new XMLHttpRequest();}
				catch(e){
					try{objXmlHttp_qoptions = new ActiveXObject("Msxml2.XMLHTTP");}
					catch(e){objXmlHttp_qoptions = new ActiveXObject("Microsoft.XMLHTTP");}
				}
				objXmlHttp_qoptions.onreadystatechange = function() {
					if(objXmlHttp_qoptions.readyState == 4){
						strResultValue = '' + objXmlHttp_qoptions.responseText;
						// Execute
						eval(strResultValue);
						if(_qoptions){
							var objXmlHttp_quantcast = null;
							try{objXmlHttp_quantcast = new XMLHttpRequest();}
							catch(e){
								try{objXmlHttp_quantcast = new ActiveXObject("Msxml2.XMLHTTP");}
								catch(e){objXmlHttp_quantcast = new ActiveXObject("Microsoft.XMLHTTP");}
							}
							objXmlHttp_quantcast.onreadystatechange = function() {
								if(objXmlHttp_quantcast.readyState == 4){
									strResultValue = '' + objXmlHttp_quantcast.responseText;
									// Execute
									eval(strResultValue);
								}
							}
							objXmlHttp_quantcast.open("GET","./jscript/quantcast/quant.js",true);
							objXmlHttp_quantcast.send("");
						}
					}
				}
				objXmlHttp_qoptions.open("GET","./jscript/quantcast/quant_qoptions.js",true);
				objXmlHttp_qoptions.send("");

            }
        }

        formObj.fname.value = "";
        formObj.lname.value = "";
        formObj.sms.value = "";
        formObj.email.value = "";

        var save = 0;
        if(formObj.save.checked){
            formObj.save.checked = false;
            save = 1;
        }

        var reminder = 0;
        if(formObj.reminder.checked){
            formObj.reminder.checked = false;
            reminder = 1;
        }

        var invite = 0;
        if(formObj.invite.checked){
            formObj.invite.checked = false;
            invite = 1;
        }

        var attend = "a";
        if(formObj.attend[1].checked){
            attend = "n";
        }

        document.getElementById("ajax_content").innerHTML = "...loading...";
        self.xmlHttpReq.send("handler=events&action=add_guest&evn_id="+evn_id+"&fname="+fname+"&lname="+lname+"&phone="+phone+"&email="+email+"&save="+save+"&invite="+invite+"&attend="+attend+"&reminder="+reminder);
    }
    else{
        alert("Please enter First Name and Last Name");
        formObj.fname.focus();
        return;
    }
}

/**
 *
 */
function removeGuest(formObj,inv_id){
    document.getElementById("ajax_content").innerHTML = "...loading...";

    var xmlHttpReq = false;
    var self = this;

    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
            document.getElementById("ajax_content").innerHTML = self.xmlHttpReq.responseText;
        }
    }

    var evn_id = formObj.evn_id.value;
    self.xmlHttpReq.send("handler=events&action=remove_guest&evn_id="+evn_id+"&inv_id="+inv_id);
}

/**
 *
 */
function addSavedGuest(formObj){
    var xmlHttpReq = false;
    var self = this;

    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
            document.getElementById("guest_info").innerHTML = self.xmlHttpReq.responseText;
        }
    }

    var selectedIndex = formObj.saved_guests.options.selectedIndex;
    var guest_id = 0;
    if(selectedIndex > -1){
        guest_id = formObj.saved_guests.options[selectedIndex].value;
    }

    self.xmlHttpReq.send("handler=events&action=add_saved_guest&guest_id="+guest_id);
}
/**
 * Unregister event
 */
function UnregisterEvent(mem_id,evn_id,url){
    var xmlHttpReq = false;
    var self = this;
	var attend = document.getElementById("attend");
	var notattend = document.getElementById("notattend");
    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
        if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
            if(self.xmlHttpReq.responseText == 1){
                document.location.href = url;
            }
            else if(self.xmlHttpReq.responseText == 2){
                alert("You can not register for this event because of member city restriction.");
               	attend.checked = 'true'

            }
            else if(self.xmlHttpReq.responseText == 4){
                alert("The registration still has not opened or the registration is closed.");
               	attend.checked = 'true'
            }
        }
    }
    self.xmlHttpReq.send("handler=events&action=unreg&id="+evn_id);
}

/**
 * Register event
 */
function RegisterEvent(mem_id,level_id,evn_id,returnUrl,contextPath){
    if(level_id == 15){//only web member
        if(!confirm("Remember you can register to only 2 events per year. Register only if you are sure to come. In case of no-show, registration rights won't be recredited on your profile.")){
            return;
        }
    }

    var xmlHttpReq = false;
    var self = this;
	var attend = document.getElementById("attend");
	var notattend = document.getElementById("notattend");
    var send_cell_phone = document.getElementById("send_cell_phone");
    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
        if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
            errorNumber = self.xmlHttpReq.responseText;
            if(errorNumber == "1"){
                document.location.href = returnUrl;
            }
            else if(errorNumber == "2"){
                alert("You can not register for this event because of member city restriction.");
                notattend.checked="true";

            }
            else if(errorNumber == "3"){
                alert("You have reached the maximum number of events you can attend. Please contact FT Team for more explanations.");
               	notattend.checked="true";
            }
            else if(errorNumber == "4"){
                alert("The registration still has not opened or the registration is closed.");
            	notattend.checked="true";

            }
            else if(errorNumber == "5"){
                alert("You have reach the maximum of attendees.");
                notattend.checked="true";
            }
            else if(errorNumber.charAt(0) == '0'){
                var sur_id = errorNumber.substring(1,errorNumber.length);
                document.location.href = contextPath + "index.php?page=polls&section=vote&sur="+sur_id;
            }
        }
    }

    if(send_cell_phone){
        if(send_cell_phone.checked){
            self.xmlHttpReq.send("handler=events&action=reg&id="+evn_id+"&send_confirm=1");
        }
        else{
            self.xmlHttpReq.send("handler=events&action=reg&id="+evn_id);
        }
    }
    else{
        self.xmlHttpReq.send("handler=events&action=reg&id="+evn_id);
    }
}

/**
 *
 */
function handleRegistration(mem_id,level_id,evn_id,returnUrl,contextPath){
    var attend = document.getElementById("attend");
    var notattend = document.getElementById("notattend");
    if(attend.checked){
        RegisterEvent(mem_id,level_id,evn_id,returnUrl,contextPath);
    }
    else{
        if(notattend.checked){
            UnregisterEvent(mem_id,evn_id,returnUrl);
        }
    }
}

/**
 *
 */
function displayRegistrationInfo(evn_id,return_url,city_id){
    var xmlHttpReq = false;
    var self = this;

    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function(){
        if (self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200){
            document.getElementById("registration_info").innerHTML = self.xmlHttpReq.responseText;
        }
    }

    self.xmlHttpReq.send("handler=events&action=show_registration&evn_id="+evn_id+"&return_url="+return_url+"&city="+city_id);
}

function showConfirm(mem_id,evn_id,returnUrl,myGuests)
{
	var r=confirm("You currently have ["+ myGuests +"] guest(s) on your guest list.\nDo You still want to proceed with un-register request?");
	
	if(r==true)
	{
		UnregisterEvent(mem_id,evn_id,returnUrl);
	}
	else
	{
		return false;
	}
}
