function call_url(url, element) {
	var req;
	var data;
	
 	var msxmlhttp = new Array(
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) {
		try {
			req = new ActiveXObject(msxmlhttp[i]);
		} catch (e) {
			req = null;
		}
	}
 			
	if(!req && typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req)
		return;
	
	req.onreadystatechange = function() { 
		if(req.readyState == 4) {
			if(req.status == 200) {
				data = req.responseText.replace(/^\s*|\s*$/g,"");
				if (element) {
					document.getElementById(element).innerHTML = eval(data);
				} else {
					eval(data);
				}
			} else {
				return;
			}
		}
	};
	req.open("GET", url, true);
	req.send(null);
	return true;
}

function attach_file(p_script_url) {
	script = document.createElement('script');
	script.src = p_script_url;
	document.getElementsByTagName('head')[0].appendChild(script);
}

function make_active(tab) { 
	document.getElementById("tab1").className = ""; 
	document.getElementById("tab2").className = ""; 
	document.getElementById("tab3").className = ""; 	
	document.getElementById("tab"+tab).className = "selected"; 
	
	document.getElementById("cont_data_1").style.display = "none"; 
	document.getElementById("cont_data_2").style.display = "none"; 
	document.getElementById("cont_data_3").style.display = "none"; 	
	document.getElementById("cont_data_"+tab).style.display = "block"; 
	if (tab == 1) {
		obj_series_id_list = document.getElementById('series_id_list');
		display_series(obj_series_id_list.options[obj_series_id_list.options.selectedIndex].value);
		document.getElementById("subtabs").style.display = "none"; 
		document.getElementById("reload_btn").style.visibility = "hidden";
		document.getElementById("reload_btn2").style.visibility = "hidden";
	} 
	
	if (tab == 2) {
		document.getElementById("subtabs").style.display = "block"; 
		document.getElementById("reload_btn").style.visibility = "visible";
		document.getElementById("reload_btn2").style.visibility = "hidden";
		if (document.getElementById('link_pr_frt').className == 'selected') {
			bucket_type = 'forecast';
		} else {
			bucket_type = 'market';
		}
		call_url('refresh_map.php?type=' + bucket_type);
	}
	if (tab == 3) {
		document.getElementById("subtabs").style.display = "none";
		document.getElementById("reload_btn").style.visibility = "hidden";
		document.getElementById("reload_btn2").style.visibility = "visible";
		call_url('refresh_map_world.php');
	}	
}


function make_active_acc_tab(tab) { 
	document.getElementById("acc_tab_1").className = ""; 
	document.getElementById("acc_tab_2").className = ""; 
	document.getElementById("acc_tab_3").className = ""; 	
	document.getElementById("acc_tab_4").className = ""; 	
	document.getElementById("acc_tab_"+tab).className = "selected"; 
	
	document.getElementById("cont_acc_1").style.display = "none"; 
	document.getElementById("cont_acc_2").style.display = "none"; 
	document.getElementById("cont_acc_3").style.display = "none";
	document.getElementById("cont_acc_4").style.display = "none"; 
	document.getElementById("cont_acc_"+tab).style.display = "block"; 
}

function view_forecast_based_probs(){
	fmInitialView(); 
	fmThemeReloadAreas('fm-us/counties.php');
	fmReloadPOIs('fm-us/pois.php');
	document.getElementById('link_pr_mkt').className = '';
	document.getElementById('link_pr_frt').className = 'selected';
	call_url('refresh_map_legend.php?type=forecast');
	
}

function view_market_based_probs(){
	fmInitialView();
	fmThemeReloadAreas('fm-us/counties_markets.php');
	fmReloadPOIs('fm-us/pois.php');
	document.getElementById('link_pr_mkt').className = 'selected';
	document.getElementById('link_pr_frt').className = '';
	call_url('refresh_map_legend.php?type=market');
}

function switch_ownership_dot(_switch){
	call_url('ajax_ownership_dot.php?switch=' + _switch);
	
}
function display_series(series_id) {
	series_id = (series_id) ? series_id : '';
	obj_series_pot_container = document.getElementById('series_pot');
	obj_series_pot_container.innerHTML = '';
	obj_container = document.getElementById('prob_data');
	obj_container.innerHTML = '<br /><img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> <small class=""txt_gray>Loading...</small>';
	call_url('populate_opt_probabilities.php?series_id=' + series_id, '');
}

function display_option_menu(series_id) {
	series_id = (series_id) ? series_id : '';
	call_url('populate_main_opt_menu.php?series_id=' + series_id, '');
}



