var x_pos = 20;
var y_pos = 20;
var already_open = 0;
var next_wid = 0;

function select_selected(list, selected_value) 
{
	var count = list.options.length;
 	for (var current = 0; current < count; current ++) 
 	{
  	if (list.options[current].value == selected_value) 
  	{
   		list.options[current].selected = "1";
   		break;
  	}
 	}
}

function submit_kernel_form()
{
	document.kernel_form.submit();
	document.kernel_form.target = '';
//	document.kernel_form.t.value = t;
}

function new_item(a_t)
{
	document.kernel_form.action.value='new';
	document.kernel_form.t.value = a_t;
	if (popups == 1) {
		tmp_t = t.replace('/', '_');
		openwin('', 'edit_'+tmp_t, 800,600);
		document.kernel_form.target='edit_'+tmp_t;
	}
	submit_kernel_form();
}

function set_first_selected(field)
{
	id = get_first_selected();
	if (id !== false) {
		field.value = id;
		return true;
	}
	else
		return false;
}

function get_first_selected()
{
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(document.kernel_form.mask.value) && document.kernel_form.elements[i].checked) {
			id = RegExp.$1;
			return id;
		}
	}
	return false;
}

function count_selected() {
	count=0;
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(document.kernel_form.mask.value)) {
			if (document.kernel_form.elements[i].checked) count++;
		}
	}
	return count;
}

function inverse_selected() {
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(document.kernel_form.mask.value)) {
			document.kernel_form.elements[i].checked = !document.kernel_form.elements[i].checked
			id = RegExp.$1;
			select_row(id, 1);
		}
	}
}

function clear_all_selected() {
	document.kernel_form.action.value = 'clear_selected';
	submit_kernel_form();
}

function select_all()
{
	//alert(document.kernel_form.cb_select_all.checked);
	if (!document.kernel_form.cb_select_all.checked) clear_all_selected();
	else {
		document.kernel_form.action.value = 'select_all';
		submit_kernel_form();
	}
}

function clear_selected() {
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(document.kernel_form.mask.value)) {
			if (document.kernel_form.elements[i].checked) {
				document.kernel_form.elements[i].checked = false;
				id = RegExp.$1;
				select_row(id, 1);
			}
		}
	}
}

function select_n_modify(t, item_id) {
		document.kernel_form.elements['item_'+item_id].checked = true;
		edit_item(t);
	}

function edit_item(t)
{
	if (selected_count > 0) {
			if (document.kernel_form.elements[pass_id_name] == '')
				set_first_selected(document.kernel_form.elements[pass_id_name]);
			document.kernel_form.action.value='load';
			document.kernel_form.t.value=t;
			if (popups == 1) {
				tmp_t = 'edit_'+t+'_';
				if (multiple_windows == 1) 
					tmp_t += document.kernel_form.elements[pass_id_name].value;
				tmp_t = tmp_t.replace('/', '_');
				//alert(tmp_t);
				openwin('', tmp_t, 800,600);
				document.kernel_form.target=tmp_t;
			}
			if (already_open != 1) submit_kernel_form();
			if ((popups == 1) && (multiple_windows == 1))
				clear_selected();
	}
	else 
		alert('Please select at least one item!');
}

function delete_items() {
	if (selected_count > 0) {
		if (confirm('Are you sure you want to delete '+selected_count+' selected items?')) {
			document.kernel_form.action.value='delete_selected';
			submit_kernel_form();
		}
	}
	else 
		alert('Please select at least one item!');
}

	function do_delete(name) {
		if (get_first_selected(document.kernel_form, document.kernel_form.mask.value) !== false) {
			if (confirm('Are you sure you want to delete selected item(s)?')) {
				document.kernel_form.add_action.value='delete_notifications';
				document.kernel_form.submit();
			}
		}
		else 
			alert('Please select item(s) first');
	}


function go_to_page(template, page) {
	document.kernel_form.t.value = template;
	document.kernel_form.page.value = page;
	submit_kernel_form();
}

function to_page(template, page, target_proc) {
	document.kernel_form.t.value = template;
	document.kernel_form.page.value = page;
	if (target_proc == 'invoices' || target_proc == 'notifications')
		document.kernel_form.target_proc.value = target_proc;
	submit_kernel_form();
}


function save_edit()
{
	submit_kernel_form();
}

function cancel_edit()
{
	document.kernel_form.action.value='cancel';
	submit_kernel_form();
}

var has_changed = false;
function changed()
{
	has_changed = true;
}

