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,module_name){				
	var urlstr ;
    if(checked_bool == true ){
        urlstr = "/provider/search.php?module="+module_name+"&ajaxuncheck_id="+ id ; 
		makeRequest(urlstr);
  	}else{
	  	urlstr = "/provider/search.php?module="+module_name+"&ajaxcheck_id="+ id ; 
		makeRequest(urlstr);
  	}
}	
function Submitselected(){
	var checkbox_choices = 0;
	var appendString ="";
	var urlstr ;
	var checkBoxArr = getSelectedCheckbox(document.doctorSelectorForm.cb);
	var isUndefined = checkBoxArr.length ;
	if ( isUndefined == 1) {
		checkbox_choices = checkbox_choices + 1;
		appendString = appendString + getSelectedCheckboxValue(document.doctorSelectorForm.cb)+",";
	} else {
		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < document.doctorSelectorForm.cb.length; counter++) {
			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (document.doctorSelectorForm.cb[counter].checked) {
				checkbox_choices = checkbox_choices + 1;
				appendString = appendString + document.doctorSelectorForm.cb[counter].value+",";
			}
		}	
    		
		if (checkbox_choices > 3 ) {
			msg="You're limited to only three selections.\n";
			msg=msg + "You have made " + checkbox_choices + " selections.\n";
			msg=msg + "Please remove " + (checkbox_choices-3) + " selection(s).";
			alert(msg);
			return false;
		}
	}
	appendString = appendString + 0 ;
  
	urlstr = "/provider/search.php?ajax_ids="+ appendString; 
	makeRequest(urlstr);
}
			
function submitForm() {
	var temp ;
	var valid = "0123456789-";
	if(document.doctorFindForm.zip.value.length >= 1 ){
		var hyphencount = 0;
		if (document.doctorFindForm.zip.value.length!=5 ) {
			alert("Please enter your 5 digit zip code");
			document.doctorFindForm.zip.value="";
			document.doctorFindForm.zip.focus();
			return false;
		} else {
			for (var i=0; i < document.doctorFindForm.zip.value.length; i++) {
				temp = "" + document.doctorFindForm.zip.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") {
					alert("Invalid characters in your zip code.  Please try again.");
					document.doctorFindForm.zip.value="";
					document.doctorFindForm.zip.focus();				
					return false;
				}						
			}
		}
	}

	if(document.doctorFindForm.miles.value !=""){
		var filterhyphen = /-/;
		if (filterhyphen.test(document.doctorFindForm.miles.value)) {
			alert("Invalid characters in miles.  Please try again.");
			document.doctorFindForm.miles.focus();				
			return false;
		}
		
		for (var i=0; i < document.doctorFindForm.miles.value.length; i++) {
			temp = "" + document.doctorFindForm.miles.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in miles.  Please try again.");
				document.doctorFindForm.miles.value="";
				document.doctorFindForm.miles.focus();				
				return false;
			}						
		}
				
		if(document.doctorFindForm.zip.value == ""){
			alert("Please enter Zip Code, if you want to perform proximity searches.");
			document.doctorFindForm.zip.focus();
			return false ;
		}
	}
	var alphaRegxp = /[^a-zA-Z,"'",\-, ]/
	if(document.doctorFindForm.lastname.value!=""){		
		if((alphaRegxp.test(document.doctorFindForm.lastname.value))){
			alert("Please enter correct value for last name.\n");
			document.doctorFindForm.lastname.focus();
			return false;
		}
	}
	if(document.doctorFindForm.firstname.value!=""){	
		if((alphaRegxp.test(document.doctorFindForm.firstname.value))){
			alert("Please enter correct value for first name.\n");
			document.doctorFindForm.firstname.focus();
			return false;
		}
	}
	
	return true;
}


function checkMiles() {
	if(document.doctorFindForm.zip.value != ""){
			if(document.doctorFindForm.miles.value==""){
				document.doctorFindForm.miles.value = 5;
			}
	}
}

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

	if ( isUndefined == 1) {
		checkbox_choices = checkbox_choices + 1 + idsize;
		
		appendString = appendString + getSelectedCheckboxValue(document.doctorSelectorForm.cb)+",";
		if (checkbox_choices < 1 && idexists== 0  ) {
			msg="No plans selected. Please select at least one plan";
			alert(msg);
			return false;
		}

		if ( (checkbox_choices) > 3 ) {
			msg="You're limited to only three selections.\n"
			msg=msg + "You have made " + (checkbox_choices)  + " selections.\n"
			msg=msg + "Please remove " + (checkbox_choices - 3) + " selection(s)."
			msg=msg + "Would you like to remove these selections now?" 
			$confirm = confirm(msg);
			
			if($confirm == true){							
			}else{
				return false;
			}	
		}
	} else {
		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < document.doctorSelectorForm.cb.length; counter++) {
			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (document.doctorSelectorForm.cb[counter].checked) {
				checkbox_choices = checkbox_choices + 1;
				appendString = appendString + document.doctorSelectorForm.cb[counter].value+",";
			}
		}
		
		if ( (checkbox_choices + idsize) > 3 ) {
			msg="You're limited to only three selections.\n"
			msg=msg + "You have made " + (checkbox_choices + idsize)  + " selections.\n"
			msg=msg + "Please remove " + (checkbox_choices + idsize-3) + " selection(s).\n"
			msg=msg + "Would you like to remove these selections now?" 
			$confirm = confirm(msg);
			
			if($confirm == true){
						
			}else{
				return false;
			}	
		}
		
		if ((checkbox_choices + idsize) < 1 && idexists== 0 ) {
			msg="No doctors selected. Please select at least one "+module_name+"."
			alert(msg);
			return false;
		}
	}

	if(module_name=='doctors'){
		document.doctorSelectorForm.action="/provider/providercompare.php";
		document.doctorSelectorForm.module.value= "Doctors";
	}else{
		document.doctorSelectorForm.action="/provider/providercompare.php";
		document.doctorSelectorForm.module.value= "Dentists";
	}	

	document.doctorSelectorForm.doctorids.value=appendString+"0";
	document.doctorSelectorForm.method="POST";
	document.doctorSelectorForm.submit();
}

