// This part changes per your application

function handleResponse() {

	//document.getElementById('status').innerHTML = 'ajax http.readyState= ' + http.readyState;

    if (http.readyState == 4) {

        var response = http.responseText;

        var update = new Array();

		var divid = 'foo';
		
		document.getElementById('foo').innerHTML = 'ajax response = ' + response;

		// window.alert("resp: " + response);

        if(response.indexOf('|' != -1)) {

            update = response.split('|');

			//document.getElementById('status').innerHTML = 'ajax updating available zipcodes!';

			//window.alert("response...");

			var oSel = document.getElementById('zips_near');

			while ( 0 < oSel.length )

				oSel.remove(oSel.length-1);
				
			for( var n = 0; n<update.length-1; n++ ) {

				//document.getElementById('status').innerHTML = 'ajax add selection ' + update[n] + '...';

				var opt =document.createElement('option');

				opt.text = update[n];

				//window.alert("opt: " + opt.text);

				try {

					oSel.add(opt,null);}

				 catch(ex) {

				    oSel.add(opt); } // IE requires this, otherwise a type mismatch error occurs!

			}

 			//document.getElementById('status').innerHTML = 'ajax request done!';

      }

    }

}



function RadiusSelect( o ) {
	var zipstates=document.userInfo.state_selected	
	if(zipstates.length==0){
		
		alert("Please select the Service area State first");
		return false;
	}
	else{
	
	var oDivZip = document.getElementById('zipdiv');

	var oZip = document.getElementById('zipcode');

	var oSel = document.getElementById('zips_near');

	var oSel2 = document.getElementById('zips_selected');

	var myzip = oZip.value;
	
	statezip= new Array();
	
	for(i=0;i<zipstates.length;i++){
		statezip[i] = zipstates[i].text;
	}
	
	statezip=statezip.join(",");
	
	//window.alert("RadiusSelect");

	while ( 0 < oSel.length )

		oSel.remove(oSel.length-1);



	// leave selection boxes alone if no zip entered in text box

	if ( "" != myzip )

		while ( 0 < oSel2.length )

			oSel2.remove(oSel2.length-1);

			

	ZipDivUpdate();  // make sure the hidden values are also cleared

	sndReq( oSel2.value,statezip );
	
	}	

	// the ajax functions above do the rest of the work

}



function ZipSelectAll() {

	//window.alert("ZipSelectAll()");

	var oDivZip = document.getElementById('zipdiv');

	var oSel = document.getElementById('zips_near');

	var oSel2 = document.getElementById('zips_selected');



	var i = 0;

	if ( null != oSel ) {

		while ( 0 < oSel.length) {

			var opt =document.createElement('option');

			opt.text = oSel.options[0].text;

			try {oSel2.add(opt,null); } catch (ex) { oSel2.add(opt); }

			oSel.remove(0);

		}

		ZipDivUpdate();

	}

}



function ZipSelectSome() {

	//window.alert("ZipSelectSome()");

	var oDivZip = document.getElementById('zipdiv');

	var oSel = document.getElementById('zips_near');

	var oSel2 = document.getElementById('zips_selected');

	var i = 0;
	

	if ( null != oSel ) {

		while( -1 != ( i = oSel.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel.options[i].text;
			
			for (var s=0;s<(oSel2.options.length);s++){
				if(String(oSel2.options[s].text)==String(oSel.options[i].text)){
					alert("Zip already added in selected list");
					return false;				
				}
			}
			
			try { oSel2.add(opt,null);} catch(ex) {oSel2.add(opt);}

			oSel.remove(i);

		}

	ZipDivUpdate();

	}

}



function ZipdeSelectAll() {

	//window.alert("ZipdeSelectAll()");

	var oDivZip = document.getElementById('zipdiv');

	var oSel = document.getElementById('zips_near');

	var oSel2 = document.getElementById('zips_selected');

	var s = "";

	if ( null != oSel ) {
		
		oSel2.options.length=0;

		while ( 0 < oSel2.length) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[0].text;

			try {oSel.add(opt,null); } catch (ex) { oSel.add(opt); }

			oSel2.remove(0);

		}

	ZipDivUpdate();

	}

}