function set_on_change() {
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur = document.kernel_form.elements[i];
		if (cur.type != 'hidden') {
			document.kernel_form.elements[i].onchange=changed;
		}
	}
	return false;		
}

function save_and_go(order_id) {
	if (!has_changed) {
		document.kernel_form.action.value='load';
		document.kernel_form.elements[pass_id_name].value=order_id;
		submit_kernel_form();	
	}
	else {
		if (confirm('Do you want to save changes?')) {
			document.kernel_form.action.value='update';
			document.kernel_form.add_action.value='go';
			document.kernel_form.go_id.value=order_id;
			submit_kernel_form();
		}
		else {
			document.kernel_form.action.value='load';
			document.kernel_form.elements[pass_id_name].value=order_id;
			submit_kernel_form();	
		}
	}
}

function sort_grid(field)
{
	document.kernel_form.sort1.value = field;
	document.kernel_form.action.value = 'sort';
	submit_kernel_form();
}

function filter_grid()
{
	document.kernel_form.action.value = 'filter';
	submit_kernel_form();
}

function reset_filters()
{
	document.kernel_form.action.value = 'reset_filters';
	submit_kernel_form();
}

function update_checkbox(cb, cb_hidden)
{
	cb_hidden.value = cb.checked ? 1 : 0;
}

function update_checkbox_options(cb_mask, cbo_hidden)
{
	var tmp = '';
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(cb_mask)) {
			if (document.kernel_form.elements[i].checked) 
				tmp += '|'+document.kernel_form.elements[i].value+'|,';
		}
	}
	tmp = tmp.replace(/,$/, '');	
	cbo_hidden.value = tmp;
}

function column_picker(table,prefix,special)
{
	norm_width = 300;
	norm_height = 440;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=std/column_picker&action=init&table='+table+'&template='+t+'&sid='+sid+'&prefix='+prefix+'&special='+special, 'fields', 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function synchronize_selected()
{
	for (var i = 0; i < document.kernel_form.elements.length; i++) {
		cur_name = document.kernel_form.elements[i].name;
		if (cur_name.match(document.kernel_form.mask.value)) {
			id = RegExp.$1;
			select_row(id, 1);
		}
	}
}

function update_selected_count()
{
	sp_selected_count.innerHTML = selected_count;
	if (selected_count == total_items) document.kernel_form.cb_select_all.checked = true;
	else document.kernel_form.cb_select_all.checked = false;
	
	if (count_selected() == per_page) document.kernel_form.inverse.checked = true;
	else document.kernel_form.inverse.checked = false;
	
}

function select_row(id, mode)
{
	var selected = document.all['item_'+id].checked;
	if (mode == 1) {
		selected = !selected;
	}
	if (selected){
		selected_count--;
		document.all['data_tr_'+id].className = 'data-tr';
		document.all['cb_tr_'+id].className = 'data-tr';
		document.all['item_'+id].checked = false;
	}
	else {
		selected_count++;
		document.kernel_form.elements[pass_id_name].value = id;
		document.all['data_tr_'+id].className = 'data-tr-selected';
		document.all['cb_tr_'+id].className = 'data-tr-selected';
		document.all['item_'+id].checked = true;
	}
	update_selected_count();
}

function select_row2(id, mode)
{
	var selected = document.all['item_'+id].checked;
	if (mode == 1) {
		selected = !selected;
	}
	if (selected){
		selected_count--;
		document.all['item_'+id].checked = false;
	}
	else {
		selected_count++;
		document.kernel_form.pass_id_name.value = id;
		document.all['item_'+id].checked = true;
	}
	update_selected_count();
}





function highlight_row(id, mode)
{
	if (mode == 1) {
		document.all['data_tr_'+id].className = 'data-tr-highlighted';
		document.all['cb_tr_'+id].className = 'data-tr-highlighted';
	}
	else {
		if (document.all['item_'+id].checked) {
			document.all['data_tr_'+id].className = 'data-tr-selected';
			document.all['cb_tr_'+id].className = 'data-tr-selected';
		}
		else {
			document.all['data_tr_'+id].className = 'data-tr';
			document.all['cb_tr_'+id].className = 'data-tr';
		}
	}
}

function remove_closed_windows()
{
	var windows_tmp = Array();
	for(var i = 0;i < window.parent['hidden_script'].windows.length;i++) {
		if(!window.parent['hidden_script'].windows[i].closed) {
			windows_tmp.push(window.parent['hidden_script'].windows[i]);
		}
	}
	window.parent['hidden_script'].windows = windows_tmp;
	var windows_tmp = Array();
}

function find_window(name)
{
	var ix;
	for(var i = 0;i < window.parent['hidden_script'].windows.length;i++) {
		if(window.parent['hidden_script'].windows[i].name == name) {
			ix = i;
			break;
		}
	}
	return ix;
}

function close_windows()
{
	for(var i = 0;i < window.parent['hidden_script'].windows.length;i++) {
		window.parent['hidden_script'].windows[i].close(); 
	}
}

function openwin(url,name,w,h) {
	
	remove_closed_windows();
	//if(params != '') { params = '&'+params; }
	//if(name == '') { name = t+'_'+last_level_id; }
	
	//alert('windows count: '+window.parent['hidden_script'].windows.length);
	
	var ix = find_window(name);
	
	if(isNaN(ix)) {
		next_wid++;
		var new_windw = window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + url,name, 'resizable=yes,toolbar=no,location=no,scrollbars=yes,width='+w+',height='+h+',resizable=yes,top='+y_pos+',left='+x_pos);
		var x = window.parent['hidden_script'].windows.push(new_windw) - 1;
		already_open = 0;
		
		x_pos += 25;
		y_pos += 25;
		if (	(x_pos > (window.screen.width - 800)) ||
					(y_pos > (window.screen.height - 600))) {
			x_pos = 20;
			y_pos = 20;
		}
	}else {
		x = ix;
		if (multiple_windows == 1) already_open = 1;
	}
	
	window.parent['hidden_script'].windows[x].focus();
	document.kernel_form.wid.value = next_wid;
}

