//siteUrl = "http://mycues.staging.cues.org/";
function call_ajax(redirectUrl){
    $.post(
            siteUrl+"login/index/get-user",
            {
                    user        : $('#email').val()
            }, 
            function(data)
            {
                
            }, 
            "html");
    $.post(
            siteUrl+"login/index/login",
            {
                email       : $('#email').val(),
                password    : $('#password').val()
            }, 
            function(data)
            {
                //alert(data);
                if (data.login == true){
                    isLogged = 1;
                    $.modal.close();
                    $('div#topHeader').html('<div id="logout"><a href="'+siteUrl+'/my-cues">My CUES</a> | <a href="#">Profile</a> | <a href="'+siteUrl+'/logout">Logout</a></div>');
                
                    //alert('submit login form ...');
                    
                    if(redirectUrl != '' && redirectUrl != null && redirectUrl != undefined){
                        //window.location.replace(redirectUrl);
                        window.location.href = redirectUrl;
                    }
                    
                    if (redirectUrl.substr(-13) == '/fullReport/1'){
                        $('#fileType').val('full');
                        $('#downloadForm').submit();
                    }
                }
                else{
                    alert('Login failed!');
                }
            }, 
            "json");
    
    /* var url = "<?php echo Zend_Registry::get('config1')->site->mag_url; ?>login/index/login/us/" + $('#email').val();
    $.ajax({
          type: "POST",
          url: url,
          dataType: "html"
        }); */
            
}

 
function modalLogin(options){
    var chosen_message='';
    //default_message = 'Our membership services team can help you out right away, just click to chat.';  
    default_message = 'Please join as a <a href="'+siteUrl+'mycues/signup/executive/">CUES Member</a> or <a href="'+siteUrl+'mycues/signup/director/">CUES Director Member</a> to review the Full Report.';  
    
    if (typeof(options.message) == 'undefined') {
        chosen_message += default_message;
    } else{                  
        chosen_message += options.message;
    }                                      
    $.modal('<div class="modalWindow" id="modal1">'+
            '<div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center">'+
            
            '<div style="float:right; margin-top:-10px;"><img src="'+siteUrl+'images/controls/modalClose.png" onClick="$.modal.close();" class="action"></div>'+
            
            '<h1 class="boxTitle">Please login first</h1><form name="login_form" method="post" onSubmit="return false;">'+
            '<table><tr><td style="padding-left:15px;" align="right">Username:</td><td align="left">'+
            '<input class="inputText" type="text" name="email" id="email" /></td></tr><tr><td style="padding-left:15px;" align="right">Password:</td><td align="left">'+
            '<input class="inputText" type="password" name="password" id="password" /></td></tr>'+
            '<tr><td colspan="2" align="right">'+
            '<a href="'+siteUrl+'shop-cues/membership"><img src="'+siteUrl+'images/mycues/default/layouts/main/btnJoin.png" alt="Join now!"/></a> '+
            '<input onclick="call_ajax(\''+  options.redirectUrl +'\');" class="btnInput" type="image" src="'+siteUrl+'images/mycues/default/layouts/main/login_btn.jpg"></td>'+
            '</tr></table></form>'+
            '<hr class="dottedHR" /><a href="#">Forgot your username or password?</a>'+
            '<br/><br/>'+chosen_message+'</div></div></div><div class="mbottom"></div></div>');  
}

function modalMessage(message, title, refreshPage){
	if(title == undefined){
		title = '';
	}
    
    if (typeof(refreshPage) != 'undefined')
    {
        $.modal('<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">'+ title +'</h1><br/><h4>'+ message +'</h4><br/><input onclick="$.modal.close(); location.reload();" class="btnInput" type="image" src="'+siteUrl+'images/mycues/default/layouts/main/cancel_btn.jpg"></div></div></div><div class="mbottom"></div></div>');
    }
    else
    {
        $.modal('<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">'+ title +'</h1><br/><h4>'+ message +'</h4><br/><input onclick="$.modal.close();" class="btnInput" type="image" src="'+siteUrl+'images/mycues/default/layouts/main/cancel_btn.jpg"></div></div></div><div class="mbottom"></div></div>');
    }
}

