/*************   univision javascript functions ****************/

var iammc=true,city=0,ifvideo=false,iftransit=false,ifacity=false,mysite=window.location.host;

window.adon=false;
adimg=new Image(468,60);

/*addmc*/
function addmc(){

  var mylist,mypair,length,i,pos,content;
  var userid,password;
  mylist = document.cookie.split("; ");
  length=mylist.length;
  for(i=0;i<length;i++){
    pos=mylist[i].indexOf("=");
    tag=mylist[i].substr(0,pos);
    content=mylist[i].substr(pos+1);
    if(tag == "userid"){
      userid=unescape(content);
    }
    if(tag == "pass"){
      password=unescape(content);
    }
  }
  if(!password){
    theurl=unescape(top.text.location.href);
    var oldurl = theurl;
    theurl = oldurl.replace(/_temp/ig, "");   // using the g modifier in case of multiple pgtype in url
    top.text.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=" + escape("Add+report+password") + "&theurl=" + escape(theurl);
  } else {
    theurl=unescape(top.text.location.href);
    var oldurl = theurl;
    theurl = oldurl.replace(/_temp/ig, "");   // using the g modifier in case of multiple pgtype in url
    top.text.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=Add+report&thename=" + escape(userid) + "&thepassword=" + escape(password) + "&theurl=" + escape(theurl);
    top.toggleMenu( 'logIn', 'logOut', userid );
    showinexitinfo( "/mc/built.html" );
  }

}



/* getcity */
function getcity(){
  var mylist,mypair,length,i,pos,mycity,content;
  mylist = document.cookie.split("; ");
  length=mylist.length;
  ifshow=false;
  for(i=0;i<length;i++){
    pos=mylist[i].indexOf("=");
    tag=mylist[i].substr(0,pos);
    content=mylist[i].substr(pos+1);
    if(tag == "city"){
	    return(content);
    }
  }
  return("www");
}



/* savemc */
function savemc(){
  var expires,dat;
  dat = new Date();
  dat.setFullYear(dat.getFullYear() + 1);
  expires=dat.toGMTString();
  document.cookie= 'city=' + city  + "; path=/; expires=" + expires;
  document.cookie= 'texthref=' + escape(top.text.location.href) +"; path=/; expires=" + expires;
  if ( isNewInterface ) {
    showinexitinfo( "/mc/univision_startpageset.html" );
  } else {
    setexitinfo(1,'/mc/univision_startpageset.html');
  }

}


/* sendmc */
function sendmc(){
  var mylist,mypair,length,i,pos,content;
  var userid,password;
  if(checkbuild()){
    mylist = document.cookie.split("; ");
    length=mylist.length;
    for(i=0;i<length;i++){
      pos=mylist[i].indexOf("=");
      tag=mylist[i].substr(0,pos);
      content=mylist[i].substr(pos+1);
      if(tag == "userid"){
        userid=unescape(content);
      }
      if(tag == "pass"){
        password=unescape(content);
      }
    }
    if(!password){
      parent.exitinfo.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=" + escape("Add+Report+Password");
    } else {
      parent.text.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=Add+Report&thename=" + escape(userid) + "&thepassword=" + escape(password);
    }
  }
}


/*setcity*/
function setcity(newcity){

  city = newcity;

}



/* showmc */
function showmc(mymcset){
  var mylist,mypair,length,i,pos,content;
  mylist = document.cookie.split("; ");
  length=mylist.length;
  for(i=0;i<length;i++){
    pos=mylist[i].indexOf("=");
    tag=mylist[i].substr(0,pos);
    content=mylist[i].substr(pos+1);
    if(tag == "city"){
      setcity(content);
    }
    if(tag == "texthref"){
      mymcset = "1";
      top.text.location.href=unescape(content);
//      setexitinfo(1,'/mc/startpageview.html');            
    }
  }
  if(!mymcset){
    if ( isNewInterface ) {
      showinexitinfo( "/mc/univision_startpageunset.html" );
    } else {
      setexitinfo(1,'/mc/univision_startpageunset.html');
    }
  }
}



var isNewInterface = false;
function setInterface( myInterface ) {

  isNewInterface = myInterface;

}




function change_link( my_rollover, my_item ) {

  my_item.className = my_rollover;

}



function showinexitinfo( url ) {
 
  var exitwindow;
  var width = 400;
  var height = 300;
  var left = parseInt( screen.availWidth - width );
  var top = 0;
  var windowFeatures = "width=" + width + ",height=" + height + ",resizable,scrollbars,dependent,left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;

  var isVideo = new RegExp( "video" );

  if( !top.exitinfo || top.exitinfo.closed ) {
    exitwindow = window.open( url, "exitinfo", windowFeatures );
    exitwindow.focus();
  } else {
    if ( top.exitinfo.location.href != url ) {
      top.exitinfo.location.href = url;
    }      
    exitwindow.focus();
  }

}






