window.onload=function()
{	var b, r, t = document.getElementsByTagName("TABLE");
	for(var i=0; i<t.length; i++)
	{	var current_t = t[i];
		if(current_t.className && current_t.className == "ema")
		{	b = current_t.getElementsByTagName("tbody");
			for(var j=0; j<b.length; j++)
			{	var current_b = b[j];
				r = current_b.getElementsByTagName("tr");
				for(var k=0; k<r.length; k+=2)
				{	r[k].className = "alternate";
				}
			}
		}
	}
}

function sendMessage(email_template_id,app_url,to_user_id){
  if(!Ext.WindowMgr.getActive()){
     var win = new Ext.Window({
        layout:'fit',
        width:600,
        height:425,
        closeAction: 'close',
        plain: true,
        id: 'my-id-'+ 21, 
        title:'Message',
        html: "<iframe name=\"extWindow\" id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='"+ app_url +"/email_template_id/"+ email_template_id +"/to_user_id/"+ to_user_id +"'></iframe>"
    });

    win.show();
  }
}

function extWin(app_url,title,width,height){
  if(!width) var width = 600;
  if(!height) var height = 425;
  
  if(!Ext.WindowMgr.getActive()){
     var win = new Ext.Window({
        layout:'fit',
        width:width,
        height:height,
        closeAction: 'close',
        plain: true,
        id: 'my-id-'+ 21, 
        title: title,
        html: "<iframe name=\"extWindow\" id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='"+ app_url +"'></iframe>"
    });

    win.show();
  }
}

function viewMessage(email_id,app_url){
  if(!Ext.WindowMgr.getActive()){
     var win = new Ext.Window({
        layout:'fit',
        width:600,
        height:520,
        closeAction: 'close',
        plain: true,
        id: 'my-id-'+ 59, 
        title:'Message',
        html: "<iframe name=\"extWindow\" id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='"+ app_url +"/email_id/" + email_id +"'></iframe>"
    });

    win.show();
  }
}

function flopThis(app_url,t_tip){

  if(!Ext.WindowMgr.getActive()){
    
    var t_id= 'content-anchor-tip_'+ t_tip;
    //kill associated open tool tip
    if(Ext.getCmp(t_id))
    {
      Ext.getCmp(t_id).hide();
    }
      
    var win = new Ext.Window({
      layout:'fit',
      width:760,
      height:490,
      closeAction: 'close',
      plain: true,
      id: 'my-id-'+ 21, 
      title: 'Details',
      html: "<iframe name=\"extWindow\" id='ifr' style='width:100%;height:100%;border:none;background-color:white' src='"+ app_url +"'></iframe>"
    });

    win.show();
  }
}

function saveFlop(o)
{
      Ext.MessageBox.show({
         msg: 'Submitting your request, please wait...',
         progressText: 'Saving...',
         width:300,
         wait:true,
         waitConfig: {interval:600},
         icon:'ext-mb-download', //custom class in msg-box.html
         animEl: o
     });
}

function validateProfile()
{
  user_req = ['first_name','last_name','email','company','title'];
  info_req = ['city','state'];
  company_req = ['message', 'supplier_url'];
  tab = 0;
  invalid = [];
  form = $('#form-editinfo');

  function check(sel, tb, alt_sel) {
    var item = $(sel, form);
    var val = $.trim(item.val());
    var label = item.parents('td').prev('th').text().replace('*', '');
    if (!val) { invalid.push(label); tab = tb; }
  }


  for(i=0; i<user_req.length; i++) check('#user_' + user_req[i], 0);
  for(i=0; i<info_req.length; i++) check('#user_info_' + info_req[i], 0);

  if(invalid.length)
  {
    Ext.Msg.alert("Alert",'The following information is required: '+invalid.join(', ')+'.');
    Ext.getCmp('form-tabs').setActiveTab(tab);
    return false;
  }
  else if (!checkDescription())
   {
    Ext.Msg.alert("Alert",'Your company description was too long, please keep it short (less than 500 characters)');
    Ext.getCmp('form-tabs').setActiveTab(1);    

   }
   else {
    $('#form-editinfo').submit();
  }
}

function update_textarea(obj)
{
    $('#message').text($(obj).attr('value')); //whatever you type in the textarea would be reflected in the div
}

function checkDescription() {

if ($('#message').attr('value').length >500 && $('#message').attr('value').length != '')
  return false

return true;

}
function openProfile(win)
{
  if(!win){
      win = new Ext.Window({
          applyTo:'hello-win',
          layout:'fit',
          width:550,
          height:498,
          closeAction:'hide',
          plain: true,

          items: new Ext.TabPanel({
              applyTo: 'hello-tabs',
              autoTabs:true,
              activeTab:0,
              deferredRender:false,
              border:false
          }),

          buttons: [{
              text:'Save',
              handler: function(){
                  validateProfile();
              }
          },{
              text: 'Cancel',
              handler: function(){
                  win.hide();
              }
          }]
      });
  }
  win.show(this);
}

