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 open_order(order_id, sid)
{
	norm_width = 500;
	norm_height = 450;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2
	window.open('index.php?t=my_account/order&order_id='+order_id+'&sid='+sid, 'order_'+order_id, 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;

function preloadImages() {
	if (document.images) {
		b1_over = newImage("img/overnight_serv_a.gif");
		b2_over = newImage("img/same_day_serv.gif");
		b3_over = newImage("img/consulting.gif");
		b4_over = newImage("img/place_order_a.gif");
		b5_over = newImage("img/personal_info_a.gif");
		b6_over = newImage("img/pickup_addr_a.gif");
		b7_over = newImage("img/orders_history_a.gif");
		b8_over = newImage("img/opportunites_a.gif");
		b9_over = newImage("img/profile_a.gif");
		b10_over = newImage("img/survey_a.gif");
		b11_over = newImage("img/edu_btn-a.gif");
		b12_over = newImage("img/chat_btn-a.gif");
		b13_over = newImage("img/rate_btn-a.gif");
		b14_over = newImage("img/affil_btn-a.gif");
		b15_over = newImage("img/resource_btn-a.gif");
		b16_over = newImage("img/report_btn-a.gif");
		b17_over = newImage("img/n_clients_a.gif");
		b18_over = newImage("img/n_contractors_a.gif");
		b19_over = newImage("img/n_myaccount_a.gif");
		b20_over = newImage("img/n_consultants_a.gif");						
		preloadFlag = true;
	}
}

function inverse_selected(obj, mask) {
	for (var i = 0; i < obj.elements.length; i++) {
		cur_name = obj.elements[i].name;
		if (cur_name.match(mask)) {
			obj.elements[i].checked = !obj.elements[i].checked
		}
	}
}

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

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

// invoices

function open_invoice(invoice_num, sid)
{
	norm_width = 700;
	norm_height = 600;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;	
	window.open('index.php?t=my_account/invoice_view&invoice_num='+invoice_num+'&sid='+sid, 'view_invoice_num_'+invoice_num, 'width='+norm_width+',height='+norm_height+',scrollbars=yes,resizable=yes,left='+screen_x+',top='+screen_y);
}

function print_invoice_pdf(invoice_num, sid)
{
	norm_width = 800;
	norm_height = 600;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2;	
	window.open('index.php?t=my_account/invoice_pdf&invoice_num='+invoice_num+'&sid='+sid, 'pdf_invoice_num_'+invoice_num, 'width='+norm_width+',height='+norm_height+',scrollbars=yes,resizable=yes,left='+screen_x+',top='+screen_y);
}

function do_select_invoices() {
	if ( get_first_selected(document.list, '^invoice_([0-9]+)') !== false) {	
		document.list.mask.value='^invoice_([0-9]+)';
		document.list.action.value='select_billed_invoices';
		document.list.submit();
	}
	else
		alert('Please select billed invoice(s) first');
}

function do_remove_invoices() {
	if ( get_first_selected(document.list, '^invoice_([0-9]+)') !== false) {	
		document.list.mask.value='^invoice_([0-9]+)';
		document.list.action.value='remove_billed_invoices';
		document.list.submit();
	}
	else
		alert('Please select billed invoice(s) first');
}

function do_select_all_invoices()
{
	inverse_selected(document.list, '^invoice_([0-9]+)');
	do_select_invoices();
}

function order_by(field, dir)
{
	if (dir == 'asc') f_dir='desc';
	if ((dir == 'desc') || (dir == 'no')) f_dir='asc';
	document.list.sort1.value=field;
	document.list.sort1_dir.value=f_dir;
	document.list.submit();
}

function OpenTransaction(payment_id, sid)
{
	norm_width = 680;
	norm_height = 600;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2 - 30;	
	window.open('index.php?t=my_account/payment_view&action=load&payment_id='+payment_id+'&sid='+sid, 'view_payment_id_'+payment_id, 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function PrintTransactionPDF(payment_id, sid)
{
	norm_width = 800;
	norm_height = 600;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2 - 30;	
	window.open('index.php?t=my_account/get_pdf&action=load&payment_id='+payment_id+'&sid='+sid, 'pdf_payment_id_'+payment_id, 'width='+norm_width+',height='+norm_height+',scrollbars=yes,resizable=yes,left='+screen_x+',top='+screen_y);
}

function show_calendar(sid, d_field)
{
	norm_width = 210;
	norm_height = 240;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2
	window.open('index.php?t=fedex/calendar&sid='+sid+'&d_field='+d_field, 'calendar', 'width=210,height=240,resizable=yes,status=no,scroll=no,left='+screen_x+',top='+screen_y);
}

function cart_action(action)
{
	document.edit.action.value=action; 
	document.edit.submit();	
}

function reset_cc(url)
{
	field1 = document.getElementById('last_cvv2');
	field2 = document.getElementById('new_cvv2');

	var o = document.edit;
	var field = Array('cc_number', 'cc_type', 'cc_expire_month', 'cc_expire_year');
	
	for ( i = 0; i < 4 ; i ++ ) {
		o.elements[field[i]].value = '';
		o.elements[field[i]].disabled = false;
	}
	
	field1.value = '';
	field2.value = '';
	field1.disabled = true;
	field2.disabled = false;

	window.location.href = url;
}

function dis_cvv2(_state)
{
	field1 = document.getElementById('last_cvv2');
	field2 = document.getElementById('new_cvv2');

	var state = _state == "" ? 1 : _state;
	
	if ( state == 2 ) {
		field1.disabled = false;
		field2.disabled = true;
	}
	else {
		field1.disabled = true;
		field2.disabled = false;		
	}
}

function UpdateOrder()
{
	document.order.action.value='update'; 
	document.order.submit();
}

function UpdateOrderStay(pop_type)
{
	document.order.pop_type.value=pop_type; 
	UpdateStay();
}

function UpdateStay()
{
	document.order.add_action.value='stay'; 
	UpdateOrder();
}

function UpdateServiceType(opt)
{
	document.order.add_action.value='stay';
	if ( opt.value == '92' ) {
		document.order.packaging_type.value = '01';
	}
	if ( opt.value == '90' ) {
		document.order.d_residential_address.checked = true;
	}	
	UpdateOrder();
}

function openwin_simple(url, name, width, height) 
{
	// alert('openwin_simple = url' + url + ' name ' + name + ' width ' + width + ' height ' + 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(url,name,'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=yes');	
}

function edit_p_address(sid)
{
	openwin_simple('index.php?t=fedex/edit_p_address&sid='+sid, 'edit_p_address', '700', '500' );
}

function find_location(sid)
{
	openwin_simple('index.php?t=fedex/fedex_location&sid='+sid, 'edit_location', '650', '600' );
}

function pop_up(template, sid, width, height )
{
	// alert('herr');
	openwin_simple('index.php?t='+template+'&sid='+sid, 'edit_location', width, height );
}

function open_forder(f_order_id, sid)
{
	openwin_simple('index.php?t=my_account/fedex_order_view&sid='+sid+'&f_order_id='+f_order_id, 'view_fedex_order', '760', '650' );
}

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

function SelectAddress(address_id)
{
	document.list.action.value = 'set_fedex_address'; 
	document.list.address_id.value = address_id; 
	document.list.submit();
}

function AddressSearch() 
{
	var str = document.order.search.value;
	if ( str != "" ) {
		// str_reg = new RegExp(str);
		var found_data = "";
		var found = 0;
		var cur_address = "";
		if ( str.length > 1 ) {
			for( address_id in add ) {
				cur_address = add[address_id];
				// str_reg.exec(cur_address)
				if ( cur_address.indexOf(str) > 0 ) {
					found++;
					found_data+="<tr><td id='address_id"+address_id+"' onmouseover='this.style.background=\"#CCCCCC\";' onmouseout='this.style.background=\"#FFFFFF\";' onclick=javascript:PostAddress("+address_id+");>"+found+". "+cur_address+"</td></tr>\n";
				}
			}
		}
		if ( found == 0 ) found_data = "<tr><td>No records were found</td></tr>";
		out = document.getElementById("search_result");
		out.style.display = '';
		found_data = "<table cellspacing=0 cellpadding=0>"+found_data+"</table>";
		out.innerHTML = found_data;
	}
}

function PostAddress(address_id)
{
	a_id = parent.document.getElementById("d_address_id");
	a_id.value = address_id; 
	// alert(" a_id.value " + a_id.value );
	parent.window.ChangeDeliveryAddress();
}

function getStates(f_name, country_field, state_field)
{
	var form_name = document.getElementById(f_name);
	c_value = form_name.elements[country_field].value;
	
	country = '';
	state	  = '';
	s = '';
	
	var initial_length = form_name.elements[state_field].options.length;
	
	for (var i = initial_length - 1; i >= 0; i--) 	// obnuljaet massiv optionsov
		form_name.elements[state_field].options[i] = null; 
	
	if (c_value == 'CA' || c_value == 'US') {
		form_name.elements[state_field].disabled = false;
		form_name.elements[state_field].options[0] = new Option('', ''); // add an new empty option

		for(country in countries) { // base countries list
			if (c_value == country) {
				i=1;
				for(state in countries[country]) { // base states list
					//s = s + " => "+  state +"\n";
					form_name.elements[state_field].options[i] = new Option(countries[country][state], state);
					i++;
				}
			}
		}					
		//alert("States array is: \n" + s);
	}
	else {
		form_name.elements[state_field].options[0] = new Option('', ''); // add an new empty option
		form_name.elements[state_field].disabled = true;
	}
}

function do_cvs() {	
	document.list.action.value='get_csv_import';
	document.list.submit();
}

function open_track(track, sid)
{
	norm_width = 700;
	norm_height = 450;
	screen_x = (screen.availWidth-norm_width)/2;
	screen_y = (screen.availHeight-norm_height)/2
	window.open('index.php?t=my_account/order_track&track='+track+'&sid='+sid, 'track_'+track, 'width='+norm_width+',height='+norm_height+',resizable=yes,left='+screen_x+',top='+screen_y);
}

function is_msie()
{
	var browser = "";
	browser = navigator.appName;	
	if (browser == 'Microsoft Internet Explorer')
		return 1;
	else
		return 0;	
}

function showsubm(submenu)
{	
	document.getElementById(submenu).style.display = 'inline';
}

function hidesubm(submenu)
{
	document.getElementById(submenu).style.display = 'none';
}