/*************************************************************/
$(document).ready(function() 
{
  $('.aExternal').attr('target', '_blank');


  $("#myform input").keypress(function (e) 
  {  
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
    {  
      doSubmitForm();
      return false;  
    } 
    else 
    {  
      return true;  
    }  
  });  

});



/*************************************************************/
function ppitup(url) 
{
  newwindow=window.open(url,'name','height=100,width=350');
  if (window.focus) {newwindow.focus()}
  return false;
}


/*************************************************************/
function changeExportTo(objExp)
{
  document.getElementById('strExportTo').value = objExp.id;

  $('#exportToPDF').removeClass('aChoosen');
  $('#exportToDOC').removeClass('aChoosen');
  $('#exportToXML').removeClass('aChoosen');
  $('#exportToCSV').removeClass('aChoosen');
  $('#exportToTXT').removeClass('aChoosen');
  $('#exportToXLS').removeClass('aChoosen');
  $('#exportToHTML').removeClass('aChoosen');

  $('#'+objExp.id).addClass('aChoosen');
  
  if (isAnyMethodSelected())
  {
    $('#aGo').fadeOut();
    $('#aGo').fadeOut();
    $('#aGo').fadeIn();
  }

  return false;
}

/*************************************************************/
function exportSettings(objExp, strFocusTo)
{
  document.getElementById('strExportSettings').value = objExp.id;

  bolDontSet = 0;
  if ($('#'+objExp.id).is('.aChoosen')) 
  { 
    bolDontSet = 1;
    document.getElementById('strExportSettings').value = "";
  }

  for (i=1; i<12; i++)
  {
    $('#aSettings'+i).removeClass('aChoosen');
    $('#aSettings'+i+'_div').css('display', 'none');
  }
  

  if (bolDontSet == 0)
  {
    $('#'+objExp.id).addClass('aChoosen');
    $('#'+objExp.id+'_div').css('display', 'block');

    document.getElementById(strFocusTo).focus();

    $('#aGo').fadeOut();
    $('#aGo').fadeOut();
    $('#aGo').fadeIn();
  }

  return false;
}

/*************************************************************/
function isAnyMethodSelected()
{
  for (i=1; i<12; i++)
  {
    if ($('#aSettings'+i+'_div').css('display') == 'block')
    {
      return true;
    }
  }
  return false;
}

/*************************************************************/
function doSubmitForm()
{

  if (isAnyMethodSelected())
  {
    $('body').fadeOut();
    document.getElementById('myform').submit();
    return false;
  }

  confirm('Please click on the item you want to export first!');
  //alert('Please click on the item you want to export first!');
  return false;
}

/*************************************************************/
function advOptions_selAll()
{
  document.getElementById('f__text').checked = true;
  document.getElementById('f__created_at').checked = true;
  document.getElementById('f__name').checked = true;
  document.getElementById('f__screen_name').checked = true;
  document.getElementById('f__in_reply_to').checked = true;
  document.getElementById('f__in_reply_to_status_id').checked = true;
  document.getElementById('f__source').checked = true;
  document.getElementById('f__id').checked = true;
  document.getElementById('f__recipient_name').checked = true;
  document.getElementById('f__recipient_screen_name').checked = true;
  document.getElementById('f__sender_name').checked = true;
  document.getElementById('f__sender_screen_name').checked = true;
  return false;
}

/*************************************************************/
function advOptions_selNone()
{
  document.getElementById('f__text').checked = false;
  document.getElementById('f__created_at').checked = false;
  document.getElementById('f__name').checked = false;
  document.getElementById('f__screen_name').checked = false;
  document.getElementById('f__in_reply_to').checked = false;
  document.getElementById('f__in_reply_to_status_id').checked = false;
  document.getElementById('f__source').checked = false;
  document.getElementById('f__id').checked = false;
  document.getElementById('f__recipient_name').checked = false;
  document.getElementById('f__recipient_screen_name').checked = false;
  document.getElementById('f__sender_name').checked = false;
  document.getElementById('f__sender_screen_name').checked = false;
  return false;
}

/*************************************************************/
function advOptions_show()
{
  $('#pAdvOptShow').css('display', 'none');
  $('#pAdvOptions').fadeOut("slow");
  $('#pAdvOptions').fadeOut("slow");
  $('#pAdvOptions').fadeIn("slow");

  return false;
}
/*************************************************************/