function addinfo( myself, type ) {

  var myform = myself.form;
  var newhref;
  var mytext;

  if ( top.text ) {
    mytext = top.text;
  } else if ( window.opener ) {
    mytext = window.opener;
  }

  if ( mytext ) {
    newhref = mytext.location.href.replace( /&info=[^&]*/g, "" );
    for ( i=0; i<myform.info.length; i++ ) {
      if ( myform.info[ i ].checked ) { 
        newhref += "&info=" + myform.info[ i ].value;
      }
    }
    mytext.location.href = newhref.replace( / /g, "+" );
  }

}




function readCookie( name ) {

    var mylist, mylength, i, pos, tag, content, userid, password;
    mylist = document.cookie.split( "; ");
    mylength = mylist.length;

    for( i = 0; i < mylength; i++ ) {
      
      pos = mylist[ i ].indexOf( "=" );
      tag = mylist[ i ].substr( 0, pos );
      content = mylist[ i ].substr( pos + 1 );
      
      switch( tag ) {
      
        case "userid":
          userid = unescape( content );
          break;
        case "pass":
          password = unescape( content );
          break;
        default:
          break;

      }

    }

    if( !userid ) {
      
      text.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=" + escape( name ) + "%20Log%20In";

    } else {

      text.location.href="/cgi-bin/metro/univision_emaildb.pl?OP=" + escape( name ) + "&thename=" + escape( userid ) + "&thepassword=" + escape( password );

    }
       
}



function toggleMenu( menuChoice1, menuChoice2, thename ) {
   
    document.getElementById( menuChoice1 ).style.display = "none";
    document.getElementById( menuChoice2 ).style.display = "block";
   
    document.getElementById( "username" ).innerHTML = "Hola&nbsp; <span style=\"color: #c00\">" + thename + "<\/span> ";

    return false;

}



function toggleLink( menulink ) {
  
    var action = document.getElementById(menulink).style;

    if (action.display == "block") {
      action.display = "none";
    } else {
      action.display = "block";
    }
  
    return false;

}



/****************** this function is to be deleted ************************/
function getFrameSize_old( city, frameID ) {

  var frameSize = { height:0, width:0 };  

  if ( document.getElementById ) {
    var frame = top.document.getElementById( frameID );
    if ( frame.scrollWidth ) {
      frameSize.height = Math.round( frame.scrollHeight * 0.5 );
      frameSize.width = Math.round( frame.scrollWidth * 0.96 );
    }
  }

  if ( city == "newyork" ) {
    text.location.href="/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&x0=-15&x1=35&y0=-7&y1=24&control=off&area=" + city + "&width=" + frameSize.width + "&height=" + frameSize.height + "&noborder=off&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Transit+Construction&info=Construction+Alert&info=Parking+Regulations&info=Cameras";
  } else if ( city == "losangeles" ) {
    text.location.href="/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&x0=-118.9614&x1=-117.4179&y0=33.3787&y1=34.4392&control=off&area=" + city + "&width=" + frameSize.width + "&height=" + frameSize.height + "&noborder=off&info=Headline&info=Accident+Alert&info=Delay&info=Special+Event&info=Cameras";
  }

}




function getFrameSize( frameID ) {

  var frameSize = { height:0, width:0 };  

  if ( document.getElementById ) {
    var frame = top.document.getElementById( frameID );
    if ( frame.scrollWidth ) {
      frameSize.height = Math.round( frame.scrollHeight * 0.5 );
      frameSize.width = Math.round( frame.scrollWidth * 0.96 );
    }
  }

  return frameSize.width;


}





function resize_map( my_url ) {


  var width = getFrameSize( "text" );
  var height = Math.round(width / 2);


  var isZoommap = /zoommap/i;
  var isWidth   = /(width=)\d+/i; 
  var isHeight  = /(height=)\d+/i; 

  if ( my_url.match(isZoommap) ) {
    if ( my_url.match(isWidth) && my_url.match(isHeight) ) {
      my_url.replace( isWidth, "$1" + frameSize.width );
      my_url.replace( isHeight, "$1" + frameSize.height );
    } else {
      my_url = my_url + "&width=" + width + "&height=" + height;
    }
  }

  top.text.location.href = my_url;

}



function getFrameSize_old( frameID ) {

  var frameSize = { height:0, width:0 };  

  if ( document.getElementById ) {
    var frame = top.document.getElementById( frameID );
    if ( frame.scrollWidth ) {
      frameSize.height = Math.round( frame.scrollHeight * 0.5 );
      frameSize.width = Math.round( frame.scrollWidth * 0.96 );
    }
  }

  return frameSize.width;


}


