var boolFormDataChanged = false;
var boolPreventTableResize = false;
var intCurrResizeColumnId = -1;
var intDragStartPosition = 0;
var boolResizing = false;
var boolBrowseExpanded = true;
var intBrowsePreviousSize = 0;
var intLeftBound = 174;
var	intRightBound = 0;
var strOldPageLink;

function apply_form()
{
	window.onbeforeunload=null;
	document.getElementById('save_quit').value='';
}

function save_form()
{
	window.onbeforeunload=null;
	document.getElementById('save_quit').value='yes';
}

function cancel_form(boolCloseWindow)
{
	if (boolCloseWindow)
	{
		if ( window.opener != undefined )
			window.close();
	}
}

function revert_form()
{
}

function disable_submit_buttons()
{
	document.getElementById('apply_button').disabled = true;
	document.getElementById('save_button').disabled = true;
}

function is_form_data_changed()
{
	return boolFormDataChanged;
}

function set_form_data_changed()
{
	boolFormDataChanged = true;
}

function is_preview_ok()
{
	if(is_form_data_changed())
	{
		return confirm("You did not save the changes.\nThe page you are about to view will not reflect unsaved changes.\nWould you like to view this page anyway?");
	}
	else return true;
}

function handle_unload()
{
	if(is_form_data_changed())
		return "You did not save the changes!";
}

function highlight_list_row(objRow, strColor)
{
	objRow.style.backgroundColor = strColor;
}

function test_func()
{}

function expand_collapse_browse(intColumnId)
{
	strColumnName = "column_"+intColumnId;
	
	if(boolBrowseExpanded)
	{
		intBrowsePreviousSize = document.getElementById(strColumnName).width;
		document.getElementById(strColumnName).width = 1;
		document.getElementById("slider").style.left = intLeftBound;
		document.getElementById("browse_expand_collapse_button").src='assets/images/gui/pluroweb-b-browse-expand.gif';
		boolBrowseExpanded = false;
	}
	else
	{
		document.getElementById(strColumnName).width = intBrowsePreviousSize;
		document.getElementById("slider").style.left = parseInt(intLeftBound) + parseInt(intBrowsePreviousSize);
		document.getElementById("browse_expand_collapse_button").src='assets/images/gui/pluroweb-b-browse-collapse.gif';
		boolBrowseExpanded = true;
	}
}

function start_resize(objEvent, intColumnId, intLeftBound)
{
	document.getElementById('resize_cover_layer').style.display = 'block';
	intDragStartPosition = parseInt(document.getElementById("slider").style.left);

	document.onmouseup = stop_resize;
	document.onmousemove = do_drag;

	intCurrResizeColumnId = intColumnId;
//	if (objEvent) intDragStartPosition = objEvent.clientX;
//	else intDragStartPosition = window.event.clientX;

//alert(intDragStartPosition);
//alert(document.getElementById("slider").style.left);
	return false;
}

function stop_resize(objEvent)
{
	if(!boolBrowseExpanded)
	{
		boolBrowseExpanded = true;
		document.getElementById("browse_expand_collapse_button").src='assets/images/gui/pluroweb-b-browse-collapse.gif';
	}

	intRightBound = document.body.clientWidth - 200;

	document.onmousemove = null;
	document.onmouseup = null;
	
/*	if (objEvent)
	{ intDragEndPosition = objEvent.clientX; }
	else { intDragEndPosition = window.event.clientX; }*/

	intDragEndPosition = parseInt(document.getElementById("slider").style.left);
//	alert(intDragStartPosition)
//	alert(intDragEndPosition)
//	alert(parseInt(intDragEndPosition - intDragStartPosition))
	intNewWidth = parseInt(intDragEndPosition - intDragStartPosition) + parseInt(document.getElementById("column_"+intCurrResizeColumnId).width);

	if(intNewWidth >= 1)
	{
		document.getElementById("column_"+intCurrResizeColumnId).width = intNewWidth;
		intDragStartPosition = intDragEndPosition;
	}

	document.getElementById('resize_cover_layer').style.display = 'none';
	return false;
}

