// JavaScript Document


//------------------------------------ Displays a real time clock ----------------------------------------//

function Display_Time(currentlang){
//alert(currentlang);
		var currenttime = new Date();
		var hours = currenttime.getHours();
		var minutes = currenttime.getMinutes();
		var seconds = currenttime.getSeconds();
		var timesuffix = "AM";
		
		if ( currentlang == "en" ){
			  
					if (hours > 11){
						timesuffix = "PM";
						hours = hours - 12;
					}
					
					if (hours == 0){
						hours = 12;
					}
		}
		else {
			
			 var timesuffix = ""; // fr
			
		}
		
		if (minutes < 10){
			minutes = "0" + minutes;
		}
		
		if (seconds < 10){
			seconds = "0" + seconds;
		}

		var clocklocation = document.getElementById("clock");
		clocklocation.innerHTML = hours + ":" + minutes + ":" + seconds + " " + timesuffix;

		setTimeout("Display_Time('"+currentlang+"')", 1000);
	
}



//--------------------------------- Set Menu Width -----------------------------------//

function Set_Menu_Width() {
	var menu_width = 0;	
	if ( document.getElementById("menu") ) {
		if ( document.getElementById("navmenu") ) {
  	var menu = document.getElementById("menu");
  	var menu_ul = document.getElementById("navmenu"); 
			//alert(menu_ul.offsetWidth);
	  //menu.style.width = String(parseInt(menu_ul.offsetWidth + 34) )+ "px";
		 if ( parseInt(menu_ul.offsetWidth + 4) <= 738 ) {
			 menu.style.width = '738px';
			} else {
				menu.style.width = String(parseInt(menu_ul.offsetWidth + 4) )+ "px";
			}
		}
	}
}


// ------------------------------  Menu Functions ------------------------------------//

function hover_onmouseover( obj, background_img, current_browser ) {
  
		if ( current_browser == "Explorer" ) {
   obj.className += " iehover";			
		}
		obj.style.background = "url(" + background_img + ")";

}

function hover_onmouseout( obj, background_img, current_browser ) {
 
	if ( current_browser == "Explorer" ) {
	 obj.className = obj.className.replace(new RegExp(" iehover\\b"), "");
	}
	obj.style.background = "url(" + background_img + ")";

}


// -------------- Shows the clicked row of a list. Example : news list ---------------//

function List_Show_Hide( list_name, total_rows, current_id ) {
	
 var list_row;
	var i;
	
	for ( i = 1; i <= total_rows; i++ ) {		

		list_row = document.getElementById( list_name + "_" + i );

  if ( i == current_id ) {
			if ( list_row.style.display == '' ){
		  list_row.style.display='none';
			} else {
		  list_row.style.display='';
			}
		} else { 
		 list_row.style.display='none';
		}
	}

}


// ------------------------- Economic Newsletter Form Validation ----------------------------- //

