// Handles email integration
var sharedObject;

function setSharedObject(emailAddr)
{
 sharedObject = new Object()
 toAdd = new Array() 
 for(i=0;i<emailAddr.length; i++) {
   toAdd[i] = new Array();
   toAdd[i][0] = '';
   toAdd[i][1] = emailAddr[i];
 }
 sharedObject.toAdd = toAdd;
}

function getSharedObject()
{
 return sharedObject;
}
// window handling funcs
function __close() {
  if (IE >= 3 && IE < 4) {
    var win = window.open('', this.name, this.args)

    win.window.close()
  } else if (!this.hdl.closed) {
    this.hdl.close()
  }
}

function __isClosed() {
  return (IE >= 4 || NN > 2) && this.hdl.closed
}

function Window(url, name, args) {
  this.url = url
  this.name = name
  this.args = args
  this.close = __close
  this.isClosed = __isClosed
  this.hdl = window.open(url, name, args)
}
function sendMail(mailURL, mailType, emailaddress) 
{
  var emailArray = null;
  if (typeof emailaddress == 'string') {
    emailArray = new Array();
    emailArray[0] = emailaddress;
  }
  else {
    emailArray = emailaddress;
  }
  var newWindow = false;
  var notEmpty = false;
  var url = '';
  switch(mailType)
  {
    case 'Yahoo' :
      url = 'http://f34.mail.yahoo.com/py/ymTo.py?&To=';
      for (var i = 0; i < emailArray.length; i++) {
        if (emailArray[i].length > 0) {
          url = url + emailArray[i] + ',';
          notEmpty = true;
        }
      }
      url = url.substr(0, url.length -1);
      newWindow = true;
      break;
    case 'mailto' :   
    case 'desktop':
       url = "mailto:";
       for (var i = 0; i < emailArray.length; i++) {
        if (emailArray[i].length > 0) {
          url = url + emailArray[i] + ',';
          notEmpty = true;
        }
      }
      url = url.substr(0, url.length -1);
      break;

    case 'uc':
    default:
      url = mailURL + '?argv=view=compose&argv=laurel=on';
      setSharedObject(emailArray);
      for (var i = 0; i < emailArray.length; i++) {
        if( emailArray[i].length > 0) {
          notEmpty = true;
          break;
        }
      }

      newWindow = true;
      break;
  }

  if (notEmpty == true) {
    if (newWindow) {
       var a= new Window(url,'stdCompose','resizable=yes,width=740,height=640')
    }
    else {
      location.href = url;
    }
  }
  else {
    alert(i18n['No entry containing an email address selected']);
  }
}

// Handles IM integration
function sendIM(imService, userID) 
{
  var newWindow = false;
  var url = "";
  switch(imService)
  {
    case 'iPlanet' :
      if (userID != null) {
        url = "http://absinthe.red.iplanet.com/iim/StartConference?initiator="+curUserID;
        if ((userID.length != null) && (userID.constructor == Array)) {
          for (i = 0; i < userID.length; i++) {
            url += "&entity="+userID[i];
          }
          url += "&autoclose=5000";
        }
        else {
          url += "&entity="+userID+"&autoclose=5000";            
        }
      }
      newWindow = true;
      break;
    case 'AIM' :
      url = "aim:goim?screenname="+userID;
      break;
    case 'Yahoo' :
/*    url = "ymsgr:send?target="+userID; */
      url = "http://edit.yahoo.com/config/send_webmesg?.target="+userID+"&.src=pg"; 
      newWindow = true;
      break;
    case 'ICQ' :
/*
Uses a window from the icq client
      url = "http://web.icq.com/whitepages/message_me/1,,,00.icq?uin="+userID+"&action=message"; 
*/
      url = "http://wwp.icq.com/"+userID+"#pager";
      newWindow = true;
      break;
    case 'MSN' :
  }
  if (newWindow) {
    window.open(url);
  }
  else {
    window.location.href = url;
  }
}

// Handles calendar actions
function viewUWCCalendar(calArray, contexturi) 
{
  if( calArray != null && calArray.length == 0 )
  {
    alert(i18n['No entry containing an email address selected']);
    return;
  }

  var url= contexturi + "/base/UWCMain?forwardtodefview=true";
  
  if( calArray.length > 0 ) {
    url += "&caltype=temporaryCalids&calid=";
                
    for(i=0; i<calArray.length; i++)  {
     if( calArray[i].length > 0 )
       url += "mailto:" + calArray[i] + ";";
    }
   
    url = url.substring(0, url.length - 1);          
  } else {
    alert(i18n['Selection is empty, select some entries first to view calendar']);
    return;
  }
    
  if( parent )
    parent.location.href = url;
  else
    window.localtion.href = url;   
}