function do_drag(objEvent)
{
	intRightBound = document.body.clientWidth - 200;

	if (objEvent)
	{ intCurrPos = objEvent.clientX; }
	else { intCurrPos = window.event.clientX; }

	if(intCurrPos < intLeftBound) intCurrPos = intLeftBound;
	if(intCurrPos > intRightBound) intCurrPos = intRightBound;

	document.getElementById("slider").style.left = intCurrPos;
	return false;
}

function resize_table()
{
	return true;
	if(!boolPreventTableResize)
	{
		if(detect_browser()!='msie') document.getElementById('row_div').style.height = window.innerHeight-document.getElementById('list_header_cell').scrollHeight-20;

		objHeadTable = document.getElementById('head_table');
		objRowTable = document.getElementById('row_table');	
  
		// check if table contains any rows
		if(objRowTable.rows[0])
		{
			// iterate through rows in the row table
			for (var i = 0; i < objRowTable.rows[0].cells.length; i++)
			{
				intWidth = objRowTable.rows[0].cells[i].offsetWidth;
				if ( intWidth != 0 )
					objHeadTable.rows[0].cells[i].width = intWidth;
			}
		}
	}
	return false;
}


function toggle_menu_div(strMenuDivId)
{
	objMenuDiv = document.getElementById('menu_div_'+strMenuDivId);
	objExClolImg = document.getElementById('excol_img_'+strMenuDivId);
	
	if(objMenuDiv.style.display == 'block')
	{
		objMenuDiv.style.display = 'none';
		objExClolImg.src = 'assets/images/gui/pluroweb-b-expand.gif';
	}
	else
	{
		objMenuDiv.style.display = 'block';
		objExClolImg.src = 'assets/images/gui/pluroweb-b-collapse.gif';
	}
}

function link_mouseover(strLinkTableId)
{
	return;
	objLinkTableId = document.getElementById('menu_link_table_' + strLinkTableId);
	objLinkTableId.className = 'table_link_ov';
}

function link_mouseout(strLinkTableId)
{
	return;
	objLinkTableId = document.getElementById('menu_link_table_' + strLinkTableId);
	objLinkTableId.className = 'table_link';
}


function toggle_div(strDivId)
{
	alert(strDivId);
	objDiv = document.getElementById(strDivId);

	if(objDiv.style.display=='block')
	{
		objDiv.style.display='none';
	}
	else
	{
		objDiv.style.display='block';
	}
}

function show_help(x, y, name, message)
{
	document.getElementById('help_message').style.left = x-210;
	document.getElementById('help_message').style.top = y-10;
	document.getElementById('help_message').innerHTML = '<table width="100%"  border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF" style="border:1px solid #134596; font-size: 11px;">  <tr><td bgcolor="#134596" style="color:#FFFFFF"><b>'+name+'</b></td></tr><tr><td>'+message+'</td></tr></table>';
	document.getElementById('help_message').style.visibility = 'visible';
}

function hide_help()
{
	document.getElementById('help_message').style.visibility = 'hidden';
}

function show_row_quick_preview(intRowCounter)
{
	objQuickPreview = document.getElementById('row_quick_preview_' + intRowCounter);
	if ( objQuickPreview != undefined )
	{
		objQuickPreview.style.left = xMousePos+20;
		objQuickPreview.style.top  = yMousePos-90;
		objQuickPreview.style.display = 'block';
	}
}

function hide_row_quick_preview(intRowCounter)
{
	objQuickPreview = document.getElementById('row_quick_preview_' + intRowCounter);
	if ( objQuickPreview != undefined )
		objQuickPreview.style.display = 'none';
}

var strOldPageLink = null;
function auto_generate_page_handle()
{
	if(document.getElementById('autolink').checked)
	{
		strOldPageLink = document.getElementById('new_page_handle').value;
		generate_page_handle();
	}
	else
	{
		if ( strOldPageLink == undefined )
			strOldPageLink = '';
		document.getElementById('new_page_handle').value = strOldPageLink;
	}	
}

