var HOST = "http://postalhistorycanada.net/php/";
function loadUpSubMenu() { //loads submenu from hidden div
	var hiddenString = document.getElementById('hiddenmenu').innerHTML;
	document.getElementById('submenu').innerHTML=hiddenString;
	}
function loadUpSideBar() { //loads sidebar from hidden div
	var hiddenString = document.getElementById('hiddensidebar').innerHTML;
	document.getElementById('sidebar').innerHTML=hiddenString;
	document.getElementById('hiddensidebar').innerHTML="";
	}
function getCookie(c_name)
{
//this is here just for the use of setProvinceSelect()
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name+"=");
  alert('cookies exist '+c_start);
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
	}
function setProvinceSelect() {
// this doesn't work, as the getCookie function can't seem to find cookies
	prov = getCookie('Imcrsc_lastsrch[province]');
	alert(prov);
	if (prov!=null && prov!="" && prov!="undefined")
		{
		document.getElementById('prov_srch').options[getElementById('prov_srch').selectedIndex].value = prov;
		}
	}
function setCountyDisplayed() {
	var provSelect=document.getElementById('prov_srch');
	if(provSelect.options[provSelect.selectedIndex].value=="ON"){
		document.getElementById('county_select_ON').style.display="block";}
	else {document.getElementById('county_select_ON').style.display="none";}
	if(provSelect.options[provSelect.selectedIndex].value=="NB"){
		document.getElementById('county_select_NB').style.display="block";}
	else {document.getElementById('county_select_NB').style.display="none";}
	if(provSelect.options[provSelect.selectedIndex].value=="NS"){
		document.getElementById('county_select_NS').style.display="block";}
	else {document.getElementById('county_select_NS').style.display="none";}
	if(provSelect.options[provSelect.selectedIndex].value=="PQ"){
		document.getElementById('county_select_PQ').style.display="block";}
	else {document.getElementById('county_select_PQ').style.display="none";}
		
		
	}
function loadDuplexSearchResults(urlString) { //pass the inputs to the search page
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'table=Duplex&';
	if(document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value!='-'){
	urlString = urlString + 'search_province='+document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value + '&';
	}
	if((document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value=='ON')&&(document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value!='all')){
		urlString = urlString + 'search_county='+document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value + '&';
		
		}
	if(document.getElementById('comm_srch').value!=''){
		urlString = urlString + 'search_community='+document.getElementById('comm_srch').value + '&';
	}	

	urlString = urlString + 'field_county='+document.getElementById('county').checked + '&';
	urlString = urlString + 'field_dater='+document.getElementById('dater').checked + '&';
	urlString = urlString + 'field_killer='+document.getElementById('killer').checked + '&';
	urlString = urlString + 'field_Diameter='+document.getElementById('Diameter').checked + '&';
	urlString = urlString + 'field_proof_date='+document.getElementById('proof_date').checked + '&';
	urlString = urlString + 'field_eku='+document.getElementById('eku').checked + '&';
	urlString = urlString + 'field_lku='+document.getElementById('lku').checked + '&';
	urlString = urlString + 'field_time_marks='+document.getElementById('time_marks').checked + '&';
	urlString = urlString + 'field_notes='+document.getElementById('notes').checked + '&';

	makeRequest(urlString);
	closeRightMenu();
	}
function loadBrokenCirclesSearchResults(urlString) { //pass the inputs to the search page
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'table=BrokenCircles&';
	if(document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value!='-'){
	urlString = urlString + 'search_province='+document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value + '&';
	}
	if((document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value=='ON')&&(document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value!='all')){
		urlString = urlString + 'search_county='+document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value + '&';
		
		}
	if(document.getElementById('comm_srch').value!=''){
		
		urlString = urlString + 'search_community='+document.getElementById('comm_srch').value + '&';
	}	
	if(document.getElementById('type_srch').value!='-'){
		urlString = urlString + 'search_Type='+document.getElementById('type_srch').value + '&';
	}	
	urlString = urlString + 'user='+document.getElementById('user').value + '&';

	urlString = urlString + 'field_county='+document.getElementById('county').checked + '&';
	urlString = urlString + 'field_province='+document.getElementById('province').checked + '&';
	urlString = urlString + 'field_town_id='+document.getElementById('town_id').checked + '&';
	urlString = urlString + 'field_Type='+document.getElementById('Type').checked + '&';
	urlString = urlString + 'field_Diameter='+document.getElementById('Diameter').checked + '&';
	urlString = urlString + 'field_Left_arc='+document.getElementById('Left_arc').checked + '&';
	urlString = urlString + 'field_Right_arc='+document.getElementById('Right_arc').checked + '&';
	urlString = urlString + 'field_proof_text='+document.getElementById('proof_text').checked + '&';
	urlString = urlString + 'field_eku_text='+document.getElementById('eku_text').checked + '&';
	urlString = urlString + 'field_lku_text='+document.getElementById('lku_text').checked + '&';
	urlString = urlString + 'field_PO_open='+document.getElementById('PO_open').checked + '&';
	urlString = urlString + 'field_PO_closed='+document.getElementById('PO_closed').checked + '&';
	urlString = urlString + 'field_notes='+document.getElementById('notes').checked + '&';

	makeRequest(urlString);
	closeRightMenu();
	}