function display_forecast() {
	obj_forecast_data = document.getElementById('forecast_data');
	obj_forecast_data.innerHTML = '<div style="margin:10px 4px;"><img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> <small class=""txt_gray>Loading...</small></div>';
	obj_forecast_data_desc = document.getElementById('forecast_data_desc');
	obj_forecast_data_desc.innerHTML = '';
	call_url('populate_forecast_data.php');
}

function display_storm(named_id) {
	call_url('ajax_set_storm.php?active_storm=' + named_id);
	obj_container = document.getElementById('prob_data');
	obj_container.innerHTML = '<br /><img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> <small class=""txt_gray>Loading...</small>';
	call_url('populate_opt_probabilities.php');
}

function set_storm(named_id) {
	call_url('ajax_set_storm.php?active_storm=' + named_id + '&x=1');
}

function register_click(link_id) {
	link_id = (link_id) ? link_id : '';
	call_url('ajax_register_click.php?link_id=' + link_id);
}

function set_purchase(region_id) {
	//alert(region_id);
	document.form_purchaseoption1.county_id_1.options.selectedIndex = region_id;
	document.form_purchaseoption1.num_options.focus();
}

function set_order_book_purchase(region_id) {
	//alert(region_id);
	document.form_purchaseoption1.county_id.options.selectedIndex = region_id;
	document.form_purchaseoption1.num_options.focus();
}

function get_option_price(num_options, series_id, region_id) {
	if (num_options) {
		obj_container = document.getElementById('cont_current_price');
		obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  />';
		call_url('populate_current_price.php?num_options=' + num_options + '&series_id=' + series_id + '&region_id=' + region_id, '');
	}
}

function get_bid_highest_price(series_id, region_id) {
	obj_container = document.getElementById('cont_bid_price');
	obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  />';
	call_url('populate_order_book_price.php?type=bid&series_id=' + series_id + '&region_id=' + region_id, '');
}

function get_ask_lowest_price(series_id, region_id) {
	obj_container = document.getElementById('cont_ask_price');
	obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  />';
	call_url('populate_order_book_price.php?type=ask&series_id=' + series_id + '&region_id=' + region_id, '');
}



function load_series(obj) {
	if(typeof(opt_timer) != "undefined") {
		clearTimeout(opt_timer);
	}
	series_id = obj.options[obj.selectedIndex].value;
	//alert(series_id);
	if(series_id != 0) {
		display_series(series_id);
		display_option_menu(series_id);
		call_url('populate_forecast_data.php?series_id=' + series_id);
		if (document.getElementById('link_pr_mkt').className == 'selected') {
			call_url('refresh_map_legend.php?type=market&series_id=' + series_id);
		} else {
			call_url('refresh_map_legend.php?type=forecast&series_id=' + series_id);
		}
	}
}

function refresh_map() {
	call_url('refresh_map.php');
	setTimeout("refresh_map()", timer);
}


function confirm_this(msg) {
	var res = confirm(msg);
	if(res) {
		return true;
	} else {
		return false;	
	}
}

function confirm_cancel(msg, url) {
	var res = confirm(msg);
	if(res) {
		window.location = url;
	} else {
		return false;	
	}
}


function confirm_cancel_log(msg, url) {
	var res = confirm(msg);
	if(res) {
		obj_container = document.getElementById('cont_loading');
		obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> Canceling...';
		call_url('ajax_log_purchase_cancel.php?url=' + url);
	} else {
		return false;	
	}
}

function load_past_storm_by_year(obj) {
	year = obj.options[obj.selectedIndex].value;
	// remove existing content of the storm dropdown
	document.form_storm.named_id.options.length = 0;
	document.getElementById('loading').innerHTML = 'Populating....';

	call_url('ajax_load_past_storm_by_year.php?year=' + year);
}

function load_past_advisories_by_named_id(obj) {
	named_id = obj.options[obj.selectedIndex].value;
	obj_container = document.getElementById('block_advisories');
	obj_container.innerHTML = '<br /><img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> <small class=""txt_gray>Loading...</small>';
	call_url('ajax_load_past_advisories_by_named_id.php?named_id=' + named_id);
}

function display_loader(cont_loader, msg) {
	obj_container = document.getElementById(cont_loader);
	obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  /> ' + msg;
}



function check_order_confirmation() {
	obj_check_box = document.getElementById('agree').checked;
	if (obj_check_box == false) {
		alert('You must agree and check the checkbox before proceeding');
		call_url('ajax_log_confirm_agree_error.php');
		return false;
	} else {
		document.getElementById('btn_submit').disabled = 'disabled';
		return true;
	}
}

function popup_calculator() {
	series_id=document.getElementById('series_id_input').value;
	county_id=document.getElementById('form_county_id').value;
	num_purchase=document.getElementById('form_num_options').value;
	 
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	var seconds = currentTime.getSeconds()

	var this_var = 'w' + hours + minutes + seconds;
	window.open('limit_price_calculator.php?series_id='+series_id+'&county_id='+county_id+'&num_purchase='+num_purchase+'',this_var,'toolbar=no,width=550,height=400');
	//this_var.focus();
}

