var country_mode = true;

window.onload = function () {
	if(document.form.mode[1].checked == true) {
		showCustomMode();
	}
	else {
		showCountryMode();
		document.form.mode[0].checked = true;
	}

 	updatePhotoType();
}

function selectCountryMode() {
	if(!country_mode) {
		document.form.mode[0].checked = true;
		showCountryMode();
	}
}

function selectCustomMode() {
	if(country_mode) {
		document.form.mode[1].checked = true;
		showCustomMode();
	}
}

function showCountryMode() {
	document.form.country.disabled = false;

	document.form.custom_width.disabled = true;
	document.form.custom_height.disabled = true;
	document.form.unit.disabled = true;

	country_mode = true;

	updateOptionBackground();
}

function showCustomMode() {
	document.form.country.disabled = true;

	document.form.custom_width.disabled = false;
	document.form.custom_height.disabled = false;
	document.form.unit.disabled = false;

	country_mode = false;

	updateOptionBackground();
}

function updateOptionBackground() {
  countryOption = document.getElementById("country_option");
  customOption = document.getElementById("custom_option");

  if(country_mode) {
  	countryOption.className="selected_option_bg";
  	customOption.className="option_bg";
  }
  else {
  	countryOption.className="option_bg";
  	customOption.className="selected_option_bg";
  }
}

function checkForm() {
	if(country_mode) {
		if (document.form.country.value == "NIL") {
		    alert( "Please selection a country/region." );
		    document.form.country.focus();
		    return false ;
		}
	}
	else {
		if (document.form.width.value == "") {
		    alert( "Please enter the width of the passport photo." );
		    document.form.width.focus();
		    return false ;
		}
		else if (document.form.height.value == "") {
		    alert( "Please enter the height of the passport photo." );
		    document.form.height.focus();
		    return false ;
		}

		if(document.form.unit.value == "mm") {
			if (parseFloat(document.form.width.value) > 100) {
			    alert( "The maximum width is 100 mm." );
			    document.form.width.focus();
			    return false ;
			}

			if (parseFloat(document.form.height.value) > 150) {
			    alert( "The maximum height is 150 mm." );
			    document.form.height.focus();
			    return false ;
			}
		}
		else if(document.form.unit.value == "cm") {
			if (parseFloat(document.form.width.value) > 10) {
			    alert( "The maximum width is 10 cm." );
			    document.form.width.focus();
			    return false ;
			}

			if (parseFloat(document.form.height.value) > 15) {
			    alert( "The maximum height is 15 cm." );
			    document.form.height.focus();
			    return false ;
			}
		}
		else if(document.form.unit.value == "inch"){
			if (parseFloat(document.form.width.value) > 4) {
			    alert( "The maximum width is 4 inches." );
			    document.form.width.focus();
			    return false ;
			}

			if (parseFloat(document.form.height.value) > 6) {
			    alert( "The maximum height is 6 inches." );
			    document.form.height.focus();
			    return false ;
			}
		}
	}

	return true;
}

function updatePhotoType()
{
  var xmlHttp;
  var country_code = document.form.country.value;

  if(country_code == "NIL") {
  	document.getElementById("photo_type_info").innerHTML="&nbsp;";
  	return true;
  }

  document.getElementById("photo_type_info").innerHTML="&nbsp;&nbsp;&nbsp;<font color='#FFFFFF'><span style='background-color: #FF3300'>&nbsp;Updating photo type infomation... </span></font>";

  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      document.getElementById("photo_type_info").innerHTML=xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET","get_photo_type.php?country_code="+country_code+"&sid="+Math.random(),true);
  xmlHttp.send(null);
}

