
/*
 * Ext JS Library 1.1.1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://www.extjs.com/license
 */

var NTV = function(){
    var dialog, viewLink;
    var mailto, name, subject, comment;
    var pnl,videoLink;
    
    return {
        init: function(){
            
            viewLink = Ext.get('view-comments');
            mailto = Ext.get('mailto');
            name = Ext.get('name');
            subject = Ext.get('subject');
            comment = Ext.get('comment');
            videoLink = Ext.get('video_link');


            // showBtn = Ext.get('subscribe-btn');
            showBtn = Ext.get('subscribe_btn');
            smailto = Ext.get('subScribeEmail');
            
            this.createDialog();
            
            
            viewLink.on('click', function(e){
                dialog.show();
                dialog.center();
            });
            
            
            showBtn.on('click', function(e){
                var emailAddress = smailto.dom.value;
                
                if (NTV.validate(emailAddress)) {
                    SubscribeTVResults.init(emailAddress);
                }
                else {
                    Ext.MessageBox.alert('Newport TV', 'Please enter a valid email address', null)
                }
            });
            
        },
        
        
        // submit the comment to the server
        submitComment: function(){
        
            var emailAddress = pnl.items.items[0].getEl().dom.value;
            var contact = pnl.items.items[1].getEl().dom.value;
            var sSubject = pnl.items.items[2].getEl().dom.value;
            var sComment = pnl.items.items[3].getEl().dom.value;
            var mId = 3;
            var url = videoLink.dom.value;
            
            
            
            
            
            if (NTV.validate(emailAddress) && NTV.validate_required(contact)) {
                ShareMediumResults.init(emailAddress, contact, sSubject, sComment, mId, url);
                
            }
            
        },
        
        createDialog: function(){
			var url='';
			if (videoLink!=null){
				url=videoLink.dom.value
			};
            pnl = new Ext.FormPanel({
                items: [{
                    fieldLabel: 'Recipient\'s email address',
                    name: 'mailto',
                    vtype: 'email',
                    anchor: '100%'
                }, {
                    fieldLabel: 'Your Name',
                    name: 'name',
                    allowBlank: false,
                    anchor: '100%'
                }, {
                    fieldLabel: 'Subject',
                    name: 'subject',
                    value: 'You have received a Newport video',
                    anchor: '100%'
                }, {
                    ytype: 'textarea',
                    //hideLabel:true,
                    fieldLabel: 'Comment',
                    name: 'comment',
                    anchor: '100% -53',
                    value: comment
                }],
                baseCls: 'x-plain',
                labelWidth: 100,
                defaultType: 'textfield',
                //renderTo:document.body,
                shadow: true
            
            });
            
            
            
            dialog = new Ext.Window({
                title: 'Share Videos',
                minimizable: true,
                layout: 'fit',
                closeAction: 'hide',
                plain: true,
                bodyStyle: 'padding:5px',
                buttonAlign: 'center',
                items: pnl,
                width: 500,
                height: 350,
                buttons: [{
                    text: 'Send',
                    handler: function(){
                        NTV.submitComment();
                    }
                }, {
                    text: 'Cancel',
                    handler: function(){
                        dialog.hide();
                    }
                }]
            
            
            });
            
            
            // auto fit the comment box to the dialog size															 
         //   var sizeTextBox = function(){
                //txtComment.setSize(dialog.size.width-44, dialog.size.height-264);
           // };
            //sizeTextBox();
            //dialog.on('resize', sizeTextBox);
        
        
        },
        
        
        //close dialog
        closeDialog: function(){
        
            dialog.hide();
        },
        
        
        //display section list
        showdiv: function(section){
            var content = document.getElementById('morevideos_scrollwindow');
            
            var top = elementTop(content) + content.offsetHeight;
            var left = elementLeft(content);
            
            var divs = content.getElementsByTagName('div');
            
            
            for (var i = 0; i < divs.length; i++) {
                if (divs[i].id.match(section)) {//if they are 'see' divs 
                    var active_lnk = document.getElementById('lnk_' + divs[i].id);
                    active_lnk.className = "morevideos_link_active";
                    
                    if (document.getElementById) // DOM3 = IE5, NS6 
                    {
                    
                        divs[i].style.left = left;
                        divs[i].style.top = top;
                        divs[i].style.visibility = "visible";// show/hide
                        divs[i].style.display = "";
                        
                    }
                    else 
                        if (document.layers) // Netscape 4 
                        {
                        
                            document.layers[divs[i]].display = 'visible';
                        }
                        else // IE 4 
                        {
                        
                            document.all.divs[i].display = '';
                            document.all.divs[i].visibility = 'visible';
                        }
                    
                    
                }
                else {
                
                    var inactive_lnk = document.getElementById('lnk_' + divs[i].id);
                    
                    if (inactive_lnk) {
                        inactive_lnk.className = "morevideos_link";
                    }
                    
                    if (document.getElementById) {
                    
                        divs[i].style.display = "none";
                        divs[i].style.visibility = "hidden";
                        
                    }
                    else 
                        if (document.layers) // Netscape 4 
                        {
                            document.divs[i].style.display = "none";
                            document.divs[i].visibility = "hidden";
                        }
                        else // IE 4 
                        {
                        
                            document.all.divs[i].display = 'none';
                            document.all.divs[i].visibility = 'hidden';
                        }
                    
                    
                }
                
            }
        },
        
        
  
        // check if it's valid email
        isValidEmail: function(email, required){
            if (required == undefined) { // if not specified, assume it's required
                required = true;
            }
            if (email == null) {
                if (required) {
                    return false;
                }
                return true;
            }
            if (email.length == 0) {
                if (required) {
                    return false;
                }
                return true;
            }
            if (!this.allValidChars(email)) { // check to make sure all characters are valid
                return false;
            }
            if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
                return false;
            }
            else 
                if (email.lastIndexOf(".") <= email.indexOf("@")) { // last dot must be after the @
                    return false;
                }
                else 
                    if (email.indexOf("@") == email.length) { // @ must not be the last character
                        return false;
                    }
                    else 
                        if (email.indexOf("..") >= 0) { // two periods in a row is not valid
                            return false;
                        }
                        else 
                            if (email.indexOf(".") == email.length) { // . must not be the last character
                                return false;
                            }
            return true;
        },
        
        allValidChars: function(email){
            var parsed = true;
            var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
            for (var i = 0; i < email.length; i++) {
                var letter = email.charAt(i).toLowerCase();
                if (validchars.indexOf(letter) != -1) 
                    continue;
                parsed = false;
                break;
            }
            return parsed;
        },
        
        validate: function(email){
            if (!this.isValidEmail(email)) {
                Ext.MessageBox.alert('Newport TV', 'Please enter a valid email address', null)
               
                return false;
            }
        
            return true;
        },
        
        
        validate_required: function(contact){
        
            if (contact == null || contact == "") {
                Ext.MessageBox.alert('Newport TV', 'Please enter your name', null)
              
                return false
            }
            
            return true
            
            
        }
        
        
        
    };
}
();

Ext.onReady(NTV.init, NTV, true);