function resize_map_old( my_url ) {



  var width = getFrameSize( "text" );
  var height = Math.round(width / 2);



//  if ( document.getElementById ) {
//    var frame = top.document.getElementById( "text" );
//    if ( frame.scrollWidth ) {
//      frameSize.height = Math.round( frame.scrollHeight * 0.5 );
//      frameSize.width = Math.round( frame.scrollWidth * 0.96 );
//    }
//  }

  var isZoommap = /zoommap/i;
  var isWidth   = /(width=)\d+/i; 
  var isHeight  = /(height=)\d+/i; 

  if ( my_url.match(isZoommap) ) {
    if ( my_url.match(isWidth) && my_url.match(isHeight) ) {
      my_url.replace( isWidth, "$1" + frameSize.width );
      my_url.replace( isHeight, "$1" + frameSize.height );
    } else {
      my_url = my_url + "&width=" + frameSize.width + "&height=" + frameSize.height;
    }
  }
    
  top.text.location.href = my_url;

}



function getWindowSize() {


  if ( typeof( window.innerWidth ) == 'number' ) {

    // Non-IE browser
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;

  } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    
    // IE 6+ in standard compliant mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;

  } else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

    // IE 4 compatible browser
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;

  }

}






function adjustIFrameSize() {


  // Get the size of the browser window
  getWindowSize();



  // Get the size of the document loaded inside the iframe
  // and adjust the size of the iframe

  var myIframe = document.getElementById( "text" );
  
  if ( myIframe ) {
    if ( myIframe.contentDocument && myIframe.contentDocument.body.offsetHeight ) {
      // W3C DOM syntax for NN 6
      myIframe.height = 0;
      myIframe.height = myIframe.contentDocument.body.offsetHeight + 30;
    } else if (  myIframe.Document && myIframe.Document.body.scrollHeight ) {
      // IE DOM syntax
      myIframe.height = 0;
      myIframe.height = myIframe.Document.body.scrollHeight + 30;
    }
  }
 


}




function zoom_to_region_ny( myRegion ) {

  var my_url = "/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&area=newyork&noborder=off&control=off&region=" + myRegion.options[ myRegion.selectedIndex ].value + "&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Construction+Alert";
  
  resize_map( my_url );
}




function zoom_to_region_la( myRegion ) {

  var my_url = "/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&area=losangeles&noborder=off&control=off&region=" + myRegion.options[ myRegion.selectedIndex ].value + "&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Construction+Alert";
  
  resize_map( my_url );

}




function loadContent( myContent ) {

  document.getElementById( "content" ).innerHTML = myContent;

}



//get url parameter
function getURLParam(strParamName) {

  var strReturn = "";
  var strHref = window.location.href;
  // if ( strHref.indexOf("?") > -1 ) {
  if ( strHref.indexOf("&p=") > -1 ) {
    // var strQueryString = strHref.substr(strHref.indexOf("&p=")).toLowerCase();
    var strQueryString = strHref.substr(strHref.indexOf("&p="));
    var strReturn = strQueryString.replace(/^&p=/gi, ""); 
    // var aQueryString = strQueryString.split("&");
    // for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
    //  if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
    //    var aParam = aQueryString[iParam].split("=");
    //    strReturn = aParam[1];
    //    break;
    //  }
    // }
  }
  return strReturn;
}



//send page to iframe
function load_page() {


  var dURL = getURLParam('p');

  var is_zoommap    = /zoommap/i; 
  var is_newyork    = /cbuniv_ny/i;
  var is_houston    = /cbuniv_ho/i;
  var is_losangeles = /cbuniv_la/i;



  if(dURL!='') {


    if ( dURL.match(is_zoommap) ) {
      resize_map( dURL );
    } else { 
      document.getElementById('text').src = dURL;
    }


  } else {


    if ( top.location.href.match(is_newyork) ) { 

      resize_map( '/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&area=newyork&x0=-15&x1=35&y0=-7&y1=24&control=off&noborder=off&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Construction+Alert&info=Transit+Construction+Alert' );

    } else if ( top.location.href.match(is_houston) ) { 

     resize_map( '/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&area=houston&x0=-95.8250&x1=-94.9260&y0=29.3990&y1=30.1770&control=off&noborder=off&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Construction+Alert&info=Transit+Construction+Alert' );

    } else if ( top.location.href.match(is_losangeles) ) { 

     resize_map( '/cgi-bin/metro/univision_zoommap.pl?pgtype=univision_map_reports&area=losangeles&x0=-118.9614&x1=-117.4179&y0=33.3787&y1=34.4392&control=off&noborder=off&info=Headline&info=Special+Event&info=Transit+Alert&info=Accident+Alert&info=Delay&info=Construction+Alert&info=Transit+Construction+Alert' );

    }

  }


}