function postAjax(url, modalMsg, formId){
    
    $.ajax(
    {
        type: "POST",
        url: url,
        data: $('#'+formId).serialize(),     
        success: function(msg)
        {
            if(modalMsg !== true)
            {
                $('#'+formId).html(msg);
            } 
            else 
            {
                $.modal.close();
                modalMessage(msg);
            }
        }
    });
}

function modalFormOld(title,url,options,beforeForm, afterForm, buttons, modalMsg){
        var postData;
        var html = '<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">'+title+'</h1>'+ ((beforeForm != undefined && beforeForm != null) ? beforeForm : '') +'<form id="frm12345" method="post" onSubmit="return false;"><table>';
        $.each(options, function(){
            html+= '<td style="padding-left:15px;" align="right">'+this.label+': </td><td align="left" class="modalFormInput"><' + (this.type == 'text' ? 'input' : ( this.type == 'textarea' ? 'textarea' : 'input' )) + ' '+(this.value != undefined ? 'value="'+this.value+'" ':'""')+' class="txtInput" ' +( this.type != 'textarea' ? 'type="'+this.type+'"' : '' )+ ' name="'+this.name+'" id="'+this.name+'" '+ ( this.type != 'textarea' ? '/>' : '></textarea>' ) +' </td></tr>';
        });
        html+= '<tr><td colspan="2" align="right"><input onclick="$.modal.close();" class="btnInput" type="image" src="' +((buttons == undefined || buttons.cancel == undefined) ? siteUrl+'images/mycues/default/layouts/main/cancel_btn.jpg' : buttons.cancel )+ '">&nbsp;<input onclick="postAjax(\''+url+'\', true);" class="btnInput" type="image" src="' +((buttons == undefined || buttons.go == undefined) ? siteUrl+'images/mycues/default/layouts/main/login_btn.jpg' : buttons.go )+ '"></td></tr>';
        html += '</table></form><hr class="dottedHR" /><br/>'+ ((afterForm != undefined && afterForm != null) ? afterForm : '') +'</div></div></div><div class="mbottom"></div></div>';
        $.modal(html);
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}


function modalForm(formIdKey, title,url,options,beforeForm, afterForm, buttons, modalMsg){
        var postData;
        var formId = "modalForm" + formIdKey;
        // so that we can use multiple forms on one page
        var html = '<div class="modalWindow" id="modal1"><div class="mtop"></div><div class="mmain"><div class="mcenter"><div class="center"><h1 class="boxTitle">'+title+'</h1>'+ ((beforeForm != undefined && beforeForm != null) ? beforeForm : '');
        html += '<form id="'+formId+'" method="post" onSubmit="return false;" style="text-align:center">';
        $.each(options, function(){
            if (this.type == "hidden")
            html+= '<input type="hidden" id="'+this.name+'" name="'+this.name+'" value="'+this.value+'"/>';
        });

        html += '<table style="margin: auto;text-align:left"">';                    
        $.each(options, function(){
            if (this.type != "hidden")
                html+= '<td><label for="'+this.name+'">'+this.label+'</label><br/><' + (this.type == 'text' ? 'input' : ( this.type == 'textarea' ? 'textarea' : 'input' )) + ' '+(this.value != undefined ? 'value="'+this.value+'" ':'""')+' class="'+this.classValidation+'" ' +( this.type != 'textarea' ? 'type="'+this.type+'"' : '' )+ ' name="'+this.name+'" id="'+this.name+'" '+ ( this.type != 'textarea' ? '/>' : '></textarea>' ) +' </td></tr>';
        });
        html+= '<tr><td align="center"><input onclick="$.modal.close();" class="btnInput" type="image" src="' +((buttons == undefined || buttons.cancel == undefined) ? siteUrl+'images/controls/btnClose.png' : buttons.cancel )+ '">&nbsp;<input onclick="submitModalForm(\''+url+'\', true, \''+formId+'\');" class="btnInput" type="image" src="' +((buttons == undefined || buttons.go == undefined) ? siteUrl+'images/controls/btnSend.png' : buttons.go )+ '"></td></tr>';
        html += '</table></form>'+ ((afterForm != undefined && afterForm != null) ? afterForm : '') +'</div></div></div><div class="mbottom"></div></div>';
        $.modal(html);
        var validator = $("#" + formId).validate({meta: "validate"});
}