// Handles calendar actions
function viewCalendar(calendarUrl) 
{
  var url='';
  var calendarArray = null;
  if (typeof calendarUrl == 'string') {
    calendarArray = new Array();
    calendarArray[0] = calendarUrl;
  }
  else {
    calendarArray = calendarUrl;
  }

  if (calendarArray.length == 1) {
    url = calendarArray[0];
  }
  else {
    var urlPrefix = '';
    for (var i = 0; i < calendarArray.length; i++) {
      currentCal = calendarArray[i];
      if (currentCal == null || currentCal.length <= 0)
        continue;

      if (url == '') {
        urlPrefix = currentCal.substr(0, currentCal.indexOf('?'));
        url = urlPrefix + 'command.shtml?view=groupview&calid=';
      }
      if (currentCal.substr(0,currentCal.indexOf('?')) == urlPrefix) {
        var calidArray = currentCal.match(/calid=([^&]*)/);
        var calid = '';
        if (calidArray && calidArray.length > 0) {
          calid = calidArray[1];
        }
        if (calid.length > 0)
          url = url + calid + ';';

      }
   
    }
    url = url.substr(0, url.length -1);
  }
  window.open(url);
}

// Handles map integration
function popupLocationMap(country,city,zip,address,state){    
  var url="";
  var i=0;
  var found=false;
  if(country.length==0)
    country="US";
  
  europeCountryCode=new Array();
  ecountry=new Array();

  ecountry[0] = /(france)/i; 
  europeCountryCode[0] = "01033"; 
  ecountry[1] = /(holland)/i; 
  europeCountryCode[1] = "01031"; 
  ecountry[2] = /(england)/i; 
  europeCountryCode[2] = "01044"; 
  ecountry[3] = /(UK)/i; 
  europeCountryCode[3] = "01044"; 
  ecountry[4] = /(Great Britain)/i; 
  europeCountryCode[4] = "01044"; 
  ecountry[5] = /(United Kingdom)/i; 
  europeCountryCode[5] = "01044";
  ecountry[6] = /(Switzerland)/i; 
  europeCountryCode[6] = "01044"; 
  ecountry[7] = /(Suisse)/i; 
  europeCountryCode[7] = "01041"; 
  ecountry[8] = /(Italy)/i; 
  europeCountryCode[8] = "01039"; 
  ecountry[9] = /(Germany)/i; 
  europeCountryCode[9] = "01049"; 
  ecountry[10] = /(Allemagne)/i; 
  europeCountryCode[10] = "01049";
  ecountry[11] = /(spain)/i; 
  europeCountryCode[11] = "01034"; 
  ecountry[12] = /(espagne)/i; 
  europeCountryCode[12] = "01034";
  
  while((i < ecountry.length) && (!found)){             
    if(country.match(ecountry[i])!=null){
     address=encode(address);
     city=encode(city);
     country=europeCountryCode[i];
     url="http://wgs2.ismap.com/CGI-BIN/GEO/WGSWEBMAP.EXE?"
     url=url.concat("city=");
     url=url.concat(city);
     url=url.concat("&adresse=");
     url=url.concat(address);
     url=url.concat("&country=");
     url=url.concat(country);
     url=url.concat("&model=_nis.mod");
     url=url.concat("&type=13");
     found=true; 
     window.open(url, "map");
    }
    i++;
  }
  us = new Array();
  us[0] = /(USA)/i; 
  us[1] = /(US)/i; 
  us[2] = /(Etats Unis)/i; 
  us[3] = /(United States)/i;
  i=0;
  url="";
  if(!found){
    while((i < us.length) && (!found)){         
     if(country.match(us[i])!=null){ 
      address=encode(address);
      city=encode(city);
      zip=encode(zip);
      state=encode(state);
      country="US";
      url=url.concat("http://www.mapquest.com/maps/map.adp?&country=");
	  url=url.concat(country); 
      url=url.concat("&city=");
      url=url.concat(city);
      url=url.concat("&state=");
      url=url.concat(state);
      url=url.concat("&zipcode=");
      url=url.concat(zip);
      url=url.concat("&address=");
      url=url.concat(address);
      url=url.concat("&Find+Map=Get+Map");
      found=true;
      window.open(url, "map");          
     }
     i++;
    }      
  } 
}