function calculate_req_napl(napl) {
	withdrawal = document.getElementById('withdrawal_amount').value;
	req_napl_container = document.getElementById('req_napl');
	if (withdrawal > napl) {
		alert('Amount should not be greater than current net authorized purchase limit');
		req_napl_container.innerHTML = '';
		document.getElementById('withdrawal_amount').value = '';
	} else {
		req_napl = napl - withdrawal;
		req_napl_container.innerHTML =  '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  />';
		call_url('calculate_req_napl.php?napl=' + napl + '&withdrawal=' + withdrawal);

	}
}

function change_process_buttons() {
	obj_check_box = document.getElementById('agree').checked;
	if (obj_check_box == true) {
		document.getElementById('btn_quote_cancel').className = 'btn_quote_cancel_checked';
		document.getElementById('btn_submit').className = 'btn_quote_order_checked';
	} else {
		document.getElementById('btn_quote_cancel').className = 'btn_quote_cancel';
		document.getElementById('btn_submit').className = 'btn_quote_order';
	}
}

function change_bid_process_buttons() {
	obj_check_box = document.getElementById('agree').checked;
	if (obj_check_box == true) {
		document.getElementById('btn_bid_cancel').className = 'btn_bid_cancel_checked';
		document.getElementById('btn_submit').className = 'btn_place_bid_checked';
	} else {
		document.getElementById('btn_bid_cancel').className = 'btn_bid_cancel';
		document.getElementById('btn_submit').className = 'btn_place_bid';
	}
}

function change_ask_process_buttons() {
	obj_check_box = document.getElementById('agree').checked;
	if (obj_check_box == true) {
		document.getElementById('btn_ask_cancel').className = 'btn_ask_cancel_checked';
		document.getElementById('btn_submit').className = 'btn_place_ask_checked';
	} else {
		document.getElementById('btn_ask_cancel').className = 'btn_ask_cancel';
		document.getElementById('btn_submit').className = 'btn_place_ask';
	}
}

function popup_focus(file, width, height) {
	var wp=window.open(file,'LimitCalculator','toolbar=no,width=' + width + ',height=' + height);
	wp.focus();
}

// check NUMBER
function isNumber(obj,msg){
  var strTmp = obj.value;
  var ValidChars = "0123456789";

  //Check Validity
  for (i=0; i<=strTmp.length-1; i++) 
    if (ValidChars.indexOf(strTmp.charAt(i)) == -1){
	  alert(msg)
      obj.focus()
      return false;
	}
  return true;
}	

//check if empty
function notEmpty(obj,msg){   
	var mystr = ""
	len = obj.value.length
	if (len > 0){  
		for (i=0;i<len; i++){
			if(obj.value.substring(i,i+1)!=" "){ 
				mystr = mystr + obj.value.substring(i,i+1);
			}
		}	
	}
	if(mystr==""){
		alert(msg);
		if(obj.name !='rte1'){
			if(obj.name!='txt_total'){
				obj.focus();
			}
		}
		return false;
	}
 return true;	
}


function is_number_key(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
		
	return true;
}

function is_float_key(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (((charCode > 47 && charCode < 58) || charCode == 46 || charCode < 32))
		return true;
		
	return false;
}

function add_commas(str) {
	str += '';
	x = str.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


//check form_purchaseoption1
function check_form_purchaseoption1(obj){
	if(notEmpty(obj.numberofoptions,"Please enter the number of options.") == false ||
		isNumber(obj.numberofoptions,"This field must be a valid number.") == false){
		return false;
	}else{
		return true;
	}		
}

//check form_purchaseoption2
function check_form_purchaseoption2(obj){
	if(notEmpty(obj.totaldollaramount,"Please enter the total dollar amount.") == false ||
		isNumber(obj.totaldollaramount,"This field must be a valid number.") == false){
		return false;
	}else{
		return true;
	}		
}

//popup
function popup_pic(sPicURL) { 
	window.open("dsp_pic_popup.php?"+sPicURL, "ViewImage",  "resizable=1,HEIGHT=200,WIDTH=200"); 
}

function display_element(element, switch_) {
	if (switch_ == 0) {
		document.getElementById(element).style.display = 'none';
	} else {
		document.getElementById(element).style.display = 'block';
	}
}

function get_damage_threshold(region_id) {

	obj_container = document.getElementById('cont_damage_threshold');
	obj_container.innerHTML = '<img src="images/ajax-loader-sm.gif" width="18" height="18" align="absmiddle"  />';
	call_url('populate_damage_threshold.php?region_id=' + region_id, '');

}