function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e || a[j]!="" || a[j]!=0)return true;
	return false
}

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

	var checkBoxArr = getSelectedCheckbox(document.doctorSelectorForm.cb);
	
	var isUndefined = checkBoxArr.length ;
	if ( isUndefined == 1) {
		checkbox_choices = checkbox_choices + 1;
		appendString = appendString + getSelectedCheckboxValue(document.doctorSelectorForm.cb)+",";
	} else {
		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < document.doctorSelectorForm.cb.length; counter++) {
			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (document.doctorSelectorForm.cb[counter].checked)
			{
				checkbox_choices = checkbox_choices + 1;

				appendString = appendString + document.doctorSelectorForm.cb[counter].value+",";
			}
		}	
    		
		if (checkbox_choices > 3 ) {
			msg="You're limited to only three selections.\n";
			msg=msg + "You have made " + checkbox_choices + " selections.\n";
			msg=msg + "Please remove " + (checkbox_choices-3) + " selection(s).";
			alert(msg);
			document.doctorSelectorForm.action="/provider/search.php";
			document.doctorSelectorForm.doctorids.value= appendString;
			document.doctorSelectorForm.offset.value= current_offset;
			document.doctorSelectorForm.method="GET";
			document.doctorSelectorForm.submit();
			return 0;
		}
	}
		
	appendString = appendString + 0 ;
	document.doctorSelectorForm.action="/provider/search.php";
	document.doctorSelectorForm.doctorids.value= appendString;
	document.doctorSelectorForm.offset.value= offset;
	document.doctorSelectorForm.method="GET";
	document.doctorSelectorForm.submit();
}

function removeFromCompareG(form_obj)
{	
	var obj = form_obj;
	var myString = obj.doctorids.value;
	var tokens = new Array();
    if(String.tokenize != null) {
		tokens = myString.tokenize(",", " ", true);

    }
	else{
		
		var filter = new RegExp(",","ig");
        myString = myString.replace(filter, "~");
		tokens = tokenize_forie5(myString,"~", " ", true);
	}
	
	appendString = "";
    for(var i=0; i<tokens.length; i++) {
        if (tokens[i]== obj.rdid.value)
            continue;
        if (tokens[i]==0)
            continue;
        appendString = appendString + tokens[i]+",";
    }

	
    appendString = appendString+"0";
	obj.action="/provider/providercompare.php";
    obj.doctorids.value = appendString;
	obj.method="POST";
	obj.submit();
}

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

function CheckEventDoctorLookup(e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	
	if (keycode == 13) {
		if(submitDoctorLookup()){
			return document.getElementById('doctorLookUpForm').submit();
		}
	}
}

function submitDoctorLookup() {
	var temp ;
	var valid = "0123456789-";
	
	if(document.doctorLookUpForm.lastname.value == ""){
		alert("Please enter last name for doctor.");
		document.doctorLookUpForm.lastname.focus();
		return false ;
	}
	
	if(document.doctorLookUpForm.zip.value.length >=1 && document.doctorLookUpForm.city.value != ""){
	alert("You can search either by city/state or zip/miles ");
	document.doctorLookUpForm.zip.value="";
	document.doctorLookUpForm.city.value="";
	return false ;
	}
	
	
	if(document.doctorLookUpForm.zip.value.length >= 1 ){
		var hyphencount = 0;
		if (document.doctorLookUpForm.zip.value.length!=5 ) {
			alert("Please enter your 5 digit zip code");
			document.doctorLookUpForm.zip.value="";
			document.doctorLookUpForm.zip.focus();
			return false;
		} else {
			for (var i=0; i < document.doctorLookUpForm.zip.value.length; i++) {
				temp = "" + document.doctorLookUpForm.zip.value.substring(i, i+1);
				if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") {
					alert("Invalid characters in your zip code.  Please try again.");
					document.doctorLookUpForm.zip.value="";
					document.doctorLookUpForm.zip.focus();				
					return false;
				}						
			}
		}
	}

	if(document.doctorLookUpForm.miles.value !=""){
		var filterhyphen = /-/;
		if (filterhyphen.test(document.doctorLookUpForm.miles.value)) {
			alert("Invalid characters in miles.  Please try again.");
			document.doctorLookUpForm.miles.focus();				
			return false;
		}
		
		for (var i=0; i < document.doctorLookUpForm.miles.value.length; i++) {
			temp = "" + document.doctorLookUpForm.miles.value.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in miles.  Please try again.");
				document.doctorLookUpForm.miles.value="";
				document.doctorLookUpForm.miles.focus();				
				return false;
			}						
		}
				
		if(document.doctorLookUpForm.zip.value == ""){
			alert("Please enter Zip Code, if you want to perform proximity searches.");
			document.doctorLookUpForm.zip.focus();
			return false ;
		}
	}
	
	if(document.doctorLookUpForm.city.value != "" && document.doctorLookUpForm.state.value =="" ){
		alert("Please select state ");
		return false ;
	}
	
	if(document.doctorLookUpForm.city.value == "" && document.doctorLookUpForm.state.value !="" ){
		alert("Please enter city ");
		document.doctorLookUpForm.city.focus();
		return false ;
	}
	
	
	return true;
}
