var select_objs=new Array(2);
var origin_list=new Array();
var bl_country="";
var pre_selected=[];
var btn_style="btn_secondary";
var max_city=100;

function QItem(bl_id, bl_name, bl_state, cpc, hi_budget, contract_value, url, item_status, bl_county) {
 this.bl_id=bl_id;
 this.bl_name=bl_name;
 this.bl_state=bl_state;
 this.cpc=cpc||0.0;
 this.hi_budget=hi_budget||0;
 this.contract_value=contract_value||0;
 this.url=url||"";
 this.item_status=item_status||0;
 this.bl_county=bl_county||"";
}
QItem.prototype.toString=function() {
 var the_str = "{bl_id:" + this.bl_id + ", bl_name:\"" + this.bl_name + "\", bl_state:\"" + this.bl_state + 
				 "\", item_status:" + this.item_status + ", bl_county:\"" + this.bl_county + "\"}";
 return the_str;
}

function submitBLReviewForm() {
 	  for (var i in origin_list) {
		var item=origin_list[i];
		if (item.item_status) {
		 var url_field=document.blForm.elements["quote_items." + item.bl_id + ":records"];
		 if(!validateUrl(url_field.value)) {
			 alert(url_msg);
			 url_field.focus();
			 return;
			 
		 } 
		}
	  }

  document.blForm.submit();
 
}
function submitBLOrderForm(){
  if(document.getElementById("q_total").innerHTML !=0) {
	   document.blForm.submit();
   } else {
	   alert("Please select at least one city.");
   }

}

function submitBLForm(the_action) {
  document.blForm.form_action.value=the_action;
  var selected=0;
  var bl_str="";

  for(var i=0; i<select_objs[1].options.length; i++) {
     var the_option=select_objs[1].options[i];
     if (the_option.value) { 
	  the_option.selected=true; 
	  selected++;
	  var item=origin_list["bl_" + the_option.value];
	  item.item_status=1;
	  bl_str += item.toString() + ",";
	 }
  }

  if (selected > max_city) { 
	 alert("You have selected " + selected + " cities. The maximum number of cities that can be selected is " + max_city + ".");
	 return;
  }
  
  if (bl_str) {
   bl_str = "[" + bl_str.substring(0,bl_str.length-1) + "]";
   document.blForm.bl_str.value=bl_str;
  }
  
  if(the_action=="state") {
     document.blForm.bl_county.selectedIndex=-1;
  } else if (!selected&&the_action=="search") {
	 document.blForm.form_action.value="";
	 alert("You must select at least one city to search for visits.  To see a list of cities, select a state and then a county.");
	 return;
  } 
 
 document.blForm.submit();
  
}

function changeBtnStyle(id,the_status) {
   document.getElementById("btn_" + id).className=btn_style + (the_status?the_status:"");
   if(!id) {
	   var other_id=1;
	   var the_sel=select_objs[0];
   } else {
	   var other_id=0;
	   var the_sel=select_objs[1];
   }
   if(!the_status) {
	   document.getElementById("btn_" + other_id).className=btn_style + " off";
   }
   for(var i=0; i<the_sel.length;i++) {
	   if(the_sel.options[i].selected) {
            the_sel.options[i].selected=false;
	   }
   }
}
 
function changeSelectedBL(the_status) {
  if(!the_status) {
   var the_sel = select_objs[1];
  } else {
   var the_sel = select_objs[0];
  }
  
  for(var i=0; i<the_sel.options.length; i++) {
    var the_option=the_sel.options[i];
    if(the_option.selected&&the_option.value) {
	  var item=origin_list["bl_" + the_option.value];
	  if(item.bl_county!=bl_county && !the_status) {
		  item.item_status=-1;
	  } else {
		  item.item_status=the_status;
	  }
	  origin_list["bl_" + the_option.value]=item;
	}
  }
  
  changeBtnStyle(the_status," off");
  displayBLDropdowns();
 
}

function sortBLCities(bl_1, bl_2){
    var x=bl_1.bl_name;
	var y=bl_2.bl_name;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function displayBLDropdowns() {
 select_objs[0].options.length = 0;
 select_objs[0].size=15;
 select_objs[1].options.length = 0;
 select_objs[1].size=15;

 var the_list=new Array();
 for (var i in origin_list) {
   the_list[the_list.length]=origin_list[i];
 }

 var sorted_list=the_list.sort((sortBLCities));
 
  for (var i in sorted_list) {
    var item=sorted_list[i];
	if (item.item_status!=-1) {
	 var the_sel = select_objs[item.item_status];
	 the_sel.options.length++;
	 var the_option=the_sel.options[the_sel.options.length-1];
	 the_option.value=item.bl_id;
	 the_option.text=item.bl_name + ", " + item.bl_state;
	}
  }
  
  //for (var i in [0,1]) {
   //var the_options = select_objs[i].options;
   //if (the_options.length == 0) {
     //the_options.length++;
	 //the_options[0].value="";
	 //the_options[0].text=(i==0?"All Cities":"Cities to Search");
	 //the_options[0].style.color="white";
   //}
  //}
 
   sorted_list=[];
  
}

function changeContractValue(id) {
 var the_sel=document.blForm.elements["quote_items." + id + ":records:int"];
 var cv=parseInt(the_sel.options[the_sel.selectedIndex].value);
 origin_list["bl_" + id].contract_value=cv;
 calculateBLTotal();
}

function calculateBLTotal() {
 var total=0;
 var visits=0;
 for (i in origin_list) {
   var item=origin_list[i];
   if (item.item_status && item.hi_budget) {
    if(typeof("calculated")=="undefined") {
	 cv=item.contract_value;
	} else {
	 var the_sel=document.blForm.elements["quote_items." + item.bl_id + ":records:int"];
	 var cv=parseInt(the_sel.options[the_sel.selectedIndex].value);
	}
	var visit=Math.ceil(cv/item.cpc);
	visits+=visit;
	document.getElementById("visit_" + item.bl_id).innerHTML=visit;
    total+=cv;
   }
 }
 document.getElementById("q_total").innerHTML=total;
 document.getElementById("q_visit").innerHTML=visits;
 calculated=1;
}

function toggleBLCheckBox(id) {
 var the_box=document.blForm.elements["checkbox_" + id];
 var the_sel=document.blForm.elements["quote_items." + id + ":records:int"];
 if(!the_box.checked) {
  origin_list["bl_" + id].item_status=0;
  the_sel.disabled=true;
  document.getElementById("r_" + id).className="row off";
 } else {
  origin_list["bl_" + id].item_status=1;
  the_sel.disabled=false;
  document.getElementById("r_" + id).className="row";
 }
 calculateBLTotal();
}

var url_msg="The web page URL is not valid.\n\n" + 
            ">> Must begin with http:// or https://\n" +
            ">> Must contain .com, .net, .biz, etc.\n\n" + 
            "Example:  http://www.myMLSwebsite.com\n\n" + 
            "Please fix the URL and try again."

function checkBLUrl(id) {
	var url_field=document.blForm.elements["quote_items." + id + ":records"];
	if (!validateUrl(url_field.value)) {
		alert(url_msg);
		url_field.focus();
	} else {
		newwin(url_field.value, 750, 600);
	}
}
