var detectMacXFF;
var userAgent = navigator.userAgent.toLowerCase();
var dontCloseAlert = false;
var fProgress = "off";
if (userAgent.indexOf('firefox')!=-1) {
   detectMacXFF =  true; 
} else {
 detectMacXFF = false;
}

function showTableRow(idName)
{
 if (detectMacXFF) {
  displayWay = "table-row";
 } else {
  displayWay = "block";
 }
 showMeFull(idName,displayWay);
}


function isChecked(idName) {
	if (checkID(idName)) {
		if (document.getElementById(idName).checked) {
			return true;
		} else {
			return false;
		}
	}
}

function getClassName(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).className;
	}
}

function getChecked(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).checked;
	}
}

function matchArray(val,arr1,arr2) {
	var output = "";
	for (a = 0; a < arr1.length; a++) {
		if (arr1[a] == val) {
			output = arr2[a];
		}
	}
	return output;
}


function enableMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).disabled=false;
	}
}


function disableMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).disabled=true;
	}
}

function checkMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).checked = true;
	}
}

function uncheckMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).checked = false;
	}
}

// checks existence of ID in markup
function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

// focuses on element
function focusMe(idName) {
	if (checkID(idName)){
		document.getElementById(idName).focus();
	}
}

function srcMe(idName,iSrc) {
	 if (checkID(idName)) {
	 	if (iSrc == "") {
			hideMe(idName);
		} else {
		 	document.getElementById(idName).src=iSrc;
		}
	}
}

// submit an element
function submitMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).submit();
	}
}

// submit an element
function resetMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).reset();
	}
}

// clears an element
function clearMe(t,val) {
	if (t.value==val) {
		t.value = '';
	}
}

function valueMe(idName,val) {
	if (checkID(idName)){
		document.getElementById(idName).value=val;
	}
}

// shows a html block
function visibleMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.visibility="visible";
	}
}

// shows a html block
function invisibleMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.visibility="hidden";
	}
}


// shows a html block
function showMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="block";
	}
}


// shows a html block
function showMeFull(idName,displayWay) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display=displayWay;
	}
}

// shows a html block
function showMe2(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}



// gives INNERHTML to an element
function htmlMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = h;
	}
}


// shows a html block
function getHTML(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).innerHTML;
	}
}

// shows a html block
function getValue(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).value;
	}
}


// shows a html block
function slipMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}

// hides a html block
function hideMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="none";
	}
}

// changes a stylesheet class to a specified ID
function changeClass(idName,toClass) {
	if (checkID(idName)) {
		document.getElementById(idName).className=toClass;
	}
}

// changes a stylesheet class to a specified ID
function changeStyle(idName,styleType,styleValue) {
	var pxTypes = "|paddingtop|paddingleft|paddingright|paddingbottom|margintop|marginleft|marginbottom|marginright|right|top|left|bottom|width|height|offsetWidth|offsetHeight|scrollWidth|scrollHeight|";
	var findStyle = "|"+styleType.toLowerCase()+"|";
	if (checkID(idName)) {
		if (pxTypes.indexOf(findStyle) >=0) {
			eval("document.getElementById(idName).style."+styleType+"='"+styleValue+"px'");
		} else {
			eval("document.getElementById(idName).style."+styleType+"='"+styleValue+"'");
		}
	}
}


function goHere(u) {
	window.location.href=u;
}



function Popup(myUrl,popType) {
	var winName='flames';
	var winTop=50;
	var winLeft=100;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();
	switch (popType) {
		case 1:   // mailing list
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=520,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 2:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 3:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 4:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 5:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=300,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
	}
	pop.focus();
}