function generate_page_handle()
{
	set_form_data_changed();
	//if ( document.getElementById('new_page_handle').value )
	//{
	//	if (confirm("Change current page link?"))
	//	{
			//date = new Date();
			strPageName = document.getElementById('page_title').value.toLowerCase();
			strPageName = strPageName.replace(/\s/g, '-');

			strPageName = strPageName.replace(/[^a-z0-9\-]*/gi, '');
			document.getElementById('new_page_handle').value = strPageName;// + '-' + date.getYear() +  zero_fill(date.getMonth(), 2) +  zero_fill(date.getDay(),2) + zero_fill(date.getHours(), 2) + zero_fill(date.getMinutes(), 2) + zero_fill(date.getSeconds(), 2);
			
			process_input_blur(document.getElementById('new_page_handle'));
			
			document.getElementById('link_current_page_handle').value = document.getElementById('new_page_handle').value;
			document.getElementById('link_current_page_handle_human').value = document.getElementById('page_title').value;

			document.getElementById('link_body').value = document.getElementById('page_title').value;
			document.getElementById('link_body_on').value = document.getElementById('page_title').value;

	//	}
	//}
}

function generate_product_detail_page_handle()
{
	if ( document.getElementById('prod_page').value == '' )
	{
		date = new Date();

		str = document.getElementById('prod_sname').value.toLowerCase();
		str = str.replace(' ', '-');
		str = str.replace(/[^a-z0-9\-]*/gi, '');
		document.getElementById('prod_page').value = str + '-' + date.getYear() +  zero_fill(date.getMonth(), 2) +  zero_fill(date.getDay(),2) + zero_fill(date.getHours(), 2) + zero_fill(date.getMinutes(), 2) + zero_fill(date.getSeconds(), 2);
	}
}

function generate_product_detail_title()
{
	if ( document.getElementById('page_metatag_title').value == '' )
		document.getElementById('page_metatag_title').value = document.getElementById('prod_sname').value;
}

function generate_product_detail_page_handle_addscreen()
{
	date = new Date();

	str = document.getElementById('prod_sname').value.toLowerCase();
	str = str.replace(' ', '-');
	str = str.replace(/[^a-z0-9\-]*/gi, '');
	document.getElementById('prod_page').value = str + '-' + date.getYear() +  zero_fill(date.getMonth(), 2) +  zero_fill(date.getDay(),2) + zero_fill(date.getHours(), 2) + zero_fill(date.getMinutes(), 2) + zero_fill(date.getSeconds(), 2);
}

function generate_product_detail_title_addscreen()
{
	document.getElementById('page_metatag_title').value = document.getElementById('prod_sname').value;
}
function zero_fill(str, len)
{
	str = str.toString();
	var ret = '';
	if ( str.length < len )
	{
		for ( var i=0; i< len-str.length; i++ )
			ret = ret + '0';
			
		ret = ret + str;
	}
	
	if ( ret == '' )
		ret = str;
	
	return ret;
}

function clear_field_value(strFieldId)
{
	if ( strFieldId != '' )
	{
		objField = document.getElementById(strFieldId);
		if ( objField != undefined )
			objField.value = '';
	}
}

function generate_link_name()
{
	if ( get_checked_radio_value(document.forms[0].link_to_type) == 'page_handle' )
		strValue = document.getElementById('link_to_page_handle_human').value;
	else
		strValue = document.getElementById('link_url').value;
		
	document.getElementById('link_name').value = strValue;
	document.getElementById('link_body').value = strValue;
	document.getElementById('link_body_on').value = strValue;
}

function generate_link_name_from_page_title()
{
	strValue = document.getElementById('page_title').value;
		
	document.getElementById('link_name').value = strValue;
	document.getElementById('link_body').value = strValue;
	document.getElementById('link_body_on').value = strValue;
}

function prefil_menu_link_body()
{
	if ( document.getElementById('link_body').value == '' )
	{
		document.getElementById('link_body').value		= document.getElementById('link_name').value;
		document.getElementById('link_body_on').value	= document.getElementById('link_name').value;
	}
}

function generate_page_title()
{
	if ( document.getElementById('page_title').value == '' )
	{
		document.getElementById('page_name').value = document.getElementById('page_title').value;
	}
}

function preview_page_url(url, linkpre, linkpost)
{
	if ( document.getElementById('page_handle').value != '' ) 
		document.getElementById('page_link').innerHTML = url + linkpre + document.getElementById('page_handle').value + linkpost;
}

