// Remove outer frames (if any)
//if(self!=top)document.images?top.location.replace(document.location.href):top.location.href=document.location.href;
	
// Functions
function menu_handler(action)
{
	switch(action.params.title)
	{
		case 'logout':
		if(!confirm('This will clear your current session. Continue?'))return false;
		break;
	}
}

function hilite(Element, Action, Class, Check)
{
	switch(Action)
	{
		case 'over':
			if(Element.className!='clicked')Element.className='hilite';
			else Element.className='hilite2';
			break;
	
		case 'out':
			if(Element.className!='hilite2')Element.className=Class;
			else Element.className='clicked';
			break;
			
		case 'click':
			if(Element.className!='hilite2')Element.className='hilite2';
			else Element.className='hilite';
			if(Check)try{document.getElementById(Check).checked = Element.className=='hilite2'}catch(err){};
			break;
	}
}

function check_inverse($form_id)
{
	$frm = document.getElementById($form_id);
	$counter = 0;
    for($i=0; $i<$frm.elements.length; $i++)
	{
		if($frm.elements[$i].type=='checkbox')
		{
			$frm.elements[$i].checked = !$frm.elements[$i].checked;
			document.getElementById('hr'+($counter+1)).className = $frm.elements[$i].checked ? 'clicked' : (($counter%2) ? 'even' : 'odd');
			$counter ++;
		}
	}
}

function toggle(id)
{
	if(!id)id='';
	
	if(document.getElementById('hide'+id))
	{
		if(document.getElementById('hide'+id).style.display!="")
		{
			document.getElementById('hide'+id).style.display="";
			document.getElementById('showHide').innerHTML='[-]';
		}
		else
		{
			document.getElementById('hide'+id).style.display="none";
			document.getElementById('showHide').innerHTML='[+]';
		}
	}
}

function toggle2($hide_id, $link_id)
{
	if(document.getElementById($hide_id))
	{	
		if(!$hide_id)$hide_id='';
		if(document.getElementById($hide_id).style.display!="")
		{
			document.getElementById($hide_id).style.display="";
			document.getElementById($link_id).innerHTML='[-]';
		}
		else
		{
			document.getElementById($hide_id).style.display="none";
			document.getElementById($link_id).innerHTML='[+]';
		}
	}
}

function toggle_seesaw($show_id, $link_id_s, $hide_id, $link_id_h)
{
	if(document.getElementById($show_id) && document.getElementById($hide_id))
	{
		var $reverse = document.getElementById($show_id).style.display!="";
		
		document.getElementById($reverse ? $show_id : $hide_id).style.display="";
		document.getElementById($reverse ? $link_id_s : $link_id_h).innerHTML='[-]';

		document.getElementById($reverse ? $hide_id : $show_id).style.display="none";
		document.getElementById($reverse ? $link_id_h : $link_id_s).innerHTML='[+]';
	}
}


function toggle_id($id, $show, $display_mode) // 'block' ?
{
	if(document.getElementById($id))
	{
		if($display_mode)var $state = $show?$display_mode:'none';
		else var $state = $show?'':'none';

		if(document.getElementById)document.getElementById($id).style.display = $state; // DOM3 = IE5, NS6
		else
		{
			if(document.layers)document.$id.display = $state; // Netscape 4
			else document.all.$id.style.display = $state; // IE 4
		}
	}
}

function update_custom($promo_id)
{
	// Get list entries
	Http.get({
		url: 'js_clbls.php?p=' + $promo_id,
		callback: fillLabels,
		cache: Http.Cache.Get
	});
}

function fillLabels($xmlreply)
{
	if($xmlreply.status == Http.Status.OK)
	{
		var $array = $xmlreply.responseText.split(';'); // Get label texts
		
		for($i=1; $i<=5; $i++)
		{
			if($i<=$array.length && $array[$i-1])
			{
				// Populate label
				document.getElementById('CustomTitle' + $i).innerHTML = $array[$i-1] + ':';
													  
				// Display field
				toggle_id('CustomWrap' + $i, 1);
				
				// Update active fields num
				$tot_fields ++;
			}
			else toggle_id('CustomWrap' + $i); // Hide field
		}
		
		toggle_id('CustomWrap', $tot_fields); // Show header?

		// Show update box?
		if(!$new_entry)toggle_id('UpdateWrap', $tot_fields);
		else toggle_id('UpdateWrap');
	}
	else alert('ERROR: Could not brand field(s). Please try again!');
}

function enable_custom($enable)
{
	for($i=0; $i<$tot_fields; $i++)document.getElementById('Custom' + ($i+1)).readOnly = !document.getElementById('UpdateCustom').checked;
}

function enable_ids($enable, $ids)
{
	$field_arr = $ids.split(',');
	for($i=0; $i<$field_arr.length; $i++)document.getElementById($field_arr[$i]).readOnly = !$enable;
}
	