/*************************************************************/
/*************************************************************/
/*************************************************************/


/*
 * SimpleModal 1.3 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2009 Eric Martin
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 205 2009-06-12 13:29:21Z emartin24 $
 */
;(function($){var ie6=$.browser.msie&&parseInt($.browser.version)==6&&typeof window['XMLHttpRequest']!="object",ieQuirks=null,w=[];$.modal=function(data,options){return $.modal.impl.init(data,options);};$.modal.close=function(){$.modal.impl.close();};$.fn.modal=function(options){return $.modal.impl.init(this,options);};$.modal.defaults={appendTo:'body',focus:true,opacity:50,overlayId:'simplemodal-overlay',overlayCss:{},containerId:'simplemodal-container',containerCss:{},dataId:'simplemodal-data',dataCss:{},minHeight:200,minWidth:300,maxHeight:null,maxWidth:null,autoResize:false,zIndex:1000,close:true,closeHTML:'<a class="modalCloseImg" title="Close"></a>',closeClass:'simplemodal-close',escClose:true,overlayClose:false,position:null,persist:false,onOpen:null,onShow:null,onClose:null};$.modal.impl={opts:null,dialog:{},init:function(data,options){if(this.dialog.data){return false;}ieQuirks=$.browser.msie&&!$.boxModel;this.opts=$.extend({},$.modal.defaults,options);this.zIndex=this.opts.zIndex;this.occb=false;if(typeof data=='object'){data=data instanceof jQuery?data:$(data);if(data.parent().parent().size()>0){this.dialog.parentNode=data.parent();if(!this.opts.persist){this.dialog.orig=data.clone(true);}}}else if(typeof data=='string'||typeof data=='number'){data=$('<div/>').html(data);}else{alert('SimpleModal Error: Unsupported data type: '+typeof data);return false;}this.create(data);data=null;this.open();if($.isFunction(this.opts.onShow)){this.opts.onShow.apply(this,[this.dialog]);}return this;},create:function(data){w=this.getDimensions();if(ie6){this.dialog.iframe=$('<iframe src="javascript:false;"/>').css($.extend(this.opts.iframeCss,{display:'none',opacity:0,position:'fixed',height:w[0],width:w[1],zIndex:this.opts.zIndex,top:0,left:0})).appendTo(this.opts.appendTo);}this.dialog.overlay=$('<div/>').attr('id',this.opts.overlayId).addClass('simplemodal-overlay').css($.extend(this.opts.overlayCss,{display:'none',opacity:this.opts.opacity/100,height:w[0],width:w[1],position:'fixed',left:0,top:0,zIndex:this.opts.zIndex+1})).appendTo(this.opts.appendTo);this.dialog.container=$('<div/>').attr('id',this.opts.containerId).addClass('simplemodal-container').css($.extend(this.opts.containerCss,{display:'none',position:'fixed',zIndex:this.opts.zIndex+2})).append(this.opts.close&&this.opts.closeHTML?$(this.opts.closeHTML).addClass(this.opts.closeClass):'').appendTo(this.opts.appendTo);this.dialog.wrap=$('<div/>').attr('tabIndex',-1).addClass('simplemodal-wrap').css({height:'100%',outline:0,width:'100%'}).appendTo(this.dialog.container);this.dialog.data=data.attr('id',data.attr('id')||this.opts.dataId).addClass('simplemodal-data').css($.extend(this.opts.dataCss,{display:'none'}));data=null;this.setContainerDimensions();this.dialog.data.appendTo(this.dialog.wrap);if(ie6||ieQuirks){this.fixIE();}},bindEvents:function(){var self=this;$('.'+self.opts.closeClass).bind('click.simplemodal',function(e){e.preventDefault();self.close();});if(self.opts.close&&self.opts.overlayClose){self.dialog.overlay.bind('click.simplemodal',function(e){e.preventDefault();self.close();});}$(document).bind('keydown.simplemodal',function(e){if(self.opts.focus&&e.keyCode==9){self.watchTab(e);}else if((self.opts.close&&self.opts.escClose)&&e.keyCode==27){e.preventDefault();self.close();}});$(window).bind('resize.simplemodal',function(){w=self.getDimensions();self.opts.autoResize?self.setContainerDimensions():self.setPosition();if(ie6||ieQuirks){self.fixIE();}else{self.dialog.iframe&&self.dialog.iframe.css({height:w[0],width:w[1]});self.dialog.overlay.css({height:w[0],width:w[1]});}});},unbindEvents:function(){$('.'+this.opts.closeClass).unbind('click.simplemodal');$(document).unbind('keydown.simplemodal');$(window).unbind('resize.simplemodal');this.dialog.overlay.unbind('click.simplemodal');},fixIE:function(){var p=this.opts.position;$.each([this.dialog.iframe||null,this.dialog.overlay,this.dialog.container],function(i,el){if(el){var bch='document.body.clientHeight',bcw='document.body.clientWidth',bsh='document.body.scrollHeight',bsl='document.body.scrollLeft',bst='document.body.scrollTop',bsw='document.body.scrollWidth',ch='document.documentElement.clientHeight',cw='document.documentElement.clientWidth',sl='document.documentElement.scrollLeft',st='document.documentElement.scrollTop',s=el[0].style;s.position='absolute';if(i<2){s.removeExpression('height');s.removeExpression('width');s.setExpression('height',''+bsh+' > '+bch+' ? '+bsh+' : '+bch+' + "px"');s.setExpression('width',''+bsw+' > '+bcw+' ? '+bsw+' : '+bcw+' + "px"');}else{var te,le;if(p&&p.constructor==Array){var top=p[0]?typeof p[0]=='number'?p[0].toString():p[0].replace(/px/,''):el.css('top').replace(/px/,'');te=top.indexOf('%')==-1?top+' + (t = '+st+' ? '+st+' : '+bst+') + "px"':parseInt(top.replace(/%/,''))+' * (('+ch+' || '+bch+') / 100) + (t = '+st+' ? '+st+' : '+bst+') + "px"';if(p[1]){var left=typeof p[1]=='number'?p[1].toString():p[1].replace(/px/,'');le=left.indexOf('%')==-1?left+' + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"':parseInt(left.replace(/%/,''))+' * (('+cw+' || '+bcw+') / 100) + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"';}}else{te='('+ch+' || '+bch+') / 2 - (this.offsetHeight / 2) + (t = '+st+' ? '+st+' : '+bst+') + "px"';le='('+cw+' || '+bcw+') / 2 - (this.offsetWidth / 2) + (t = '+sl+' ? '+sl+' : '+bsl+') + "px"';}s.removeExpression('top');s.removeExpression('left');s.setExpression('top',te);s.setExpression('left',le);}}});},focus:function(pos){var self=this,p=pos||'first';var input=$(':input:enabled:visible:'+p,self.dialog.wrap);input.length>0?input.focus():self.dialog.wrap.focus();},getDimensions:function(){var el=$(window);var h=$.browser.opera&&$.browser.version>'9.5'&&$.fn.jquery<='1.2.6'?document.documentElement['clientHeight']:$.browser.opera&&$.browser.version<'9.5'&&$.fn.jquery>'1.2.6'?window.innerHeight:el.height();return[h,el.width()];},getVal:function(v){return v=='auto'?0:parseInt(v.replace(/px/,''));},setContainerDimensions:function(){var ch=this.getVal(this.dialog.container.css('height')),cw=this.dialog.container.width(),dh=this.dialog.data.height(),dw=this.dialog.data.width();var mh=this.opts.maxHeight&&this.opts.maxHeight<w[0]?this.opts.maxHeight:w[0],mw=this.opts.maxWidth&&this.opts.maxWidth<w[1]?this.opts.maxWidth:w[1];if(!ch){if(!dh){ch=this.opts.minHeight;}else{if(dh>mh){ch=mh;}else if(dh<this.opts.minHeight){ch=this.opts.minHeight;}else{ch=dh;}}}else{ch=ch>mh?mh:ch;}if(!cw){if(!dw){cw=this.opts.minWidth;}else{if(dw>mw){cw=mw;}else if(dw<this.opts.minWidth){cw=this.opts.minWidth;}else{cw=dw;}}}else{cw=cw>mw?mw:cw;}this.dialog.container.css({height:ch,width:cw});if(dh>ch||dw>cw){this.dialog.wrap.css({overflow:'auto'});}this.setPosition();},setPosition:function(){var top,left,hc=(w[0]/2)-((this.dialog.container.height()||this.dialog.data.height())/2),vc=(w[1]/2)-((this.dialog.container.width()||this.dialog.data.width())/2);if(this.opts.position&&this.opts.position.constructor==Array){top=this.opts.position[0]||hc;left=this.opts.position[1]||vc;}else{top=hc;left=vc;}this.dialog.container.css({left:left,top:top});},watchTab:function(e){var self=this;if($(e.target).parents('.simplemodal-container').length>0){self.inputs=$(':input:enabled:visible:first, :input:enabled:visible:last',self.dialog.data);if(!e.shiftKey&&e.target==self.inputs[self.inputs.length-1]||e.shiftKey&&e.target==self.inputs[0]||self.inputs.length==0){e.preventDefault();var pos=e.shiftKey?'last':'first';setTimeout(function(){self.focus(pos);},10);}}else{e.preventDefault();setTimeout(function(){self.focus();},10);}},open:function(){this.dialog.iframe&&this.dialog.iframe.show();if($.isFunction(this.opts.onOpen)){this.opts.onOpen.apply(this,[this.dialog]);}else{this.dialog.overlay.show();this.dialog.container.show();this.dialog.data.show();}this.focus();this.bindEvents();},close:function(){if(!this.dialog.data){return false;}this.unbindEvents();if($.isFunction(this.opts.onClose)&&!this.occb){this.occb=true;this.opts.onClose.apply(this,[this.dialog]);}else{if(this.dialog.parentNode){if(this.opts.persist){this.dialog.data.hide().appendTo(this.dialog.parentNode);}else{this.dialog.data.hide().remove();this.dialog.orig.appendTo(this.dialog.parentNode);}}else{this.dialog.data.hide().remove();}this.dialog.container.hide().remove();this.dialog.overlay.hide().remove();this.dialog.iframe&&this.dialog.iframe.hide().remove();this.dialog={};}}};})(jQuery);


