/* GoIndustry DoveBid - /js/main.js */
	
window.onload=setListeners;


function setListeners() { 
	
	if (window.initPage) initPage();

	if(document.getElementById('toggleAll')) { 
		document.getElementById('toggleAll').onclick=function () {
			var tags=document.getElementsByTagName('input');
			var state=(document.getElementById('toggleAll').checked==true) ? true : false;
			for(i=0;i<tags.length;i++) {
				if(tags[i].className=="check") { tags[i].checked=state; }
			}
		}
	}

	if(document.getElementById('go')) { 
		document.getElementById('go').onclick = function () {
			if (document.getElementById('searchfield').value != '') {
				document.getElementById('searchForm').submit();
			}
			return false;
		}
	}

	if(document.getElementById('valsBox')) { 
		document.getElementById('valsBox').onclick = function () {
			location.href="/valuations/";
		}
	}

	if(document.getElementById('printPage')) {
		document.getElementById('printPage').onclick=function () {
			print();
			return false;}
	}

	if(document.getElementById('printPageIcon')) {
		document.getElementById('printPageIcon').onclick=function () {
			print();
			return false;}
	}

	if(document.getElementById('closeButton')) {
		document.getElementById('closeButton').onclick=function () { 
		    hideModal();
		}
	}

// Sets event listeners based on the body id:
	
	// Auction Calendar Events
	if(document.getElementById('auctionCalBody')) {
		var regDrop=document.getElementById('regions');
		regDrop.onchange = function () {
			//this.form.submit(); (to be activated in production)
			alert("Region to be changed to "+ regDrop.options[regDrop.selectedIndex].innerHTML); 
		}
		var catDrop=document.getElementById('cats');
		catDrop.onchange = function () {
			//this.form.submit(); (to be activated in production)
			alert("Category to be changed to "+ catDrop.options[catDrop.selectedIndex].innerHTML); 
		}

	// Bid Manager Events
	} else if(document.getElementById('bidMgrBody')) {
		
		document.getElementById('thumbs').onclick=function () {
			var state=(document.getElementById('thumbs').checked==true) ? "block" : "none";
			var tags=document.getElementsByTagName('*');
			for(i=0;i<tags.length;i++) {
				if(tags[i].className.indexOf("fullDet")!=-1) { tags[i].style.display=state; }
			}
			if (window.thumbs_click) thumbs_click();
		}
			
		if(document.getElementById('selectAll')) {
			document.getElementById('selectAll').onclick=function () { 
				var tags=document.getElementsByTagName('input');
				var state=(document.getElementById('selectAll').checked==true) ? true : false;
				for(i=0;i<tags.length;i++) {
					if(tags[i].className=="selectcell") { tags[i].checked=state; }
				}
			}
		}

	// Credit Card Events
	} else if(document.getElementById('creditCardBody')) {
		
		if(document.getElementById('cvvHelp')) {
			document.getElementById('cvvHelp').onmouseover=function () { 
				document.getElementById('cvvDesc').style.display="block"; }
			document.getElementById('cvvHelp').onmouseout=function () { 
				document.getElementById('cvvDesc').style.display="none"; }
		}
		
	// BoarBerg Report Events
	} else if(document.getElementById('boarBergBody')) {
		document.getElementById('expand1').onclick=function () { expand(1); }
		document.getElementById('expand2').onclick=function () { expand(2); }
		document.getElementById('expand3').onclick=function () { expand(3); }
		document.getElementById('expand4').onclick=function () { expand(4); }
		document.getElementById('expand5').onclick=function () { expand(5); }
		document.getElementById('expand6').onclick=function () { expand(6); }
		document.getElementById('expand7').onclick=function () { expand(7); }
		document.getElementById('expand8').onclick=function () { expand(8); }
		document.getElementById('expand9').onclick=function () { expand(9); }
		
	// Manage Vetted Registrants Events
	} else if(document.getElementById('vettedRegistrantsBody')) {
		document.getElementById('expand1').onclick=function () { expand(1); }
		document.getElementById('expand2').onclick=function () { expand(2); }
		document.getElementById('expand3').onclick=function () { expand(3); }
		
	// Payment Options Page
	} else if(document.getElementById('staticPaymentOptionsPage')) {
		document.getElementById('expand1').onclick=function () { expand(1); }
		document.getElementById('expand1l').onclick=function () { expand(1); }
		document.getElementById('expand2').onclick=function () { expand(2); }
		document.getElementById('expand2l').onclick=function () { expand(2); }
		document.getElementById('expand3').onclick=function () { expand(3); }
		document.getElementById('expand3l').onclick=function () { expand(3); }
		
	// Exchange Details Page
	} else if(document.getElementById('exchangesBody')) {
		if(document.getElementById('expand1')) {
			document.getElementById('expand1').onclick=function () { expand(1); return false; }
		}
	// Any other page with expand icons/links
	} else {
		if(document.getElementById('expand1')) {
			document.getElementById('expand1').onclick=function () { expand(1); }
		}
		if(document.getElementById('expand1l')) {
			document.getElementById('expand1l').onclick=function () { expand(1); }
		}

	}
}

