function makeRequest(url) {

        http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = alertContents;
        http_request.open('GET', url, true);
        http_request.send(null);

}

function alertContents() {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            //alert(http_request.responseText);
        } else {
            alert('There was a problem with the request.');
        }
    }
}	

function check(isUndefined){
	var browser=navigator.appName;
	var version=navigator.appVersion;

	if (browser.indexOf("Microsoft") >= 0) {
	    if (version.indexOf("MSIE 5.") >= 0) {
	    	if (isUndefined === '0') {
				return true;
			} else {
				return false;
			}
        } else {
			if (isUndefined==undefined) return true;
			else return false;
		}
	} else {
	    if (isUndefined==undefined) return true;
		else return false;
	}
}

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
}

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
}

function handleCB(id, checked_bool){				
	var urlstr ;
    if(checked_bool == true ){
        urlstr = "pricingsearch.php?ajaxuncheck_id="+ id ; 
		makeRequest(urlstr);
  	}
}
		
function Submitselected(){
	var checkbox_choices = 0;
	var appendString ="";
	var urlstr ;
	var checkBoxArr = getSelectedCheckbox(document.pricingSelectorForm.cb);
	var isUndefined = checkBoxArr.length ;
	if ( isUndefined == 1) {
		checkbox_choices = checkbox_choices + 1;
		appendString = appendString + getSelectedCheckboxValue(document.pricingSelectorForm.cb)+",";
	} else {
		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < document.pricingSelectorForm.cb.length; counter++) {
			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (document.pricingSelectorForm.cb[counter].checked) {
				checkbox_choices = checkbox_choices + 1;
				appendString = appendString + document.pricingSelectorForm.cb[counter].value+",";
			}
		}	
    		
		if (checkbox_choices > 5 ) {
			msg="You're limited to only five selections.\n";
			msg=msg + "You have made " + checkbox_choices + " selections.\n";
			msg=msg + "Please remove " + (checkbox_choices-5) + " selection(s).";
			alert(msg);
			return false;
		}
	}
	appendString = appendString + 0 ;
  
	urlstr = "pricingsearch.php?ajax_ids="+ appendString; 
	makeRequest(urlstr);
}
			
function submitForm() {
	var temp ;
	var valid = "0123456789-";
	
	if(document.getElementById('pricingFindForm_zip').value == ""){
		alert("Please enter Zip Code.");
		document.getElementById('pricingFindForm_zip').focus();
		return false ;
	}

	if (document.getElementById('pricingFindForm_zip').value.length >= 1) {
		var hyphencount = 0;
		if (document.getElementById('pricingFindForm_zip').value.length!=5 ) {
			alert("Please enter your 5 digit zip code");
			document.getElementById('pricingFindForm_zip').value="";
			document.getElementById('pricingFindForm_zip').focus();
			return false;
		} else {
			if (!document.getElementById('pricingFindForm_zip').value.match(/^[0-9]{5}$/)) {
				alert("Invalid characters in your zip code. Please try again.");
				document.getElementById('pricingFindForm_zip').focus();
				return false;
			}
		}
	}
		
	if(document.getElementById('pricingFindForm_miles').value !=""){
		var filterhyphen = /-/;
		if (filterhyphen.test(document.getElementById('pricingFindForm_miles').value)) {
			alert("Invalid characters in miles.  Please try again.");
			document.getElementById('pricingFindForm_miles').focus();				
			return 100;
		}
		
		for (var i=0; i < document.getElementById('pricingFindForm_miles').value.length; i++) {
			temp = "" + document.getElementById('pricingFindForm_miles').value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in miles.  Please try again.");
				document.getElementById('pricingFindForm_miles').value="";
				document.getElementById('pricingFindForm_miles').focus();				
				return false;
				break;
			}						
		}
	}
	
	return true;
}

function CheckEvent(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	
	if (keycode == 13) {
		if(submitForm()){
			return document.getElementById('pricingFindForm').submit();
		}
	}
}

function checkMiles() {
	if(document.getElementById('pricingFindForm').zip.value != ""){
			if(document.getElementById('pricingFindForm').miles.value==""){
				document.getElementById('pricingFindForm').miles.value = 100;
			}
	}
}

function checkDRG() {
	var box = document.getElementById('pricingFindForm').drg;
	if (box.selectedIndex == -1) {
		alert('Please select a procedure first.');
		return false;
	}
	return true;
}

function compare(idexists) {
	// set var checkbox_choices to zero
	var idexists = idexists ;
	var checkbox_choices = 0;
	appendString ="";
	var checkBoxArr = getSelectedCheckbox(document.pricingSelectorForm.cb);
	var isUndefined = checkBoxArr.length ;

	if ( isUndefined == 1) {
		checkbox_choices = checkbox_choices + 1;
		appendString = appendString + getSelectedCheckboxValue(document.pricingSelectorForm.cb)+",";
		if (checkbox_choices < 1 && idexists== 0  ) {
			msg="No plans selected. Please select at least one plan";
			alert(msg);
			return false;
		}
	} else {
		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < document.pricingSelectorForm.cb.length; counter++) {
			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (document.pricingSelectorForm.cb[counter].checked) {
				checkbox_choices = checkbox_choices + 1;
				appendString = appendString + document.pricingSelectorForm.cb[counter].value+",";
			}
		}

		if (checkbox_choices > 5 ) {
			msg="You're limited to only five selections.\n"
			msg=msg + "You have made " + checkbox_choices + " selections.\n"
			msg=msg + "Please remove " + (checkbox_choices-5) + " selection(s)."
			alert(msg);
		
			return false;
		}
		
		if (checkbox_choices < 1 && idexists== 0 ) {
			msg="No pricings selected. Please select at least one pricing."
			alert(msg);
			return false;
		}
	}

	document.pricingSelectorForm.action="pricingcompare.php";
	document.pricingSelectorForm.pricingids.value=appendString+"0";
	document.pricingSelectorForm.method="POST";
	document.pricingSelectorForm.submit();
}

function Submitselected1(offset,current_offset){
	
	var checkbox_choices = 0;
	var appendString ="";
	var urlstr ;

	document.pricingSelectorForm.action="pricingsearch.php";
	document.pricingSelectorForm.offset.value= offset;
	document.pricingSelectorForm.method="GET";
	document.pricingSelectorForm.submit();
}
