document.observe("dom:loaded", dom_Loaded, false);

function dom_Loaded() {
 
 if ( Object.isElement($('fldCountryID')) == true ) {
		Event.observe($('fldCountryID'), "change", ajax_lookup_locations, false);
		if ($('fldCountryID').selectedIndex > 0 ) {	
			ajax_lookup_locations();
		}
	}
	if ( Object.isElement($('fldNVCompaniesIDs')) == true ) {
		if ( $('fldNVCompaniesIDs').selectedIndex == -1 ) {
			$('fldNVCompaniesIDs').selectedIndex = 0;
		}
    }
    if (Object.isElement($('frm_registernew')) == true) {
        Event.observe($('frm_registernew'), "submit", validate_frm_registernew, false);
    }
    if (Object.isElement($('frm_reader_modify')) == true) {
        Event.observe($('frm_reader_modify'), "submit", validate_frm_reader_modify, false);
    }    
}

function validate_frm_registernew(e) {
    if ($('fldPassword').value != $('fldPasswordConfirm').value) {
        alert('Your passwords do match.');
        Event.stop(e);
    } else if ($('fldPassword').value.length == 0) {
        alert('You must provide a password.');
        Event.stop(e);
    } else if ($('fldTermsConditions').checked == false ) {
        alert('You must agree to the terms and conditions');
        Event.stop(e);
    }        
}


function validate_frm_reader_modify(e) {
    if ($('fldPassword').value != $('fldPasswordConfirm').value) {
        alert('Your passwords do match.');
        Event.stop(e);
    } else if ($('fldTermsConditions').checked == false ) {
        alert('You must agree to the terms and conditions');
        Event.stop(e);
    }     
}


function ajax_lookup_locations() {
	var pars = 'ACT=locationsbycountry&countryID=' + getCountryID(); 
	clearOptions($('fldLocationID'));
	var myAjax = new Ajax.Request(
		 url,
		 {  method: 'get',
		  parameters: pars,
		  onComplete: return_lookup_locations
		});		
}	


function return_lookup_locations(returnvalue) {
	var json = checkAJAXResult(returnvalue.responseText);
	
	if ( json == false) {
		alert('there was a problem contacting the web server, please refresh the page');
	} else {
		if (json.success == 0) {
			alert('There was a problem loading locations, please refresh the page.');
			return false;
		}
		if (json.rows.length == 0) {
			alert('No locations found, please choose another country.');
			return false;			
		}
		for (pos = 0; pos < json.rows.length; pos++) {
			if (json.rows[pos].row_id == $('fldLocationIDValue').value) {
				$('fldLocationID').appendChild(new Element('option',{'value': json.rows[pos].row_id,'selected': '0'}).update(json.rows[pos].fldname));
				$('fldLocationIDValue').value='';
			} else {
				$('fldLocationID').appendChild(new Element('option',{'value': json.rows[pos].row_id}).update(json.rows[pos].fldname));
			}			
		}		
	}	
}

function clearOptions(ctlSelect){	
	for (pos = $('fldLocationID').options.length-1; pos > 0; pos--) {
		$('fldLocationID').options[pos].remove();
	}	
}

function getCountryID() {
	if ($('fldCountryID').options.length > 0){
		return $('fldCountryID').options[$('fldCountryID').selectedIndex].value
	}
	else {
		return "0"
	}
}

function checkAJAXResult(text) {
	return Try.these(
		function() {return text.evalJSON(true);}								 
	) || false;
}

var url='http://'+hostName+'/code/classes/ajax/reader/lookups.asp';