/*** layer visibility functions for top menu****/

var bgColorOut = '';
var bgColorOver = '#f0f0f0';

function showHideMenu(id, mode) {
	showHideLayers('div' + id, '', mode);
	changeBg(id, mode);
}

function changeBg(id, mode) {
	var td = document.getElementById('td' + id)
	td.style.backgroundColor = (mode == 'hide' ? bgColorOut : bgColorOver);
}

function findObj(n, d) {
	
	var p, i, x;
	
	if(!d)
		d=document;
		
	if((p=n.indexOf("?"))>0 && parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	
	if(!(x=d[n]) && d.all)
		x=d.all[n];
	
	for (i=0;!x && i<d.forms.length;i++)
		x=d.forms[i][n];
		
	for(i=0;!x && d.layers && i<d.layers.length;i++) 
		x = findObj(n, d.layers[i].document);
		
	if(!x && d.getElementById) 
		x = d.getElementById(n); 
		
	return x;
}

function showHideLayers() {

	var i, p, v, obj, args = showHideLayers.arguments;
	
	for (i=0; i<(args.length-2); i+=3) {
		if ((obj = findObj(args[i]))!=null) { 
			v=args[i+2];
			
			if (obj.style) { 
				obj = obj.style; 
				v = (v=='show') ? 'visible' : (v=='hide') ? 'hidden' : v; 
			}
			obj.visibility=v; 
		}
	}
}

/*** toggle functions for message ****/
function togMessage(divId) {

	var div = new Array();
	div[0] = 'divHidden' + divId;
	div[1] = 'divVisible' + divId;

	for(var i=2;;i++){
		var thisDiv = 'divText' + divId + '-' + i;

		if(document.getElementById(thisDiv))
			div[i] = thisDiv
		else
			break;
	}

	return tog('block', div);
}

function tog(display, div) {

  for(var i=0; i<div.length; i++) {

    var thisDiv = document.getElementById(div[i]);

    if(!thisDiv)
		continue;

    if(thisDiv.style.display == "none" || thisDiv.style.display == "") {
      thisDiv.style.display = display;
    }
    else {
      thisDiv.style.display = "none";
    }
  }
}

/*** board functions ****/
function boardGoTo(behavior, initialBehavior, boardName, boardId, commentId, boardPage, criteria, keyword, defaultPage, sectionId, goToPage) {
	submitBoardForm(behavior, initialBehavior, boardName, boardId, commentId, goToPage, criteria, keyword, defaultPage, sectionId);
}

function boardView(behavior, boardName) {
	submitBoardForm(behavior, '', boardName, '', '', '', '', '');
}

function boardSearchReset() {
	document.form.boardCriteria.value = '';
	document.form.boardKeyword.value = '';
}

function setBoardValues(behavior, initialBehavior, boardName, boardId, commentId, boardPage, criteria, keyword) {

	document.form.behavior.value = behavior;
	document.form.boardName.value = boardName;

	if(document.getElementById('initialBehavior'))
		document.form.initialBehavior.value = initialBehavior;

	if(document.getElementById('boardId'))
		document.form.boardId.value = boardId;

	if(document.getElementById('commentId'))
		document.form.commentId.value = commentId;

	if(document.getElementById('boardPage'))
		document.form.boardPage.value = boardPage;

	if(behavior != 'BoardSearch') {
		if(document.getElementById('boardCriteria'))
			document.form.boardCriteria.value = criteria;

		if(document.getElementById('boardKeyword'))
			document.form.boardKeyword.value = keyword;
	}
}

function submitBoardForm(behavior, initialBehavior, boardName, boardId, commentId, boardPage, criteria, keyword, defaultPage, sectionId) {
	
	if(initialBehavior == 'BoardViewTitle')
		initialBehavior = '';

	setBoardValues(behavior, initialBehavior, boardName, boardId, commentId, boardPage, criteria, keyword);
	
	var thisDefaultPage = '';
	if(defaultPage && defaultPage != '')
		thisDefaultPage = defaultPage;
	if(sectionId && sectionId != '')
		thisDefaultPage = thisDefaultPage + '?sectionId=' + sectionId;
	if(thisDefaultPage != '')
		document.form.action = thisDefaultPage;

	// do the validation for adding and editing
	if((behavior == 'BoardDoAddMessage') || (behavior == 'BoardDoEditMessage') || (behavior == 'BoardDoDeleteMessage')) {
		if(validate(document.getElementById('form'))) {
			document.form.submit();
		}
	}
	else {
		document.form.submit();
	}
}

/*** other functions ****/
function focusOn(id) {

	if(document.getElementById(id)) {	
		if(document.getElementById(id).type != 'hidden')
			eval('document.form.' + id + '.focus();');
		else if(document.getElementById('title'))
			document.getElementById('title').focus();
	}
	else if(document.getElementById('currentPassword'))
		document.getElementById('currentPassword').focus();
}

function createFlash(width, height, url, transparent)
{
  var code =
  	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
  	+ ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"'
  	+ ' width="' + width + '" height="' + height + '" ID="Shockwaveflash1">'
  	+ '<param name="movie" value="' + url + '">'
  	+ '<param name="quality" value="high">'
  	+ (transparent ? '<param name="wmode" value="transparent">' : '')
  	+ '<embed src="' + url + '" width="' + width + '" height="' + height + '" quality="high"'
  	+ ' pluginspage="http://www.macromedia.com/go/getflashplayer"'
  	+ ' type="application/x-shockwave-flash" wmode="transparent"></embed></object>';

  	document.write(code);
}

function popUpWindow(url,left,top,width,height){

	window.open(url,"","width=width,height=height,scrollbars=1")
}

function openWindow(url, name, width, height) {

	window.open(url,name,'top=20, left=50, location=no, width=' + width + ',height=' + height + ',scrollbars=no');
}

function galleryGoTo(sectionId, current, page) {

	document.form.galleryPage.value = page;
	document.form.action = 'Default.aspx?sectionId=' + sectionId;
	document.form.submit();
}

function submitForm(behavior, sectionId) {

	document.form.behavior.value = behavior;

	if(!sectionId)
	sectionId = 10;
	
	document.form.action = 'Default.aspx?sectionId=' + sectionId;

	if((behavior == "SearchCompany") && document.getElementById('searchPage'))
		document.form.searchPage.value = 1;

	if(behavior == 'UpdateProfile') {
		if(validate(document.getElementById('form'))) {
			document.form.submit();
		}
	}
	else {
		document.form.submit();
	}
}

function searchCompany(behavior, sectionId, page) {

	document.form.behavior.value = behavior;
	if(sectionId)
		document.form.action = 'Default.aspx?sectionId=' + sectionId;

	document.form.searchPage.value = page;
	document.form.submit();
}

/*** template functions ***/
function userManager(behavior, userId, userPerPage, page) {


	document.form.behavior.value = behavior;
	
	if(userId && document.getElementById('userId'))
		document.form.userId.value = userId;

	if((behavior == 'UserSearch') && document.getElementById('userPage'))
		document.form.userPage.value = 1;
	else if(page && document.getElementById('userPage'))
		document.form.userPage.value = page;

	if(userPerPage && (userPerPage != '') && document.getElementById('userPerPage'))
		document.form.userPerPage.value = userPerPage;

	if((behavior == 'DoAddUser') || (behavior == 'DoUpdateUser') || (behavior == 'DoEditUser') || (behavior == 'DoUserPassword')) {
		if(validate(document.getElementById('form')))
			document.form.submit();
	}
	else
		document.form.submit();
}