function loadCdsSearchResults(urlString) { //pass the inputs to the search page
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'table=cds&';
	if(document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value!='-'){
	urlString = urlString + 'search_province='+document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value + '&';
	}
	if((document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value=='ON')&&(document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value!='all')){
		urlString = urlString + 'search_county='+document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value + '&';
		
		}
	if(document.getElementById('comm_srch').value!=''){
		urlString = urlString + 'search_community='+document.getElementById('comm_srch').value + '&';
	}	

	urlString = urlString + 'field_county='+document.getElementById('county').checked + '&';
	urlString = urlString + 'field_province='+document.getElementById('province').checked + '&';
	urlString = urlString + 'field_town_id='+document.getElementById('town_id').checked + '&';
	urlString = urlString + 'field_Type='+document.getElementById('Type').checked + '&';
	urlString = urlString + 'field_Diameter='+document.getElementById('Diameter').checked + '&';
	urlString = urlString + 'field_proof_text='+document.getElementById('proof_text').checked + '&';
	urlString = urlString + 'field_eku_date='+document.getElementById('eku_date').checked + '&';
	urlString = urlString + 'field_lku_date='+document.getElementById('lku_date').checked + '&';
	urlString = urlString + 'field_notes='+document.getElementById('notes').checked + '&';

	makeRequest(urlString);
	closeRightMenu();
	}

function loadSlogansSearchResults(urlString) { //pass the inputs to the search page
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'table=Slogans&';
	if(document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value!='-'){
	urlString = urlString + 'search_province='+document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value + '&';
	}
	if(document.getElementById('comm_srch').value!=''){
		urlString = urlString + 'search_town='+document.getElementById('comm_srch').value + '&';
	}	
	if(document.getElementById('type_srch').value!=''){
		urlString = urlString + 'search_machine_Co='+document.getElementById('type_srch').value + '&';
	}	
	if(document.getElementById('coutts_srch').value!=''){
		urlString = urlString + 'search_Coutts_no='+document.getElementById('coutts_srch').value + '&';
	}	
	if(document.getElementById('keyword_srch').value!=''){
		urlString = urlString + 'search_inscription='+document.getElementById('keyword_srch').value + '&';
	}	

/*	urlString = urlString + 'field_town='+document.getElementById('town').checked + '&';
	urlString = urlString + 'field_province='+document.getElementById('province').checked + '&';
	urlString = urlString + 'field_machine_Co='+document.getElementById('machine_Co').checked + '&';
	urlString = urlString + 'field_Coutts_no='+document.getElementById('Coutts_no').checked + '&';
	urlString = urlString + 'field_die='+document.getElementById('die').checked + '&';
	urlString = urlString + 'field_inscription='+document.getElementById('inscription').checked + '&';
	urlString = urlString + 'field_year='+document.getElementById('proof_year').checked + '&';
	urlString = urlString + 'field_eku='+document.getElementById('eku').checked + '&';
	urlString = urlString + 'field_lku='+document.getElementById('lku').checked + '&';
	urlString = urlString + 'field_notes='+document.getElementById('notes').checked + '&';
*/
	urlString = urlString + 'field_town=true' + '&';
	urlString = urlString + 'field_province=true' + '&';
	urlString = urlString + 'field_machine_Co=true' + '&';
	urlString = urlString + 'field_Coutts_no=true' + '&';
	urlString = urlString + 'field_die=true' + '&';
	urlString = urlString + 'field_inscription=true' + '&';
	urlString = urlString + 'field_year=true' + '&';
	urlString = urlString + 'field_eku=true' + '&';
	urlString = urlString + 'field_lku=true' + '&';
	urlString = urlString + 'field_notes=true' + '&';

	makeRequest(urlString);
	closeRightMenu();
	}

	