function m_column_picker(obj,template,table)
{
	norm_width = 300;
	norm_height = 440;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=member/column_picker&action=init&table='+table+'&template='+t+'&sid='+sid+'&obj='+obj, 'fields', 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function set_window_size(width, height)
{
	window.resizeTo(width, height);
}

function set_page_title(title)
{
	if (window.name == 'kernel_main') 
		window.parent.document.title = title+' - '+main_title;
	else
		document.title = title+' - '+main_title;	
}

function go_to_tab(to)
{
	//alert(t);
	document.kernel_form.tab.value = to;
	document.kernel_form.action.value = 'pre_save';
	submit_kernel_form();
}

function go_to_tab_save(to)
{
	//alert(t);
	document.kernel_form.tab.value = to;
	document.kernel_form.action.value = 'update';
	submit_kernel_form();
}


function add_to_cart(item_id, item_type)
{
	document.kernel_form.item_id.value = item_id;
	document.kernel_form.item_type.value = item_type;
	document.kernel_form.action.value = 'add_to_cart';
	submit_kernel_form();
}

function del_from_cart() {
	if (count_selected() > 0) {
		if (confirm('Are you sure you want to remove all items?')) {
			document.kernel_form.action.value='delete_selected';
			submit_kernel_form();
		}
	}
	else 
		alert('Please select at least one item!');
}

function clear_cart() 
{
	if (confirm('Are you sure you want to remove selected items?')) {
		document.kernel_form.action.value='clear_cart';
		submit_kernel_form();
	}
}


function delete_item(item_id) {
	clear_selected();
	document.kernel_form.elements['item_'+item_id].checked = true;
	del_from_cart();
}

function update_shop_cart()
{
	document.kernel_form.action.value = 'update_cart';
	submit_kernel_form();
}

function login_shop_cart()
{
	document.kernel_form.action.value = 'login';
	submit_kernel_form();
}


function product_details(id,sid)
{
	norm_width = 630;
	norm_height = 520;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/product_item&item_id='+id+'&sid='+sid,'Product_details', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}
function auction_details(id,sid)
{
	norm_width = 630;
	norm_height = 520;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/auction_item&item_id='+id+'&sid='+sid,'Auction_Details', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function eclick(url)
{
	norm_width = 800;
	norm_height = 600;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(url,'pay_e_click', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}


function price_g_detail(id,sid)
{
	norm_width = 620;
	norm_height = 530;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=user_services/price_group_item&item_id='+id+'&sid='+sid, 'price_group', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function user_transactions(sid)
{
	norm_width = 620;
	norm_height = 530;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=user_services/transactions&sid='+sid,'transactions', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}


function products_list()
{
	document.kernel_form.t.value = 'online/products';
	document.kernel_form.submit();
}


var login='1';
function UserLogin()
{
	if (login=='1') {
		login='0';
		document.login.submit();
	}
}

function catchHotKeysA()
{
 if (!event) return;
 if (event.keyCode == 13)
	UserLogin();
}


function GoToCat(t,cat_id)
{
	document.kernel_form.t.value = t;
	document.kernel_form.cat_id.value = cat_id;
	document.kernel_form.page.value = 1;
	document.kernel_form.submit();

}

function open_notification(id,sid)
{
	norm_width = 600;
	norm_height = 500;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=user_services/notification_item&n_id='+id+'&sid='+sid,'Notification_message', 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function open_charitable(id,sid)
{
	norm_width = 600;
	norm_height = 500;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=user_services/charitable_item&pass_id='+id+'&sid='+sid,'charitable_organization', 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function print_demant(sid, id)
{
	norm_width = 600;
	norm_height = 500;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/print_demand&sid='+sid+'&invoices_pass_id='+id,'invoice_data', 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}


function cc_nr_fields(field_nr)
{
	if (field_nr == 1) {
		if (document.kernel_form.cc_nr_1.value.length == 4)
			document.kernel_form.cc_nr_2.focus();
	}
	if (field_nr == 2) {
		if (document.kernel_form.cc_nr_2.value.length == 4)
			document.kernel_form.cc_nr_3.focus();
	}
	if (field_nr == 3) {
		if (document.kernel_form.cc_nr_3.value.length == 4)
			document.kernel_form.cc_nr_4.focus();
	}
	document.kernel_form.cc_number.value=document.kernel_form.cc_nr_1.value + document.kernel_form.cc_nr_2.value + document.kernel_form.cc_nr_3.value + document.kernel_form.cc_nr_4.value;
		//alert(document.kernel_form.cc_number.value);
}

function preset_cc_nr()
{
	document.kernel_form.cc_nr_1.value = document.kernel_form.cc_number.value.slice(0,4);
	document.kernel_form.cc_nr_2.value = document.kernel_form.cc_number.value.slice(4,8);
	document.kernel_form.cc_nr_3.value = document.kernel_form.cc_number.value.slice(8,12);
	document.kernel_form.cc_nr_4.value = document.kernel_form.cc_number.value.slice(12,16);
}


function openwin(url,width,height)
{
	var scr_w = screen.width;
	var scr_h = screen.height;
	var left = ( scr_w - width ) / 2;
	var top =( scr_h - height ) / 2;
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + url, 'window', 'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=yes');
}

var as_home = 'no';
function billing_as_shipping(field)
{
	home = new Array('salutation','first_name','middle_name','last_name','org_name','addr1','addr2','addr3','city','state','postal_code','zip','country');
	bill = new Array('billing_salutation','billing_first_name','billing_middle_name','billing_last_name','billing_org_name','billing_addr1','billing_addr2','billing_addr3','billing_city','billing_state','billing_postal_code','billing_zip','billing_country');
	if (as_home == 'no' )
	{
		for ( i = 0; i < 13 ; i ++ ){
			//alert('processing '+bill[i]);
			document.kernel_form.elements[bill[i]].value = document.kernel_form.elements[home[i]].value;
			document.kernel_form.elements[bill[i]].disabled = true;
		}
		as_home = 'yes';
		field.checked = true;
		//alert(field.checked);
	}
	else
	{
		for ( i = 0; i < 13 ; i ++ ){
			document.kernel_form.elements[bill[i]].disabled = false;
		}
		as_home = 'no';
		field.checked = false;
	}
}

function open_print_form(sid)
{
	norm_width = 790;
	norm_height = 700;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/form_print&sid='+sid,'enrollment_form', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}


function open_enrollment_form(sid,service_type)
{
	norm_width = 790;
	norm_height = 700;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/form&action=new&sid='+sid+'&service_type='+service_type,'enrollment_form', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function openwin_simple(url,name,w,h) 
{
	x_pos = 20;
	y_pos = 20;
	window.open(url, name, 'resizable=yes,toolbar=no,location=no,scrollbars=yes,width='+w+',height='+h+',resizable=yes,top='+y_pos+',left='+x_pos);
}

function StatusInfo(sid)
{
	norm_width = 625;
	norm_height = 520;
	screen_x = (screen.availWidth-norm_width-20)/2;
	screen_y = (screen.availHeight-norm_height-20)/2;
	//alert(id);
	window.open(window.location.protocol + '//' + window.location.host + window.location.pathname + '?t=online/auction_statuses&sid='+sid,'auction_statuses', 'scrollbars=yes,width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function show_feedback(auction,type)
{
   		document.kernel_form.feedback_type.value = type;
   		document.kernel_form.auction_id.value = auction;
   		document.kernel_form.action.value = 'show_feedback';
	 		submit_kernel_form();
   		document.kernel_form.action.value = '';
}