function isEmail(email) {
	invalidChars = " /:,;";
	if (email == "") {
		return false;
	}
	// checks for bad characters
	for (i = 0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) != -1) {
			return false;
		}
	}
	//finds position of @ symbol
	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos + 1) != -1) {
		return false;
	}
	//finds position of . in email
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	atSplit = email.split("@");
	beforeAt = atSplit[0];
	afterAt = atSplit[1];
	if (atSplit[0].length == 1) { 
		return false;
	}
	periodSplit = atSplit[1].split(".");
	beforePeriod = periodSplit[0];
	if (beforePeriod.length <=1) {
		return false;
	}
	return true;
}
	
function adjustHeight() {	
	var lHeight, cHeight;
	if (checkID("leftNav") && checkID("contentContainer") && checkID("leftFruit")) {
		lHeight = document.getElementById("leftNav").offsetHeight;
		if (checkID("contentContainer")) {
			cHeight = document.getElementById("contentContainer").offsetHeight;
		}
		if (cHeight < lHeight) {
			document.getElementById("contentContainer").style.height = lHeight+"px";
		} else {
			document.getElementById("leftNav").style.height = cHeight+"px";
		}
		if (cHeight > 644 && !detectMacXFF) {
			document.getElementById("leftFruit").style.bottom = "-1px";
		}
	}
}



function styleMe(idName,styleType) {
    addTag(idName, "<"+styleType+">", "</"+styleType+">", "");
}



function showSub(thisName) {
	var step = "";
	if (thisName=="contact") {
		step = " last";
	}
	clearTop();
	changeClass("top_"+thisName,"on"+step);
}
function hideSub(thisName,curName) {
	var step = "";
	if (thisName=="contact") {
		step = " last";
	}
	changeClass("top_"+thisName,step);
}

function clearTop() {
	uObj = document.getElementById("topnav").getElementsByTagName("li");
	for (u = 0; u < uObj.length; u++) {
		if (u == uObj.length - 1) {
			uObj[u].className="last";
		} else {
			uObj[u].className="";
		}
	}
}

function getOffH(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetHeight;
	} else {
		return 0;
	}
}

function getOffW(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetWidth;
	} else {
		return 0;
	}
}

function setHeight(idName,h) {
	if (checkID(idName)) {
			document.getElementById(idName).style.height = (h)+"px";
	}
}		

function setWidth(idName,w) {
	if (checkID(idName)) {
			document.getElementById(idName).style.width = (w)+"px";
	}
}

function hiddenFrame(hub) {
	var curHeight = getOffH(hub);
	var curWidth = getOffW(hub);
	setHeight("hiddenFrame_"+hub,curHeight-2);
	setWidth("hiddenFrame_"+hub,curWidth);
	showMe("hiddenFrame_"+hub);
}
		
function leftHighlight() {
	if (checkID("leftHighlight")) {
		htmlMe("leftTarget",getHTML("leftHighlight"));
	}
}

function validateEmail(addr) {
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   return false;
}
if (atPos == 0) {
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   return false;
}
if (addr.indexOf('.@',0) != -1){
   return false;
}
if (addr.indexOf('..',0) != -1) {
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   return false;
}
return true;
}

function isNumeric(str) {
	var good = "1234567890."
	var count = 0;
	for (s = 0; s < str.length; s++) {
		if (good.indexOf(str.charAt(s)) < 0) {
			count++;
			break;
		}
	}
	if (count > 0) {
		return false;
	} else {
		return true;
	}
}

// needs to be in A1A 1A1 format
function checkPostal(pc,boxNum) {
	var numbers = "1234567890";
	var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var isGood, char1, char2, char3;
	if (pc.length != 3) {
		isGood = false;
	} else {
		char1 = pc.charAt(0);
		char2 = pc.charAt(1);
		char3 = pc.charAt(2);
		if (boxNum == 1) { // A1A format
			if ((letters.indexOf(char1) >=0) && (numbers.indexOf(char2) >=0) && (letters.indexOf(char3) >=0)) {
				isGood = true;
			} else {
				isGood = false;
			}
		} else if (boxNum == 2) { // 1A1 format
			if ((numbers.indexOf(char1) >=0) && (letters.indexOf(char2) >=0) && (numbers.indexOf(char3) >=0)) {
				isGood = true;
			} else {
				isGood = false;
			}
		} else {
			isGood = false;
		}
	}
	if (isGood) {
		return true;
	} else {
		return false;
	}		
}