function loadPOSearchResults(urlString) { //pass the inputs to the search page
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'table=PostOffices&';
	if(document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value!='-'){
		urlString = urlString + 'search_province='+document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value + '&';
		}
	if((document.getElementById('prov_srch').options[document.getElementById('prov_srch').selectedIndex].value=='ON')
		&&(document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value!='all')){
		urlString = urlString + 'search_county='+document.getElementById('cty_srch').options[document.getElementById('cty_srch').selectedIndex].value + '&';
		}
	if(document.getElementById('comm_srch').value!=''){
		urlString = urlString + 'search_community='+document.getElementById('comm_srch').value + '&';
	}	
//	urlString = urlString + 'user='+document.getElementById('user').value + '&';

	urlString = urlString + 'field_county='+document.getElementById('county').checked + '&';
	urlString = urlString + 'field_province='+document.getElementById('province').checked + '&';
	urlString = urlString + 'field_township='+document.getElementById('township').checked + '&';
	urlString = urlString + 'field_po_id='+document.getElementById('po_id').checked + '&';
	urlString = urlString + 'field_moon_na='+document.getElementById('moon_na').checked + '&';
	urlString = urlString + 'field_moon_a='+document.getElementById('moon_a').checked + '&';
	urlString = urlString + 'field_rc_corp='+document.getElementById('rc_corp').checked + '&';
	urlString = urlString + 'field_rc_fran='+document.getElementById('rc_fran').checked + '&';
	urlString = urlString + 'field_open_text='+document.getElementById('open_text').checked + '&';
	urlString = urlString + 'field_close_text='+document.getElementById('close_text').checked + '&';
	urlString = urlString + 'field_postal_code='+document.getElementById('postal_code').checked + '&';
	urlString = urlString + 'field_eku='+document.getElementById('eku').checked + '&';
	urlString = urlString + 'field_lku='+document.getElementById('lku').checked + '&';
	urlString = urlString + 'field_name_before='+document.getElementById('name_before').checked + '&';
	urlString = urlString + 'field_name_after='+document.getElementById('name_after').checked + '&';
	urlString = urlString + 'field_notes='+document.getElementById('notes').checked;

	makeRequest(urlString);
	closeRightMenu();
	}
	
	
	
	function testFunc(str){
	alert(str);
	}
	
	
	
function loadRatesSearchResults(urlString) { //pass the inputs to the search page 
	urlString = urlString + '?submit=Submit&';
	if(document.getElementById('rate_list').options[document.getElementById('rate_list').selectedIndex].value!='-'){
	urlString = urlString + 'search_list='+document.getElementById('rate_list').options[document.getElementById('rate_list').selectedIndex].value;
	}
	makeRequest(urlString);
//	closeRightMenu();
	}
	
	
function loadLibrarySearchResults(urlString) { //pass the inputs to the search page 
	urlString = urlString + '?submit=Submit&';
	urlString = urlString + 'db=ph_index&'; // NB: library index is in db ph_index
	urlString = urlString + 'table=library_index&';
	if(document.getElementById('whole_srch').value!=''){
		urlString = urlString + 'search_wholeno='+document.getElementById('whole_srch').value + '&';
	}	
	if(document.getElementById('key_srch').value!=''){
		urlString = urlString + 'search_keyword='+document.getElementById('key_srch').value + '&';
	}	
	if(document.getElementById('auth_srch').value!=''){
		urlString = urlString + 'search_author='+document.getElementById('auth_srch').value + '&';
	}	
	makeRequest(urlString);
	}

function loadNortherner() {//issue should be Northerner Issue #
	if(document.getElementById('issue').value!=''){
		location.href="Northerner/mem/Northerner_"+document.getElementById('issue').value+".pdf";
		}
	}


function loadRecentJournal(urlString,allow) {
	urlString = urlString  + '?submit=Submit&';
	urlString = urlString + 'db=ph_index&'; // NB: Journal index is in db ph_index
	urlString = urlString + 'table=phscj_urls&';
	urlString = urlString + 'search_wholeno='+document.getElementById('wholeno').value + '&allow='+allow;

	makeRequest(urlString);
	}
	
	
function checkEnter(e,table){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e;
	characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
	e = event;
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	if(table == 'Duplex') {loadDuplexSearchResults('print_search_table.php');} //submit the form 
	if(table == 'Library') {loadLibrarySearchResults('print_search_table.php');} //submit the form 
	if(table == 'Library_pub') {loadLibrarySearchResults('print_search_table_pub.php');} //submit the form 
	if(table == 'BrokenCircles') {loadBrokenCirclesSearchResults('print_search_table.php');} //submit the form 
	if(table == 'CDS') {loadCdsSearchResults('print_search_table.php');} //submit the form 
	if(table == 'PostOffices') {loadPOSearchResults('print_search_table.php');} //submit the form 
	if(table == 'Slogans') {loadSlogansSearchResults('print_search_table.php');} //submit the form 
	if(table == 'IMCRSC') {IMCRSCSearch('print_search_table.php');} //submit the form 
	}
}