if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has 
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position 
        // reflects the position from the top/left of the screen the 
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no 
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard 
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

function update_date_field(source, destination)
{
	input = document.getElementById(source).value;
	input = input.replace(/[^0-9]*/gi, '');
	output = input.substr(0,8) + '000000';
	document.getElementById(destination).value = output;
}

function prefil_date_field(source, destination)
{
	input = document.getElementById(source).value;
	if ( input.length >= 8 )
	{
		output = input.substr(0,4) + '-' + input.substr(4,2) + '-' + input.substr(6,2);
		document.getElementById(destination).value = output;
	}
}

function update_date_time_field(strId)
{
	strFieldDate 	  = strId;
	strFieldDateHuman = strId + '_date';
	strFieldTimeHour  = strId + '_time_hh';	
	strFieldTimeMin	  = strId + '_time_mm';	
	strFieldTimeAmPm  = strId + '_time_ampm';	
		
	strDateHuman = document.getElementById(strFieldDateHuman).value;
	strDateHuman = strDateHuman.replace(/[^0-9]*/gi, '');
	strDate		 = strDateHuman.substr(0,8);

	strTimeHour	 = parseFloat(document.getElementById(strFieldTimeHour).value);
	strTimeMin	 = parseFloat(document.getElementById(strFieldTimeMin).value);
	strTimeAmPm  = document.getElementById(strFieldTimeAmPm).value;
	
	if ( strTimeAmPm == 'pm' )
	{
		if ( strTimeHour < 12 )
			strTimeHour += 12;
	}
	else if ( strTimeAmPm == 'am' )
	{
		if ( strTimeHour == 12 )
			strTimeHour = 0;
	}
	
	strTimeHour = strTimeHour.toString();
	if ( strTimeHour.length < 2 )
		strTimeHour = '0' + strTimeHour;

	strTimeMin	= strTimeMin.toString();
	if ( strTimeMin.length < 2 )
		strTimeMin = '0' + strTimeMin;
	
	strTime	= strTimeHour + strTimeMin + '00';
	
	strDateTime =  strDate + strTime;
	document.getElementById(strFieldDate).value = strDateTime;
}

function prefil_date_time_field(strId)
{
	strDateField = strId;
	strDateFieldHuman = strId + '_date';
	strFieldTimeHour  = strId + '_time_hh';	
	strFieldTimeMin	  = strId + '_time_mm';	
	strFieldTimeAmPm  = strId + '_time_ampm';	

	strDateTime = document.getElementById(strDateField).value;
	if ( strDateTime.length >= 8 )
	{
		strDate = strDateTime.substr(0,4) + '-' + strDateTime.substr(4,2) + '-' + strDateTime.substr(6,2);
		strTimeHour = strDateTime.substr(8,2);
		strTimeMin 	= strDateTime.substr(10,2);

		if ( strTimeHour >= 12 )
		{
			if ( strTimeHour != 12 )
				strTimeHour = strTimeHour - 12;
			strTimeAmPm = 'pm';
		}
		else
		{
			if ( strTimeHour == 0 )
				strTimeHour = 12;
				
			strTimeAmPm = 'am';
		}
		
		select_dropdown_value(document.getElementById(strFieldTimeHour), strTimeHour);
		select_dropdown_value(document.getElementById(strFieldTimeMin), strTimeMin);
		select_dropdown_value(document.getElementById(strFieldTimeAmPm), strTimeAmPm);

		document.getElementById(strDateFieldHuman).value = strDate;
	}
}