function expand(id) {
	var imgId=document.getElementById("expand"+id);
	var divId=document.getElementById("expBdy"+id);
	if(imgId.src.indexOf("plus")!=-1) {
		imgId.src="/img/icons/minus.gif";
		divId.style.display="block";
	} else {
		imgId.src="/img/icons/plus.gif";
		divId.style.display="none";
	}
}	

function modal() {
	showModalPopup($('#modal'));
}

function showModalMask(maskId) {
    var mask = $('#'+maskId);

    if ( !(mask.length>0) ) {
        mask = $('#modalMask');
    } 
    
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	mask.css({'width':maskWidth,'height':maskHeight,'display':'block'});
}

function hideModal() {
	$('#modal').css('display','none');
	$('#modalMask').css('display','none');
}

function hideModalPopup(modalPopupId) {
	$('#' + modalPopupId).css('display','none');
	$('#' + modalPopupId + '_ModalMask').css('display','none');
}

function showModalPopup(modalPopup) {
	var top = ($(window).height() - modalPopup.height())/2;
	if (top < 15) top = 15;

	showModalMask(modalPopup.attr('id') + '_ModalMask');
	modalPopup.css("left", ($(document).width() - modalPopup.width())/2);
	modalPopup.css("top", top);
	modalPopup.css("display", "block");
}

function countryList() {
	if(document.getElementById('countries').style.display=="block") {
		document.getElementById('countries').style.display="none";
	} else {
		document.getElementById('countries').style.display="block";
	}
}
// End - above section to be removed post-development.

function setLanguage(language, tmp) {
	var url;
	var qstr;
	url = location.protocol + '//' + location.host + location.pathname + '?lcid=' + language;
	qstr = location.search;
	qstr = qstr.replace(/\?/g, '&');
	qstr = qstr.replace(/&*tlc=[^&]*/gi, '');
	url += qstr.replace(/&*lcid=[^&]*/gi, '');
	window.location = url;
	return false;
}

function ModifyCssClass(action,element,class1,class2) {
	switch (action){
		case 'swap':
		element.className = !ModifyCssClass('check',element,class1) ? element.className.replace(class2,class1) : element.className.replace(class1,class2);
		break;
		case 'add':
		if(!ModifyCssClass('check',element,class1)) { element.className+=element.className ? ' '+class1 : class1; }
		break;
		case 'remove':
		var rep=element.className.match(' '+class1) ? ' '+class1 : class1;
		element.className=element.className.replace(rep,'');
		break;
		case 'check':
		return new RegExp('\\b'+class1+'\\b').test(element.className);
		break;
	}
}

function stopEventPropagation(ev) {
	if (ev) {
		if (ev.stopPropagation)
			ev.stopPropagation();
		else
			ev.cancelBubble=true;
	}
}