var imgUrlArr=new Array();
var captionArr=new Array();
var currentIndex;


	
function leftLightBox(){
	if(currentIndex>0){
		currentIndex--;
		document.getElementById('lbcaption').innerHTML=captionArr[currentIndex];
		document.getElementById('lbimg').src=imgUrlArr[currentIndex];
	}
	if(currentIndex<=0){
		document.getElementById('lbleft').style.visibility="hidden";
	} else {
		document.getElementById('lbleft').style.visibility="visible";
	}
	if(currentIndex>=imgUrlArr.length-1){
		document.getElementById('lbright').style.visibility="hidden";
	} else {
		document.getElementById('lbright').style.visibility="visible";
	}
}

function rightLightBox(){

	if(currentIndex<imgUrlArr.length-1){
		currentIndex++;
		document.getElementById('lbcaption').innerHTML=captionArr[currentIndex];
		document.getElementById('lbimg').src=imgUrlArr[currentIndex];
	}
	if(currentIndex<=0){
		document.getElementById('lbleft').style.visibility="hidden";
	} else {
		document.getElementById('lbleft').style.visibility="visible";
	}
	if(currentIndex>=imgUrlArr.length-1){
		document.getElementById('lbright').style.visibility="hidden";
	} else {
		document.getElementById('lbright').style.visibility="visible";
	}
}	
	
function closeLightBox(){
	document.getElementById('lightbox').style.display="none";
	document.getElementById('lightboxcell').style.display="none";
	document.body.style.overflow="scroll";
	}
	

function getScrollX() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function getScrollY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function getSizeX() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;
}

function getSizeY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
function hideDiv(divIdNumber,imgNumber) { //closes a div
	document.getElementById(divIdNumber).style.display="none";
	document.getElementById(imgNumber).src=HOST+"images/divarrow_right.gif";
	}
function unHideDiv(divIdNumber,imgNumber) { //opens a div
	document.getElementById(divIdNumber).style.display="block";
	document.getElementById(imgNumber).src=HOST+"images/divarrow_down.gif";
	}
function openCloseDiv(divIdNumber,imgNumber) {

	if(document.getElementById(imgNumber).src==HOST+"images/divarrow_right.gif") {
		unHideDiv(divIdNumber,imgNumber);
	}else{
		hideDiv(divIdNumber,imgNumber);
	}
}


function insertProvString() {
	var provString = "";
	if(document.getElementById("tool-BC").checked) {
		provString = provString + "BC,";
		}
	if(document.getElementById("tool-AB").checked) {
		provString = provString + "AB,";
		}
	if(document.getElementById("tool-SK").checked) {
		provString = provString + "SK,";
		}
	if(document.getElementById("tool-MB").checked) {
		provString = provString + "MB,";
		}
	if(document.getElementById("tool-ON").checked) {
		provString = provString + "ON,";
		}
	if(document.getElementById("tool-QC").checked) {
		provString = provString + "QC,";
		}
	if(document.getElementById("tool-NL").checked) {
		provString = provString + "NL,";
		}
	if(document.getElementById("tool-NS").checked) {
		provString = provString + "NS,";
		}
	if(document.getElementById("tool-NB").checked) {
		provString = provString + "NB,";
		}
	if(document.getElementById("tool-PE").checked) {
		provString = provString + "PE,";
		}
	if(document.getElementById("tool-YT").checked) {
		provString = provString + "YT,";
		}
	if(document.getElementById("tool-NT").checked) {
		provString = provString + "NT,";
		}
	document.getElementById("province").value = provString.substring(0,provString.length-1);
}

function getProvString(provString) {
	var tempString = "";
	for(i=0; i<=provString.length/3; i++) {
		tempString = "tool-"+provString.substring(i*3,i*3+2);
		document.getElementById(tempString).checked = true;
		}
}

function insertMachString() {

	var provString = "";
	if(document.getElementById("tool-IMP").checked) {
		provString = provString + "IMP,";
		}
	if(document.getElementById("tool-B").checked) {
		provString = provString + "B,";
		}
	if(document.getElementById("tool-I").checked) {
		provString = provString + "I,";
		}
	if(document.getElementById("tool-U").checked) {
		provString = provString + "U,";
		}
	if(document.getElementById("tool-C").checked) {
		provString = provString + "C,";
		}
	if(document.getElementById("tool-P").checked) {
		provString = provString + "P,";
		}
	if(document.getElementById("tool-PB").checked) {
		provString = provString + "PB,";
		}
	if(document.getElementById("tool-T").checked) {
		provString = provString + "T,";
		}
	if(document.getElementById("tool-K").checked) {
		provString = provString + "K,";
		}
	if(document.getElementById("tool-IPS").checked) {
		provString = provString + "IPS,";
		}
	if(document.getElementById("tool-JS").checked) {
		provString = provString + "JS,";
		}
	if(document.getElementById("tool-").checked) {
		provString = provString + ",";
		}
	document.getElementById("machine_Co").value = provString.substring(0,provString.length-1);
}

function getMachString(machString) {
	var tempString = "";
		tempString = "tool-"+machString;
		document.getElementById(tempString).checked = true;
		
}