function focusName(idName) {
	if (idName != "") {
		focusMe(idName);
	}
}

function selectUp(idName)
{
	var obj = document.getElementById(idName);
  var current;
  var reverse;
  if(obj.options[obj.options.selectedIndex].index > 0)
  {
    current = obj.options[obj.options.selectedIndex].text;
    reverse = obj.options[obj.options[obj.options.selectedIndex].index-1].text;
    currentValue = obj.options[obj.options.selectedIndex].value;
    reverseValue = obj.options[obj.options[obj.options.selectedIndex].index-1].value;
    obj.options[obj.options.selectedIndex].text = reverse;
    obj.options[obj.options[obj.options.selectedIndex].index-1].text = current;
    obj.options[obj.options.selectedIndex].value = reverseValue;
    obj.options[obj.options[obj.options.selectedIndex].index-1].value = currentValue;
    self.focus();
    obj.options.selectedIndex--;
  }
}

function selectDown(idName)
{
	var obj = document.getElementById(idName);
  var current;
  var next;
  if(obj.options[obj.options.selectedIndex].index != obj.length-1)
  {
    current = obj.options[obj.options.selectedIndex].text;
    next = obj.options[obj.options[obj.options.selectedIndex].index+1].text;
    currentValue = obj.options[obj.options.selectedIndex].value;
    nextValue = obj.options[obj.options[obj.options.selectedIndex].index+1].value;
    obj.options[obj.options.selectedIndex].text =  next;
    obj.options[obj.options[obj.options.selectedIndex].index+1].text = current;
    obj.options[obj.options.selectedIndex].value =  nextValue;
    obj.options[obj.options[obj.options.selectedIndex].index+1].value = currentValue;
    self.focus();
    obj.options.selectedIndex++;
  }
}

function updateOrders() {
	valueMe("theexecOrder",getHTML("execOrder"));
	valueMe("thecontactOrder",getHTML("contactOrder"));
	submitMe("theOrderForm");
}


// this returns the offset Height of a block, we add an extra margin where applicable	
function getHeight(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetHeight;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function getWidth(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetWidth;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function setHeight(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).style.height = h+"px";
	}
}


// this returns the offset Height of a block, we add an extra margin where applicable	
function setWidth(idName,w) {
	if (checkID(idName)) {
		document.getElementById(idName).style.width = w+"px";
	}
}

function getTags(idName,tagName) {
	var tagArray = new Array();
	if (checkID(idName)) {
		return document.getElementById(idName).getElementsByTagName(tagName);
	} else {
		return tagArray;
	}
}


function adjustNav()  {
	var pObj = getTags("topnavLinks","p");
	for (p = 1; p < pObj.length; p++) {
		if (pObj[p].className == "on") {
			pObj[p-1].className = "prevOn";
		} else {
			//pObj[p-1].className = "";
		}
	}
}	
						

						
function testTranslate() {
	if (getHTML("google_translate_element").indexOf("Powered") >=0) {
		valueMe("googleTransComplete","true");
		clearInterval("testTranslate()");
		fixTranslate();
		visibleMe("google_translate_element");
	}
}
function fixTranslate() {
	var divObj = getTags("google_translate_element","div");
	divObj[0].className = "transContainerLeft";
	divObj[1].className = "transLeft";
}
										
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en'
  }, 'google_translate_element');
}									

function googleTranslateElementInitFr() {
  new google.translate.TranslateElement({
    pageLanguage: 'fr'
  }, 'google_translate_element');
}									

function checkKeyPress(e)
{
	if(!e)e = window.event;
	var key = (typeof e.which == 'number')?e.which:e.keyCode;
	if(key == 13) {
	search();
	return (false);}
}

