/**
 * Jesse Jordan 2010
 * entities_catalog.js
 *
 */

function jecExpand(divId, request_url) {
	menuDiv = document.getElementById(divId);
	menuHeadDiv = document.getElementById(divId+'_head');
	if(menuHeadDiv.className == 'head_closed') {
		if(menuDiv.innerHTML.length == 0) {
			document.body.style.cursor = 'wait';
			ajax_request(jecExpandHandler, window.location.pathname + '?ajax_submit=1&' + request_url, divId);
		} 
		menuHeadDiv.className = 'head_opened';
		menuDiv.style.display = 'block';
	} else {
		menuHeadDiv.className = 'head_closed';
		menuDiv.style.display = 'none';
	}
	
	return false;
}

function jecExpandAll() {
	var els = document.getElementsByTagName('div');
	for(var i = 0; i < els.length; i++) {
		if(els[i].className == 'head_closed') {
			els[i].onclick();
		}
	}
}

function jecExpandHandler(response, request, divId) {
	var jecToExpand = document.getElementById(divId);
//	alert(response);
	jecToExpand.innerHTML = response;
	document.body.style.cursor = 'default';	
}

function jecReloadCart() {
	document.body.style.cursor = 'wait';
	var request_url = window.location.pathname + '?reload_cart=1';
	ajax_request(jecReloadCartHandler, request_url);
}

function jecReloadCartHandler(response, request) {
	var jecCart = document.getElementById('jecCart');
	jecCart.innerHTML = response;
	document.body.style.cursor = 'default';	
}

function jecLoadItem(artifactId) {
	var jecItem = document.getElementById('jecItem');
	
	document.body.style.cursor = 'wait';
	var request_url = window.location.pathname + '?ajax_submit=1&artifact_id=' + artifactId;
	ajax_request(jecItemHandler, request_url, artifactId);
	return false;
}

function jecSubmit(formElementId, artifactId) {
	document.body.style.cursor = 'wait';
	var fE = document.getElementById(formElementId);
	var foo = $(fE).serialize();
	//alert(foo);
	var request_url = window.location.pathname + '?ajax_submit=1&' + foo;
	ajax_request(jecSubmitHandler, request_url, formElementId, artifactId);
	return false;
}

//do the same thing, but use the itemHandler
function jecLocationSubmit(formElementId, artifactId) {
	document.body.style.cursor = 'wait';
	var fE = document.getElementById(formElementId);
	var foo = $(fE).serialize();
	//alert(foo);
	var request_url = window.location.pathname + '?ajax_submit=1&' + foo;
	ajax_request(jecItemHandler, request_url, artifactId);
	return false;
}

function jecSubmitHandler(response, request, formElementId, artifactId) {
	var jecItem = document.getElementById('jecItem');
	var junifiedContents = document.getElementById('junifiedContents');
	/* jecItem.style.display = 'block';
	junifiedContents.style.opacity = '.1';
	jecItem.style.zIndex = 599; */ 
	//var jsResult = eval('(' + [response] + ')');
	var jsResult = eval('(' + [response] + ')');
	//var jsResult = eval([response]);
	var errs = jsResult.iis.errors;
	var cp = jsResult.cp;
	var errCount = 0;
	for(var err in errs) {
//		alert(err+': '+errs[err]);
		errCount++;
	}
	
	if(errCount > 0) {
		var request_url = window.location.pathname + '?artifact_id=' + artifactId + '&'+cp;		
		ajax_request(jecItemHandler, request_url, artifactId);
		//alert('Please correct the red errors to proceed');
		//alert('after');
	} else {
		jecHideItem();
		jecReloadCart();
	}

	
		
	//jecItem.innerHTML = response;
	document.body.style.cursor = 'default';
}

function jecHideItem() {
	var jecItem = document.getElementById('jecItem');
	var jecCart = document.getElementById('jecCart');
	var junifiedContents = document.getElementById('junifiedContents');
	var container = document.getElementById('container');
	if(jecCart) {	
		container.style.visibility = 'visible';
		
		jecCart.style.position = 'static';
		jecCart.style.top = '';
		jecCart.style.marginLeft = '10px';
	
		junifiedContents.style.opacity = '1';
		
		jecItem.style.display = 'none';
	} else {
		window.location = window.location.protocol + '//' + window.location.host + window.location.pathname;
	}
}

function jecItemHandler(response, request, artifactId) {
	//var cname = document.getElementById(niId).className;
	/*if(cname.substring(cname.length-1) == 'r') { 
		document.getElementById(niId).className = response == 'on' ? 'note_icon_onr' : 'note_icon_offr';
	} else {
		document.getElementById(niId).className = response == 'on' ? 'note_icon_on' : 'note_icon_off';
	}
	document.getElementById(niId).title = response == 'on' ? 'View/Change Note' : 'Add Note';
	*/
	var jecItem = document.getElementById('jecItem');
	var junifiedContents = document.getElementById('junifiedContents');
	var container = document.getElementById('container');
	var jecCart = document.getElementById('jecCart');
	jecItem.style.display = 'block';
	junifiedContents.style.opacity = '.1';
	container.style.visibility = 'hidden';
	jecItem.style.visibility = 'visible';
	jecCart.style.visibility = 'visible';
	jecCart.style.position = 'fixed';
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		jecCart.style.marginLeft = '630px';
		jecCart.style.top = '0px';
				
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		jecCart.style.marginLeft = '630px';
		jecCart.style.top = '0px';
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		jecItem.style.position = 'absolute';
		jecItem.style.top = scrOfY + 'px';
		jecCart.style.position = 'absolute';
		jecCart.style.top = scrOfY + 'px';
	}
	
	//alert(scrOfY);
	jecItem.style.zIndex = 599; 

	var jsResult = eval('(' + [response] + ')');
	jecItem.innerHTML = jsResult.content;
	if(jsResult.status == 'loc') {
		jAddressPop(jsResult.loc_selector_id);
	} else if(jsResult.status == 'item') {
		entityChildShow();
	}
	//jecItem.document.write(response);
	document.body.style.cursor = 'default';
}