/*************************************************************/


/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#basicModal input.basic, #basicModal a.basic').click(function (e) {
		e.preventDefault();
		$('#basicModalContent').modal();
	});
});


/*************************************************************/

/*
 * SimpleModal Confirm Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: confirm.js 185 2009-02-09 21:51:12Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#confirmDialog input.confirm, #confirmDialog a.confirm').click(function (e) {
		e.preventDefault();

		// example of calling the confirm function
		// you must use a callback function to perform the "yes" action
		confirm("Please click on the item you want to export first!", function () {
			return false;
		});
	});
});

function confirm(message, callback) {                      
	$('#confirm').modal({
		close:false,
		position: ["20%",],
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}

/*************************************************************/


/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 204 2009-06-09 22:43:28Z emartin24 $
 *
 */

$(document).ready(function () {
        $('#contactForm input.contact, #contactForm a.contact').click(function (e) {
                e.preventDefault();
                // load the contact form using ajax
                $.get("includes/contact.php", function(data){
                        // create a modal dialog with the data
                        $(data).modal({
                                closeHTML: "<a href='#' title='Close' class='modalCloseX simplemodal-close'>x</a>",
                                position: ["15%",],
                                overlayId: 'contact-overlay',
                                containerId: 'contact-container',
                                onOpen: contact.open,
                                onShow: contact.show,
                                onClose: contact.close
                        });
                });
        });

        // preload images
        var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
        $(img).each(function () {
                var i = new Image();
                i.src = 'http://twdoc.com/img/contact/' + this;
        });
});