function submitModalForm(url, modalMsg, formId)
{
    //alert($("#" + formId).valid());
    if ( $("#" + formId).valid() == true ) 
        postAjax(url, modalMsg, formId);   
}
function requestBrochure(file){
    modalForm(
        'requestBrochure',
        'Request a Brochure',
        siteUrl + "request-brochure/request-brochure",
        [
            {
                name:'name',
                label:"<span class='registerRequired'>*</span>Name", 
                type:'text',
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"  
            },{
                name:"email",
                label:"<span class='registerRequired'>*</span>E-mail Address",
                type:"text",
                classValidation:"inputText {validate:{required:true, email:true, messages:{required:' *', email:' *'}}}"
            },
            {
                name:"fileName",
                type:"hidden",
                value:file
            }
        ],
        '',
        '<a href="'+siteUrl+'data/brochures/'+ file +'">Download Brochure Now</a>'
    );
}

function keepMePosted(eventId){
    modalForm(
        'keepMePosted',
        'Keep Me Posted',
        siteUrl + "keep-me-posted/keep-me-posted",
        [
            {
                name:'firstName',
                label:"<span class='registerRequired'>*</span>First Name", 
                type:'text',
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"  
            },
            {
                name:'lastName',
                label:"<span class='registerRequired'>*</span>Last Name", 
                type:'text',
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"  
            },
            {
                name:"email",
                label:"<span class='registerRequired'>*</span>E-mail Address",
                type:"text",
                classValidation:"inputText {validate:{required:true, email:true, messages:{required:' *', email:' *'}}}"
            }
            ,
            {
                name:"eventId",
                type:"hidden",
                value:eventId
            }
        ]
    );
}
    
function suggestSession(){                   
    modalForm(
        'sugestSession',
        'Suggest a Session',
        siteUrl + "suggest-session/send-suggestion",  
        [
            {
                name:"firstName",
                label:"<span class='registerRequired'>*</span>First Name",
                type:"text",
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"
            },
            {
                name:"lastName",
                label:"<span class='registerRequired'>*</span>Last Name",
                type:"text",
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"
            },
            {
                name:"speakerName",
                label:"<span class='registerRequired'>*</span>Speaker Name",
                type:"text",
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"
            },
            {
                name:"title",
                label:"<span class='registerRequired'>*</span>Title",
                type:"text",
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"
            },
            {
                name:"organization",
                label:"Organization",
                type:"text",
                classValidation:"inputText"
            },
            {
                name:"references",
                label:"Past Presentations/References",
                type:"textarea",
                classValidation:"inputText"
            },
            {
                name:"email",
                label:"<span class='registerRequired'>*</span>E-mail Address",
                type:"text",
                classValidation:"inputText {validate:{required:true, email:true, messages:{required:' *', email:' *'}}}"
            },     
            {
                name:"topic",
                label:"<span class='registerRequired'>*</span>Session Topic",
                type:"text",
                classValidation:"inputText {validate:{required:true, messages:{required:' *'}}}"
            },
            {
                name:"topicDescription",
                label:"Describe Session Topic Proposed",
                type:"textarea",
                classValidation:"inputText"
            },
            {
                name:"biography",
                label:"Brief Biography of Speaker",
                type:"textarea",
                classValidation:"inputText"
            }
        ]
    );
}    