function float_to_money(input, boolIgnoreCents, intPrecision)
{
	if ( intPrecision == undefined )
		intPrecision = 2;
		
	input = input.toString();
	input = input.replace(/[^0-9\.]*/gi, '');

	if ( input )
	{
		money = input.split('.');
		if ( money[0].length == 0 )
			dolars = '0';
		else
			dolars = money[0];
	
		if ( boolIgnoreCents )
			return dolars;
			
		if (money.length > 1)
			cents = money[1];
		else
		{
			if ( intPrecision == 4 )
				cents = '0000';
			else
				cents = '00';
		}
			
		if ( intPrecision == 4 )
		{
			switch ( cents.length )
			{
				case 0:
					cents = '0000';
					break;
				case 1:
					cents = cents + '000';
					break;
				case 2:
					cents = cents + '00';
					break;
				case 3:
					cents = cents + '0';
					break;
			}
			
			if ( cents.length > 4)
				cents = cents.substr(0, 4);
		}
		else
		{
			if (cents.length == 0)
				cents = '00';
			if (cents.length == 1)
				cents = cents + '0';
			if ( cents.length > 2)
				cents = cents.substr(0, 2);
		}
			
		return dolars + '.' + cents;
	}
	else
		return '';
}

function loadingScreen()
{
	objInputFields = document.getElementsByTagName('SELECT');
	for ( i=0; i<objInputFields.length; i++)
		objInputFields[i].disabled = true;
	
	objBody = document.body;
	objLayer = document.createElement("DIV");
	
	objLayer.style.position 	= "absolute";
	objLayer.style.visibility 	= "visible";
	objLayer.style.left			= "0px";
	objLayer.style.top			= "0px";
	objLayer.style.height		= "100%";
	
	objLayer.innerHTML = '<div style="z-index:1; filter:alpha(opacity=80); -moz-opacity:0.8; opacity:0.8; background-color:#FFFFFF; width:100%; height:100%"><div style="z-index:2; height:100%; filter:alpha(opacity=100); -moz-opacity:1.0; opacity:1.0; position:relative"><table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="middle"><table width="300" align="center" border="0" cellspacing="0" cellpadding="20" style="border:3px solid #00084A" bgcolor="#CEE5FF"><tr><td align="center"><font color="#00084A" size="4" face="Arial, Helvetica, sans-serif"><b>Please wait. Loading... </b></font></td></tr></table></td></tr></table></div></div>';
	
	objBody.appendChild(objLayer);
}

var row_selection = Array();
function highlight(obj, table_id, id, action, original_color, new_color)
{
	if ( row_selection[table_id] == undefined )
		row_selection[table_id] = Array();
		
	if (action == 'click')
	{
		if (row_selection[table_id][id] == undefined)
		{
			obj.style.backgroundColor = new_color;
			row_selection[table_id][id] = 1;
			if ( document.getElementById(id) != undefined )
				document.getElementById(id).checked = true;
		} else {
			obj.style.backgroundColor = original_color;
			row_selection[table_id][id] = undefined;
			if ( document.getElementById(id) != undefined )
				document.getElementById(id).checked = false;
		}
	}
	
	if (action == 'over')
	{
		if (row_selection[table_id][id] == undefined)
			obj.style.backgroundColor = new_color;
	}
	
	if (action == 'out')
	{
		if (row_selection[table_id][id] == undefined)
			obj.style.backgroundColor = original_color;
	}
}

function update_field_on_advanced_tab(objSource, strFieldType)
{
	// regular mapping
	strTargetId = objSource.id;
	strTargetId = strTargetId.replace('_simple', '');
	objTarget = document.getElementById(strTargetId);
	objTarget.value = objSource.value;
	process_input_blur(objTarget);
	
	// additional mapping per field
	switch(strFieldType)
	{
		case 'directory_type_name':
			document.getElementById('prod_sname').value = objSource.value;
			process_input_blur(document.getElementById('prod_sname'));
			generate_product_detail_page_handle();
			process_input_blur(document.getElementById('prod_page'));
			break;
	}
}

function update_field_on_simple_tab(objSource, strFieldType)
{
	// regular mapping
	strTargetId = objSource.id + '_simple';
	objTarget = document.getElementById(strTargetId);
	objTarget.value = objSource.value;
	process_input_blur(objTarget);
	
	// additional mapping per field
	switch(strFieldType)
	{
	}
}

function display_sort_order_disabled_message()
{
	alert('Please sort by Order column to enable this feature.');
}

function tab_disabled_upon_add(strMessage)
{
	if ( strMessage != undefined )
		alert(strMessage);
	else
		alert('This tab is disabled upon adding process. Please save to continue');
		
	return false;
}