function search(){
window.location='/'+ document.location.pathname.substr(1,2) +'/Search/Pages/default.aspx?k=' + document.getElementById('searchbx').value;
}

						
function fixGallery() {
	setInterval("fixArrowStart()",10);
	var divObj = getTags("galleryContainer","div");
	for (d = 3; d < divObj.length; d++) {
		if (divObj[d].className.indexOf("galleryBlockOn") >=0) {
			divObj[d-1].className = "galleryBlock galleryBlockLeft";
			break;
		}
	}
}

function fixArrowStart(){
	if (getValue("currImg") != undefined){
		clearInterval("fixArrowStart()");
		fixArrowVis();
	}
}

function fixArrowVis(){
	if (parseInt(getValue("currImg")) == 0){
		invisibleMe("leftarrow");
	}
	else if (imagesArray [parseInt(getValue("currImg"))-1] [0] != imagesArray [parseInt(getValue("currImg"))] [0]){
 		invisibleMe("leftarrow");
	}
	else{
		visibleMe("leftarrow");
	}

	if (parseInt(getValue("currImg")) == imagesArray.length-1){
		invisibleMe("rightarrow");
	}
	else if (imagesArray [parseInt(getValue("currImg"))+1] [0] != imagesArray [parseInt(getValue("currImg"))] [0]){
	 	invisibleMe("rightarrow");
	}
	else{
		visibleMe("rightarrow");
	}	
}

function adjustGallery(numGalleries,maxShown) {
	for (g = 0; g <= numGalleries; g++) {
		var counter = 0;
		var divObj = getTags("galleryShowcaseContainer"+g,"div");
		for (d = 0; d < divObj.length; d++) {
			if (divObj[d].className=="galleryItem") {
				counter++;
			}
			if (counter > maxShown) {
				visibleMe("galleryNext"+g);
			}
		}
	}
	fixGallery();
}

function changeGalleryTab(gID) {
	for (g = 0; g <= 3; g++) {
		if (g == 3) {	
			changeClass("galleryTab"+g,"galleryBlock galleryLast");
		} else {
			changeClass("galleryTab"+g,"galleryBlock");
		}
		hideMe("galleryShow"+g);
	}
	if (gID == 3) {	
		changeClass("galleryTab"+gID,"galleryBlock galleryBlockOn galleryLast");
	} else {
		changeClass("galleryTab"+gID,"galleryBlock galleryBlockOn");
	}
	fixGallery();
	showMe("galleryShow"+gID);
	valueMe("currTab",gID);
	
	getFirstImageInSet(gID);
	
	fixArrowVis();
	
}

function getFirstImageInSet(gID){
	for (var i = 0; i < imagesArray.length; i++){
		if (imagesArray[i] [0] == gID){
			changeImage(i, imagesArray[i][1], imagesArray[i][2]);
			break;
		}
	}
}

function moveGalleryRight(gID) {
	var curPos = parseInt(getValue("galleryPosition"+gID));
	var startPos = parseInt(getValue("galleryStart"+gID));
	var endPos = parseInt(getValue("galleryEnd"+gID));
	var maxPos = parseInt(getValue("galleryMax"+gID));
	valueMe("galleryStart"+gID,startPos+1);
	valueMe("galleryEnd"+gID,endPos+1);
	curPos = curPos - 95;
	valueMe("galleryPosition"+gID,curPos);
	changeStyle("galleryItems"+gID,"left",curPos);
	visibleMe("galleryPrev"+gID);
	if (endPos+1 >= maxPos) {
		invisibleMe("galleryNext"+gID);
	}
}
function moveGalleryLeft(gID) {
	var curPos = parseInt(getValue("galleryPosition"+gID));
	var startPos = parseInt(getValue("galleryStart"+gID));
	var endPos = parseInt(getValue("galleryEnd"+gID));
	var maxPos = parseInt(getValue("galleryMax"+gID));
	valueMe("galleryStart"+gID,startPos-1);
	valueMe("galleryEnd"+gID,endPos-1);
	curPos = curPos + 95;
	valueMe("galleryPosition"+gID,curPos);
	changeStyle("galleryItems"+gID,"left",curPos);
	visibleMe("galleryNext"+gID);
	if (startPos == 1) {
		invisibleMe("galleryPrev"+gID);
	}
}				

