function submitModelForm(){  
  if(document.model_form.handset_quick_list.options[document.model_form.handset_quick_list.selectedIndex].value == ''){
    return false;
  }
     
  Effect.SlideUp('model_selection', {duration: 2});
  
	new Ajax.Updater('result', 'ajax/make_to_model.php', {
		asynchronous:true, 
		evalScripts:true,
		// onLoading: function(){ Effect.SlideDown('status'); },
		onSuccess: function(){ Effect.SlideDown('result');},
		// onComplete: function(){ Effect.SlideUp('status'); fixBoxIE(); },
		onComplete: function(){ fixBoxIE(); },
		parameters:Form.serialize(document.model_form)
	});
	
	document.getElementById('null').focus();
	
	return false;
}

function fixBoxIE(){  
  if(navigator.appName != 'Microsoft Internet Explorer'){
	  $('phone-box').setStyle({margin: '11px 72px 0 0'});
	}
}

function showModelForm(){
  Effect.SlideUp('result');
  Effect.SlideDown('model_selection');  
}

			function onCountrySelect(element) {
				//turn off all the carrier lists first:
				var listlist = document.getElementById('carrier_lists').getElementsByTagName('select');
				for (var i = 0; i < listlist.length; i++) {
					listlist.item(i).style.display = 'none';
					listlist.item(i).disabled = true;
				}

				//turn on the chosen list:
				var country = element.getElementsByTagName('option').item(element.selectedIndex).value;
				var list = document.getElementById('list-'+country);
				list.style.display = 'inline';
				list.disabled = false;
			}


function onCarrierSelect(){  
	new Ajax.Updater('prices', 'ajax/carrier_pricing.php', {
		asynchronous:true, 
		evalScripts:true,
		onLoading: function(){ Element.hide('prices') },
		onComplete: function(){ 
		  Effect.BlindDown('prices'); 
		  $('carrier_phone').writeAttribute({value: $('carrier_form').carrier.options.selectedIndex + 1});		  
		},
		parameters:Form.serialize($('carrier_form'))
	});
	
	return false;
}

function visible(element){
  return $(element).visible(function(v){ return v })
}

function valid_form(){  
  return (required_fields.length == 0 ? true : false)
}

function order_form_verify(field, type, html_field, wait_image){  
  html_field = (html_field == null ? field : html_field)
  value = $(html_field).value
  
  verify_url = 'ajax/order-verify.php?type=' + type + '&field=' + field + '&value=' + value;
  
  new Ajax.Request(verify_url, {    
    onComplete: function(response){  
      error_field = html_field + '_error'

      if(response.responseText == 'good'){
        $(html_field).setStyle({border: '1px solid green', background: 'white', padding: '2px'})

        required_fields.splice(required_fields.indexOf(field), 1)
        
        other_error_fields = $$('#error-list li').filter(function(element){ return element.id != error_field })
      
        if($(error_field) != null){
          Effect.BlindUp(error_field);
        }
      
        if(!other_error_fields.any(function(element){ return visible(element) }) && visible($('errors'))){
          Effect.BlindUp('errors');          
        } 
      } else {        
        $(html_field).setStyle({border: '1px solid red', background: 'white', padding: '2px'})
        
        if(!required_fields.any(function(f){ return f == field })){
          required_fields.push(field)
        }
        
        error_text = Builder.node('li', { id: error_field }, response.responseText);
        
        if($(error_field) != null){
          $(error_field).replace(error_text)
        } else {        
          $('error-list').insert({bottom: error_text})
        }

        if(!errors_visible){          
          errors_visible = true
          Effect.BlindDown('errors');
        } else {
          $(error_field).hide();
          Effect.BlindDown(error_field);
        }
      }
    }
  })
}



function place_order(){
  if(!valid_form()){
    required_fields.each(function(field){ $(field).focus(); $(field).blur(); })
  } else {
    $('order').submit();
  }
}