var contact = {
        message: null,
        open: function (dialog) {
                // add padding to the buttons in firefox/mozilla
                if ($.browser.mozilla) {
                        $('#contact-container .contact-button').css({
                                'padding-bottom': '2px'
                        });
                }
                // input field font size
                if ($.browser.safari) {
                        $('#contact-container .contact-input').css({
                                'font-size': '.9em'
                        });
                }

                // dynamically determine height
                var h = 280;
                if ($('#contact-subject').length) {
                        h += 26;
                }
                if ($('#contact-cc').length) {
                        h += 22;
                }

                var title = $('#contact-container .contact-title').html();
                $('#contact-container .contact-title').html('Loading...');
                dialog.overlay.fadeIn(200, function () {
                        dialog.container.fadeIn(200, function () {
                                dialog.data.fadeIn(200, function () {
                                        $('#contact-container .contact-content').animate({
                                                height: h
                                        }, function () {
                                                $('#contact-container .contact-title').html(title);
                                                $('#contact-container form').fadeIn(200, function () {
                                                        $('#contact-container #contact-name').focus();

                                                        $('#contact-container .contact-cc').click(function () {
                                                                var cc = $('#contact-container #contact-cc');
                                                                cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
                                                        });

                                                        // fix png's for IE 6
                                                        if ($.browser.msie && $.browser.version < 7) {
                                                                $('#contact-container .contact-button').each(function () {
                                                                        if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
                                                                                var src = RegExp.$1;
                                                                                $(this).css({
                                                                                        backgroundImage: 'none',
                                                                                        filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
                                                                                });
                                                                        }
                                                                });
                                                        }
                                                });
                                        });
                                });
                        });
                });
        },
        show: function (dialog) {
                $('#contact-container .contact-send').click(function (e) {
                        e.preventDefault();
                        // validate form
                        if (contact.validate()) {
                                $('#contact-container .contact-message').fadeOut(function () {
                                        $('#contact-container .contact-message').removeClass('contact-error').empty();
                                });
                                $('#contact-container .contact-title').html('Sending...');
                                $('#contact-container form').fadeOut(200);
                                $('#contact-container .contact-content').animate({
                                        height: '80px'
                                }, function () {
                                        $('#contact-container .contact-loading').fadeIn(200, function () {
                                                $.ajax({
                                                        url: 'includes/contact.php',
                                                        data: $('#contact-container form').serialize() + '&action=send',
                                                        type: 'post',
                                                        cache: false,
                                                        dataType: 'html',
                                                        complete: function (xhr) {
                                                                $('#contact-container .contact-loading').fadeOut(200, function () {
                                                                        $('#contact-container .contact-title').html('Thank you!');
                                                                        $('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
                                                                });
                                                        },
                                                        error: contact.error
                                                });
                                        });
                                });
                        }
                        else {
                                if ($('#contact-container .contact-message:visible').length > 0) {
                                        var msg = $('#contact-container .contact-message div');
                                        msg.fadeOut(200, function () {
                                                msg.empty();
                                                contact.showError();
                                                msg.fadeIn(200);
                                        });
                                }
                                else {
                                        $('#contact-container .contact-message').animate({
                                                height: '30px'
                                        }, contact.showError);
                                }
                                
                        }
                });
        },
        close: function (dialog) {
                $('#contact-container .contact-message').fadeOut();
                $('#contact-container .contact-title').html('Goodbye...');
                $('#contact-container form').fadeOut(200);
                $('#contact-container .contact-content').animate({
                        height: 40
                }, function () {
                        dialog.data.fadeOut(200, function () {
                                dialog.container.fadeOut(200, function () {
                                        dialog.overlay.fadeOut(200, function () {
                                                $.modal.close();
                                        });
                                });
                        });
                });
        },
        error: function (xhr) {
                alert(xhr.statusText);
        },
        validate: function () {
                contact.message = '';
                if (!$('#contact-container #contact-name').val()) {
                        contact.message += 'Name is required. ';
                }

                var email = $('#contact-container #contact-email').val();
                if (!email) {
                        contact.message += 'Email is required. ';
                }
                else {
                        if (!contact.validateEmail(email)) {
                                contact.message += 'Email is invalid. ';
                        }
                }

                if (!$('#contact-container #contact-message').val()) {
                        contact.message += 'Message is required.';
                }

                if (contact.message.length > 0) {
                        return false;
                }
                else {
                        return true;
                }
        },
        validateEmail: function (email) {
                var at = email.lastIndexOf("@");

                // Make sure the at (@) sybmol exists and  
                // it is not the first or last character
                if (at < 1 || (at + 1) === email.length)
                        return false;

                // Make sure there aren't multiple periods together
                if (/(\.{2,})/.test(email))
                        return false;

                // Break up the local and domain portions
                var local = email.substring(0, at);
                var domain = email.substring(at + 1);

                // Check lengths
                if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
                        return false;

                // Make sure local and domain don't start with or end with a period
                if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
                        return false;

                // Check for quoted-string addresses
                // Since almost anything is allowed in a quoted-string address,
                // we're just going to let them go through
                if (!/^"(.+)"$/.test(local)) {
                        // It's a dot-string address...check for valid characters
                        if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
                                return false;
                }

                // Make sure domain contains only valid characters and at least one period
                if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
                        return false;        

                return true;
        },
        showError: function () {
                $('#contact-container .contact-message')
                        .html($('<div class="contact-error">').append(contact.message))
                        .fadeIn(200);
        }
};