function ZipdeSelectSome() {

	//window.alert("ZipdeSelectSome()");

	var oDivZip = document.getElementById('zipdiv');

	var oSel = document.getElementById('zips_near');

	var oSel2 = document.getElementById('zips_selected');

	if ( null != oSel ) {

		while( -1 != ( i = oSel2.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[i].text;
			
			for (var s=0;s<(oSel.options.length);s++){
				if(String(oSel.options[s].text)==String(opt.text)){
					oSel2.remove(i);
					return false;
				}
			}

			try { oSel.add(opt,null);} catch(ex) {oSel.add(opt);}

			oSel2.remove(i);

		}

	ZipDivUpdate();

	}

}



// from w3schools  http://w3schools.com/js/js_cookies.asp

function setCookie(c_name,value,expiredays) {

	var exdate=new Date();

	exdate.setDate(exdate.getDate()+expiredays);

	document.cookie=c_name+ "=" + value +

		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

}



function ZipDivUpdate() {

	// fill in the hidden elements that are used to POST the selected zipcodes

	var oDivZip = document.getElementById('zipdiv');

	var oSel2 = document.getElementById('zips_selected');

	var s = "";

	// window.alert( "oSel2.length = " + oSel2.length );

	for ( var i = 0; i < oSel2.length; i++ )

		s += "<input type='hidden' name=zips[" + i  + "] value='" + oSel2.options[i].text + "' />\n";

	oDivZip.innerHTML = s;

	

	// now update my cookie, so it can be used my the master form when it is submitted

	// this allows my form to work inside an iframe on the master form

	var c = "";

	for ( var i = 0; i < oSel2.length; i++ ) {

		if ( 0 < i )

			c+= "|";

		c+= oSel2.options[i].text;

		}

	//window.alert("setting cookie! -> " + c);

	setCookie('zipwithindist',c,1);  

	

}



function StateSelectAll() {

	//window.alert("ZipSelectAll()");

	var oDivZip = document.getElementById('statediv');

	var oSel = document.getElementById('state_near');

	var oSel2 = document.getElementById('state_selected');



	var i = 0;

	if ( null != oSel ) {
		
		oSel2.options.length=0;

		while ( 0 < oSel.length) {

			var opt =document.createElement('option');

			opt.text = oSel.options[0].text;

			try {oSel2.add(opt,null); } catch (ex) { oSel2.add(opt); }

			oSel.remove(0);

		}

		StateDivUpdate();

	}

}



function StateSelectSome() {

	//window.alert("ZipSelectSome()");

	var oDivZip = document.getElementById('statediv');

	var oSel = document.getElementById('state_near');

	var oSel2 = document.getElementById('state_selected');

	var i = 0;

	if ( null != oSel ) {

		while( -1 != ( i = oSel.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel.options[i].text;
			
			for (var s=0;s<(oSel2.options.length);s++){
				if(String(oSel2.options[s].text)==String(oSel.options[i].text)){
					alert("State already added in selected list");
					return false;				
				}
			}

			try { oSel2.add(opt,null);} catch(ex) {oSel2.add(opt);}

			oSel.remove(i);

		}

	StateDivUpdate();

	}

}



function StatedeSelectAll() {

	//window.alert("ZipdeSelectAll()");

	var oDivZip = document.getElementById('statediv');

	var oSel = document.getElementById('state_near');

	var oSel2 = document.getElementById('state_selected');

	var s = "";

	if ( null != oSel ) {

		while ( 0 < oSel2.length) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[0].text;

			try {oSel.add(opt,null); } catch (ex) { oSel.add(opt); }

			oSel2.remove(0);

		}

	StateDivUpdate();

	}

}



function StatedeSelectSome() {

	//window.alert("ZipdeSelectSome()");

	var oDivZip = document.getElementById('statediv');

	var oSel = document.getElementById('state_near');

	var oSel2 = document.getElementById('state_selected');

	if ( null != oSel ) {

		while( -1 != ( i = oSel2.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[i].text;
			
			for (var s=0;s<(oSel.options.length);s++){
				if(String(oSel.options[s].text)==String(opt.text)){
					oSel2.remove(i);
					return false;
				}
			}

			try { oSel.add(opt,null);} catch(ex) {oSel.add(opt);}

			oSel2.remove(i);

		}

	StateDivUpdate();

	}

}


function StateDivUpdate() {

	// fill in the hidden elements that are used to POST the selected zipcodes

	var oDivZip = document.getElementById('statediv');

	var oSel2 = document.getElementById('state_selected');

	var s = "";

	// window.alert( "oSel2.length = " + oSel2.length );

	for ( var i = 0; i < oSel2.length; i++ )

		s += "<input type='hidden' name=zipstate[" + i  + "] value='" + oSel2.options[i].text + "' />\n";

	oDivZip.innerHTML = s;

	

	// now update my cookie, so it can be used my the master form when it is submitted

	// this allows my form to work inside an iframe on the master form

	var c = "";

	for ( var i = 0; i < oSel2.length; i++ ) {

		if ( 0 < i )

			c+= "|";

		c+= oSel2.options[i].text;

		}

	//window.alert("setting cookie! -> " + c);

	setCookie('zipstates',c,1);
}


function contractorSelectAll() {

	//window.alert("ZipSelectAll()");

	var oDivZip = document.getElementById('contractordiv');

	var oSel = document.getElementById('contractortype');

	var oSel2 = document.getElementById('contractor_selected');



	var i = 0;
	var a = 0;

	if ( null != oSel ) {
		
		oSel2.options.length=0;

		while ( 0 < oSel.length) {

			var opt =document.createElement('option');

			opt.text = oSel.options[0].text;
			opt.value = oSel.options[0].value;
					
			try {oSel2.add(opt,null); } catch (ex) { oSel2.add(opt); }
			oSel.remove(0);

		}

		contractorDivUpdate();


	}

}



function contractorSelectSome() {

	//window.alert("ZipSelectSome()");

	var oDivZip = document.getElementById('contractordiv');

	var oSel = document.getElementById('contractortype');

	var oSel2 = document.getElementById('contractor_selected');

	var i = 0;

	if ( null != oSel ) {

		while( -1 != ( i = oSel.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel.options[i].text;
			opt.value = oSel.options[i].value;
			
			for (var s=0;s<(oSel2.options.length);s++){
				if(String(oSel2.options[s].text)==String(oSel.options[i].text)){
					alert("Contractor already added in selected list");
					return false;				
				}
			}
			
			try { oSel2.add(opt,null);} catch(ex) {oSel2.add(opt);}
			oSel.remove(i);
			
		}

	contractorDivUpdate();


	}

}



function contractordeSelectAll() {

	//window.alert("ZipdeSelectAll()");

	var oDivZip = document.getElementById('contractordiv');

	var oSel = document.getElementById('contractortype');

	var oSel2 = document.getElementById('contractor_selected');

	var s = "";

	if ( null != oSel ) {

		while ( 0 < oSel2.length) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[0].text;
			opt.value = oSel2.options[0].value;

			try {oSel.add(opt,null); } catch (ex) { oSel.add(opt); }

			oSel2.remove(0);

		}

	contractorDivUpdate();


	}

}



function contractordeSelectSome() {

	//window.alert("ZipdeSelectSome()");

	var oDivZip = document.getElementById('contractordiv');

	var oSel = document.getElementById('contractortype');

	var oSel2 = document.getElementById('contractor_selected');

	if ( null != oSel ) {

		while( -1 != ( i = oSel2.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[i].text;
			opt.value = oSel2.options[i].value;
			
			for (var s=0;s<(oSel.options.length);s++){
				if(String(oSel.options[s].text)==String(opt.text)){
					oSel2.remove(i);
					return false;
				}
			}


			try { oSel.add(opt,null);} catch(ex) {oSel.add(opt);}

			oSel2.remove(i);

		}

	contractorDivUpdate();
	

	}

}


function contractorDivUpdate() {

	// fill in the hidden elements that are used to POST the selected zipcodes

	var oDivZip = document.getElementById('contractordiv');

	var oSel2 = document.getElementById('contractor_selected');

	var s = "";

	// window.alert( "oSel2.length = " + oSel2.length );

	for ( var i = 0; i < oSel2.length; i++ )

		s += "<input type='hidden' name=conttype[" + i  + "] value='" + oSel2.options[i].value + "' />\n";

	oDivZip.innerHTML = s;

	

	// now update my cookie, so it can be used my the master form when it is submitted

	// this allows my form to work inside an iframe on the master form

	var c = "";

	for ( var i = 0; i < oSel2.length; i++ ) {

		if ( 0 < i )

			c+= "|";

		c+= oSel2.options[i].text;

		}

	//window.alert("setting cookie! -> " + c);
	changeservice();

	setCookie('contrator',c,1);
}



//service selection

function serviceSelectAll() {

	//window.alert("ZipSelectAll()");

	var oDivZip = document.getElementById('servicediv');

	var oSel = document.getElementById('servicetype');

	var oSel2 = document.getElementById('service_selected');



	var i = 0;

	if ( null != oSel ) {
		
		oSel2.options.length=0;

		while ( 0 < oSel.length) {

			var opt =document.createElement('option');

			opt.text = oSel.options[0].text;
			opt.value = oSel.options[0].value;

			try {oSel2.add(opt,null); } catch (ex) { oSel2.add(opt); }

			oSel.remove(0);

		}

		serviceDivUpdate();

	}

}



function serviceSelectSome() {

	//window.alert("ZipSelectSome()");

	var oDivZip = document.getElementById('servicediv');

	var oSel = document.getElementById('servicetype');

	var oSel2 = document.getElementById('service_selected');

	var i = 0;

	if ( null != oSel ) {

		while( -1 != ( i = oSel.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel.options[i].text;
			opt.value = oSel.options[i].value;
			
			for (var s=0;s<(oSel2.options.length);s++){
				if(String(oSel2.options[s].text)==String(oSel.options[i].text)){
					alert("Service already added in selected list");
					return false;				
				}
			}

			try { oSel2.add(opt,null);} catch(ex) {oSel2.add(opt);}

			oSel.remove(i);

		}

	serviceDivUpdate();

	}

}



function servicedeSelectAll() {

	//window.alert("ZipdeSelectAll()");

	var oDivZip = document.getElementById('servicediv');

	var oSel = document.getElementById('servicetype');

	var oSel2 = document.getElementById('service_selected');

	var s = "";

	if ( null != oSel ) {

		while ( 0 < oSel2.length) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[0].text;
			opt.value = oSel2.options[0].value;
			
			try {oSel.add(opt,null); } catch (ex) { oSel.add(opt); }

			oSel2.remove(0);

		}

	serviceDivUpdate();

	}

}



function servicedeSelectSome() {

	//window.alert("ZipdeSelectSome()");

	var oDivZip = document.getElementById('servicediv');

	var oSel = document.getElementById('servicetype');

	var oSel2 = document.getElementById('service_selected');

	if ( null != oSel ) {
		
		

		while( -1 != ( i = oSel2.selectedIndex) ) {

			var opt =document.createElement('option');

			opt.text = oSel2.options[i].text;
			opt.value = oSel2.options[i].value;
			
			//alert(oSel2.options.length);
			
			for (var s=0;s<(oSel.options.length);s++){
				if(String(oSel.options[s].text)==String(opt.text)){
					oSel2.remove(i);
					return false;
				}
			}

			try { oSel.add(opt,null);} catch(ex) {oSel.add(opt);}

			//oSel2.remove(i);

		}

	serviceDivUpdate();

	}

}


function serviceDivUpdate() {

	// fill in the hidden elements that are used to POST the selected zipcodes

	var oDivZip = document.getElementById('servicediv');

	var oSel2 = document.getElementById('service_selected');

	var s = "";

	

	for ( var i = 0; i < oSel2.length; i++ ){

		//alert( oSel2.options[i].value );

		s += "<input type='hidden' name=sertype[" + i  + "] value='" + oSel2.options[i].value + "' />\n";
	}

	oDivZip.innerHTML = s;

	

	// now update my cookie, so it can be used my the master form when it is submitted

	// this allows my form to work inside an iframe on the master form

	var c = "";

	for ( var i = 0; i < oSel2.length; i++ ) {

		if ( 0 < i )

			c+= "|";

		c+= oSel2.options[i].text;

		}

	//window.alert("setting cookie! -> " + c);

	setCookie('service',c,1);
}



function changeservice(selectedoption){

	var oSel2 = document.getElementById('contractor_selected');

	if(oSel2.length>0){
		contoption= new Array();
		for(i=0;i<oSel2.length;i++){
			contoption[i] = oSel2[i].value;
		}
		
			contoptions=contoption.join(",");
					
			var url = 'my_servicelist.php';
			var pars = 'coption=' + contoptions +'&mode=1';
			
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'get', 
					parameters: pars, 
					onComplete: showResponse
				});
				
	}
			
}
		
function showResponse(originalRequest)
{
		//put returned XML in the textarea
	$('myservicediv').innerHTML = originalRequest.responseText;
}