function changeImage(ID,Url,Descr){
	srcMe("GalleryImageViewer", Url);
	htmlMe("GalleryImageCaption", Descr);
	valueMe("currImg",ID);
	fixArrowVis();
}

function moveLeft(){
 	visibleMe("rightarrow");
	changeImage(parseInt(getValue("currImg"))-1, imagesArray [parseInt(getValue("currImg"))-1] [1],imagesArray [parseInt(getValue("currImg"))-1] [2]);
}

function moveRight(){
	visibleMe("leftarrow");
	changeImage(parseInt(getValue("currImg"))+1, imagesArray [parseInt(getValue("currImg"))+1] [1],imagesArray [parseInt(getValue("currImg"))+1] [2]);
}

function printPartOfPage(elementId)
{
 var printContent = document.getElementById(elementId);
 var printfooter = document.getElementById('PrintFooter');
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=20,top=20,width=650,height=400,resizable=yes,scrollbars=1,status=no');

printWindow.document.write("<html><head>");
printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/Style Library/en-us/Core Styles/NCBB/style.css\" />"); 
printWindow.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/Style Library/en-us/Core Styles/NCBB/print.css\" />"); 
printWindow.document.write("</head><body><div class=\"container\"><div class=\"mainContainer\"><div class=\"header\"><div class=\"logo\"><a href=\"/\"><img src=\"/style library/images/ncbb/logo.gif\" width=\"250\" height=\"150\" alt=\"\" /></a></div></div>");

printWindow.document.write(printContent.innerHTML + printfooter.innerHTML + "<br/><br/><br/><div class=\"footerCopyright\"><div><a href=\"http://www.ontarioimmigration.ca/en/index.htm\" title=\"Ontario Immigration\"><img src=\"/sitecollectionimages/footerOntario.gif\" width=\"130\" height=\"56\" alt=\"Ontario Immigration\" /></a><a href=\"http://www.cic.gc.ca/english/index.asp\" title=\"Citizenship and Immigration Canada\"><img src=\"/sitecollectionimages/footerImmi.gif\" width=\"260\" height=\"56\" alt=\"Citizebship and Immigration Canada\" /></a></div></div></body></html>");
//printWindow.document.write(printfooter.innerHTML);
//printWindow.document.write("<br/><br/><br/><div class=\"footerCopyright\"><div><a href=\"http://www.ontarioimmigration.ca/en/index.htm\" title=\"Ontario Immigration\"><img src=\"/sitecollectionimages/footerOntario.gif\" width=\"130\" height=\"56\" alt=\"Ontario Immigration\" /></a><a href=\"http://www.cic.gc.ca/english/index.asp\" title=\"Citizenship and Immigration Canada\"><img src=\"/sitecollectionimages/footerImmi.gif\" width=\"260\" height=\"56\" alt=\"Citizebship and Immigration Canada\" /></a></div></div></body></html>");

 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
}

function resize(size,idName)
{
if (checkID(idName)) {
document.getElementById(idName).style.fontSize=size;
}
return false;
}


function testTranslateMS() {
	if (getHTML("MicrosoftTranslatorWidget").indexOf("DXImageTransform") >=0) {
		valueMe("MStransComplete","true");
		clearInterval("testTranslateMS()");
		fixTranslateMS();
	}
}
function fixTranslateMS() {
var widget = document.getElementById('MicrosoftTranslatorWidget');
widget.firstChild.style.display = "none";
}