function setOptions(URL)
{
	var $ck = new Date() + rand_urn(6);
	
	// Get list entries
	Http.get({
		url: URL + (URL.indexOf('?')>-1 ? '&' : '?') + 'cK=' + $ck, // Clear cached versions (if any)
		callback: fillLists,
		cache: Http.Cache.Get
	});
}

function fillLists(xmlreply)
{
	if(xmlreply.status == Http.Status.OK)
	{
		if(response = xmlreply.responseText)
		{
			var main_arrays = response.split('::');
			for(i=0; i<main_arrays.length; i++) // First get all option value arrays
			{
				curr_array = main_arrays[i].split('|'); // Next get all elements in current array
				curr_id = document.getElementById(curr_array[0]); // i.e. 'SubGroupID'
				
				if(curr_id)
				{
					curr_id.options.length = 1; // Clear all entries except first (empty) one
					for(j=1; j<curr_array.length; j++) // Loop through array and build options list (NOTE: First entry is Options ID so is skipped here)
					{
						row = curr_array[j].split(';'); // Get current row
						curr_id.options[j]=new Option(unescape(row[1]), row[0]); // Add to options list (text, value)
						if(row[2]!='1')curr_id.options[j].className='inactive'; // Red text if list item not active
					}
				}
			}
		}
	}
	else alert('ERROR: Could not generate list(s). Please try again!');
}

function reset_option($id_csv)
{
	$id_arr = $id_csv.split(',');
	for($i=0; $i<$id_arr.length; $i++)
	{
		if(document.getElementById($id_arr[$i]))
		{
			document.getElementById($id_arr[$i]).options.length = 1; // Clear all entries except first (empty) one
		}
	}
}

function disable_button(id)
{
	document.getElementById(id).value = 'please wait ...';
	document.getElementById(id).disabled = true;
	document.getElementById(id).className = 'btn dead';
}

function rand_pwd(chars)
{
	var base1	= '4679BbCcDdFfGgHhKkMmNnPpRrTtWwXxZz4679BbCcDdFfGgHhKkMmNnPpRrTtWwXxZz';
	var base2	= '4BdFfGZbCc6DgHrwTtXGKkWXrwpTztXrwpTtXGKkWMmN9x7hnPpRZc6DgH9FfPN9bRrw';
	var base3	= 'HhCcDdNBWbRrztpTtXGKkWXrwpTtw6KkM7m749FfPN9x7hnPwpTztXpRBzrwpTtXGgnx';
	var ret_str = '';
	
	for(i=0; i<chars; i++)ret_str += eval('base' + Math.ceil(Math.random()*3)).charAt(Math.floor(Math.random()*chars));
	
	return ret_str;
}

function rand_urn(chars)
{
	var base1	= '4679CDFGHJKLMNPRTWXKWMNXZ4679CDFGHJKLMNPRTWXZ';
	var base2	= '4BFGC6HTX2KWMN9ZDHTX2KWMDNWBR26N97PRNHTXKWBR2';
	var base3	= 'HCDNWBR26KMHTXKWMN9ZDHTX2KWMN974DNWBR29FPZTXG';
	var ret_str = '';
	
	for(i=0; i<chars; i++)ret_str += eval('base' + Math.ceil(Math.random()*3)).charAt(Math.floor(Math.random()*chars));
	
	return ret_str;
}

function popupWin(url, winWidth, winHeight, title, scrollbars, resizable)
{
	var xPos=(screen.availWidth/2)-((winWidth/2)+10);
	var yPos=(screen.availHeight/2)-((winHeight/2)-10);

	if(xPos<0)xPos=1;
	if(yPos<0)yPos=1;
	if(scrollbars)scrollbars="yes"; else scrollbars="no";
	if(resizable)resizable="yes"; else resizable="no";
	var s=",top="+yPos+",left="+xPos+",width="+winWidth+",height="+winHeight+",scrollbars="+scrollbars+",resizable="+resizable;
	var win=window.open(url,title,"toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no"+s);
	win.focus();
	
	return false;
}

function copy_vals($frm, $src, $dst)
{
	if(confirm('This will OVERWRITE the \'\'' + $dst + '\'\' details (if any). Continue?'))
	{
		var $form = document.getElementById($frm);

		for(var $i=0; $i<$form.length; $i++)
		{
			var $curr_id = $form.elements[$i].id;
			if($curr_id.indexOf($src)>-1)document.getElementById($curr_id.replace($src, $dst)).value = $form.elements[$i].value;
		}
	}
}

function confirmDel(ProdTitle, qty)
{
	if((isNaN(qty)||qty<1)&&!confirm('This will DELETE "' + unescape(ProdTitle) + '" from your basket. Continue?'))return false;
}