function Check_Economic_Newsletter_Form( form_name ){
	
	var ok = true; // to bypass validation
	
	if ( ok ){
			
			if( document.forms[form_name].elements['name'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_NAME' ) );
				document.forms[form_name].elements['name'].focus();
				return false;
			}
				
			if( document.forms[form_name].elements['address'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_ADDRESS' ) );
				document.forms[form_name].elements['address'].focus();
				return false;
			}
			
			if( document.forms[form_name].elements['city'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_CITY' ) );
				document.forms[form_name].elements['city'].focus();
				return false;
			}
		
			if( document.forms[form_name].elements['province'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_PROVINCE' ) );
				document.forms[form_name].elements['province'].focus();
				return false;
			}
			
			if( document.forms[form_name].elements['postal_code'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_POSTAL_CODE' ) );
				document.forms[form_name].elements['postal_code'].focus();
				return false;
			}

			if( document.forms[form_name].elements['email'].value == "" ){
				alert( getTxt( 'MSG_EMPTY_EMAIL' ) );
				document.forms[form_name].elements['email'].focus();
				return false;
			}
			
			if( document.forms[form_name].elements['french'].checked == false && document.forms[form_name].elements['english'].checked == false){
				alert( getTxt( 'MSG_EMPTY_LANGUAGE' ) );
				return false;
			}
			
	}
	
	document.forms[form_name].submit();

}


// ------------------------- Opens a new window ----------------------------- //

function Open_Window( url, win_name, features ) {
  my_win = window.open( url, win_name, features);
		my_win.focus();
}

function Popup_Div( url, sender, width, height, title, center ){
  if ( document.getElementById("popup_div") ) {
    return;
  }
    
  var scrollY = $j(window).scrollTop();
  var innerWidth = $j(window).width();
  var innerHeight = $j(window).height();
  var pixelWidth = width;
  
  if ( width.toString().indexOf('%') > -1 ) {
    pixelWidth = innerWidth * (parseFloat(width) / 100);
  }
  
  if ( height.toString().indexOf('%') > -1 ) {
    height = innerHeight * (parseFloat(height) / 100);
  }
  
  var curleft = curtop = 0;
  var obj = sender;
  if ( !center ) {
    curleft += $j(obj).offset().left;
    curtop += $j(obj).offset().top;
    
    curtop += $j(sender).height()+5;
    /*if ( scrollY && scrollY >= curtop ) {
     curtop = scrollY + 15;
     curleft += $j(sender).width() +10;
    }*/
    
    if ( innerWidth <= (curleft + pixelWidth) ) {
     curleft = innerWidth - pixelWidth;
    }
  } else {
    curleft = (innerWidth/2) - (pixelWidth/2);
    curtop = (innerHeight/2) - (height/2);
  }
    
   if ( curleft < 10 ) {
     curleft = 10;
   }
   
   if ( curleft+width+4 > innerWidth ) {
      curleft = curleft - ((curleft+width+4) - innerWidth);
   }
   
   if ( curtop < 10 ) {
     curtop = 10;
   }
  
  var popupDiv = document.createElement("div");
  popupDiv.setAttribute("id", "popup_div");
  popupDiv.setAttribute("name", "popup_div");
  popupDiv.style.backgroundColor = "#ffffff";
  popupDiv.style.position = "absolute";
  popupDiv.style.width = pixelWidth + "px";
  popupDiv.style.height = height + "px";
  popupDiv.style.left = curleft + "px";
  popupDiv.style.top = curtop + "px";
  popupDiv.style.border = "1px solid #CCCCCC";
  
  var titleSpan = document.createElement("h4");
  titleSpan.style.fontFamily = 'Arial, Helvetica, sans-serif';
  titleSpan.style.fontSize = '9px';
  titleSpan.style.fontWeight = 'bold';
  titleSpan.style.position = "relative";
  titleSpan.style.backgroundColor = "#ffffff";
  titleSpan.style.height = "12px";
  titleSpan.style.width = (pixelWidth - 5) +"px";
  titleSpan.style.verticalAlign = "middle";
  titleSpan.style.margin = "0";
  titleSpan.style.paddingLeft = "5px";
  titleSpan.style.paddingTop = "2px";
  titleSpan.style.color = "#000000";
  titleText = document.createTextNode(title);
  titleSpan.appendChild(titleText);
  
  var closeSpan = document.createElement("span");
  closeSpan.style.position = "absolute";
  closeSpan.style.right = "3px";
  //closeSpan.style.top = "0px";
  closeSpan.style.cursor = "pointer";
  closeSpan.style.border = "none";
  titleSpan.style.lineHeight = "14px";
  closeSpan.style.verticalAlign = "bottom";
  closeSpan.onclick = function(){
   document.body.removeChild(popupDiv);
  }
  
  var closeImg = document.createElement("img");
  closeImg.setAttribute("src", "./img/global/search_box_close.jpg");
  closeImg.setAttribute("alt", "");
  closeImg.style.marginTop = "1px";
  closeImg.style.width = "9px";
  closeImg.style.height = "9px";
  closeImg.style.verticalAlign = "middle";
  
  closeSpan.appendChild(closeImg);
  titleSpan.appendChild(closeSpan);
  popupDiv.appendChild(titleSpan);
  
  var object_height = (height - 15) + "px";
  
  if (typeof popupDiv.insertAdjacentHTML != 'undefined') {
    
    // ****** IFRAME is required in IE window.parent won't work on object! ******* //
    popupDiv.insertAdjacentHTML('beforeEnd', 
                                ['<iframe id="obj_results" frameborder="0" src="' + url + '"', 
                                 ' width="100%" height="' + object_height + '">', 
                                 '<\/iframe>'  
                                ].join('\r\n'));
  } else {
   var frame_object = frame_object = document.createElement("object");
   frame_object.setAttribute("data", url);
   frame_object.setAttribute("type", "text/html");
   frame_object.style.width = "100%";
   frame_object.style.height = object_height;
   popupDiv.appendChild(frame_object);
  }
  
  document.body.appendChild(popupDiv);
  
  document.onclick = function monitorClick(e){
        var evt = (e)?e:event;
       
        var clicked_element = (evt.srcElement)?evt.srcElement:evt.target;

        if ( clicked_element != popupDiv && clicked_element != titleSpan && clicked_element != sender ){
         if ( document.getElementById("popup_div") ) {
          document.body.removeChild(popupDiv);
         }
        }
        return true;
      }
      
  if ( center ) {  
    $j(window).resize(function(eventObject){
        var newWidth = $j(window).width();
        var newHeight = $j(window).height();
        var newPixelWidth = width;
        
        if ( width.toString().indexOf('%') > -1 ) {
          newPixelWidth = newWidth * (parseFloat(width) / 100);
          document.getElementById("popup_div").style.width = newPixelWidth + "px";
          titleSpan.style.width = (newPixelWidth - 5) +"px";
        }
      
        var new_left = (newWidth/2) - (newPixelWidth/2);
        if (new_left < 10) {
         new_left = 10;
        }
        var new_top = (newHeight/2) - (height/2);
        if (new_top < 10) {
         new_top = 10;
        }
        popupDiv.style.left = new_left + "px";
        popupDiv.style.top = new_top + $j(window).scrollTop() + "px";
      
    });
    
    $j(window).scroll(function ( ev ) { 
      var newHeight = $j(window).height();
      var new_top = (newHeight/2) - (height/2);
      if (new_top < 10) {
       new_top = 10;
      }
      popupDiv.style.top = new_top + $j(window).scrollTop() + "px";
    });
  }
  

      
  return false;
}

function Search_Keyword ( keyword ) {
  document.getElementById('search').value = keyword;
  document.forms['search_coaching'].submit();
}
