var strDotNetIDPrefix;
var strDotNetNamePrefix;
var strCountry;

$(document).ready(function() {	
	$(".date1").focus(function() {
		var strCurrentVal = $(this).val().toLowerCase();
		if(strCurrentVal == "mm" || strCurrentVal == "dd") {
			$(this).val("");
		}
	});
	
	$(".date2").focus(function() {
		var strCurrentVal = $(this).val().toLowerCase();
		if(strCurrentVal == "yyyy") {
			$(this).val("");
		}
	});
		
	$('.phone1').keydown(function(event){
	    if(event.keyCode >= 65 && event.keyCode <= 90){ // character codes for a-z	
	      return false;
	    }
	});
	
	$('.phone2').keydown(function(event){
	    if(event.keyCode >= 65 && event.keyCode <= 90){ // character codes for a-z	
	      return false;
	    }
	});
		
	$('.phone1').keyup(function(event){
    if((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)){ // character codes for 0-9
     if($(this).val().length == 3){  	      
        var thisID = $(this).attr('id');
        var found = false;
        $('input.text').each(function(){
          if ($(this).attr('id') == thisID){
            found = true;              
          }else if (found){
            found = false;
            $(this).focus();
          }
        });
      }
     }
	});	
	
	$('.phone2').keyup(function(event){
    if((event.keyCode >= 48 && event.keyCode <= 57)||(event.keyCode >= 96 && event.keyCode <= 105)){ // character codes for 0-9
     if($(this).val().length == 4){  	      
        var thisID = $(this).attr('id');
        var found = false;
        $('input.text').each(function(){
          if ($(this).attr('id') == thisID){
            found = true;              
          }else if (found){
            found = false;
            $(this).focus();
          }
        });
      }
     }
	});	
	
	/*/	Changes in country will dynamically alter the phone and location fields.
	var strTargetLocationID = "";
	var strTargetPhoneID = "";
	$(".ddlCountry").change(function() {
		strTargetLocationID = $(this).parent().siblings(".locationFields").attr("id");
		strTargetPhoneID = $(this).parent().siblings(".phoneFields").attr("id");
		var strSelection = $(this).val();
//alert("strSelection = " + strSelection);
		if(strSelection != "" && strSelection != "Choose Country" && strSelection != undefined) {
			fUpdateLocationFields(strSelection, strTargetLocationID);
			fUpdatePhoneFields(strSelection, strTargetPhoneID);
		}
	});
	
//alert("strDotNetIDPrefix = " + strDotNetIDPrefix);

	if(strCountry != "" && strCountry != undefined) {	//	initial country was specified on page load.
		$(".ddlCountry").each(function() {	//	preselect the appropriate phone and location fields
			$(this).val(strCountry);
			strTargetLocationID = $(this).parent().siblings(".locationFields").attr("id");
			strTargetPhoneID = $(this).parent().siblings(".phoneFields").attr("id");
			fUpdateLocationFields(strCountry, strTargetLocationID);
			fUpdatePhoneFields(strCountry, strTargetPhoneID);
		});
	}*/
});
						
/*
	Function to verify that a dropdown list has any selected item besides the first one.
	@param strID the ID attribute of the form element.
*/
function fVerifySelect(strID) {
//alert("document.getElementById(\"" + strDotNetIDPrefix + strID + "\") = " + document.getElementById(strDotNetIDPrefix + strID));
	if(document.getElementById(strDotNetIDPrefix + strID).selectedIndex > 0) {
		return true;
	} else {
		return false;
	}
}

/*
	Update the unordered list of location fields, depending on whether the chosen 
	country is the United States, Canada or a different country.
	@param strCountry the chosen country
	@param strTarget the id attribute of the unordered list.  Defaults to "locationFields" if not specified
	@param strCity (optional) the city to display in the city field.
	@param strState (optional) the default state to be selected in the state/province dropdown list
	@param strZip (optional) the zip/postal code to display in the zipcode/postal code field.
*/
function fUpdateLocationFields(strCountry, strTarget, strCity, strState, strZip) {
	var strOutput = "";
	if(strTarget == "" || strTarget == undefined) {
		strTarget = "locationFields";
	}
//alert("strDotNetNamePrefix = " + strDotNetNamePrefix);
	var strCityFieldID = strDotNetIDPrefix + "txtCity";
	var strCityFieldName = strDotNetNamePrefix + "txtCity";
	var strStateFieldID = strDotNetIDPrefix + "txtState";
	var strStateFieldName = strDotNetNamePrefix + "txtState";
	var strZipFieldID = strDotNetIDPrefix + "txtZip";
	var strZipFieldName = strDotNetNamePrefix + "txtZip";
	if(strCity == undefined) {
		strCity = "";
	}
	if(strState == undefined) {
		strState = "";
	}
	if(strZip == undefined) {
		strZip = "";
	}
	
	if(strCountry == "United States" || strCountry == "" || strCountry == undefined) {
		strOutput += "<li>";
		strOutput += "City<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strCityFieldName + "\" id=\"" + strCityFieldID + "\" value=\"" + strCity + "\" class=\"text city\" />";
		strOutput += "</li>";
		strOutput += "<li>";
		strOutput += "State<span class=\"cssRequired\">*</span><br />";
		strOutput += "<select name=\"" + strStateFieldName + "\" id=\"" + strStateFieldID + "\" class=\"state\">";
		strOutput += fGetStateList(strState);
		strOutput += "</select>";
		strOutput += "</li>";
		strOutput += "<li>";
		strOutput += "Zip<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strZipFieldName + "\" id=\"" + strZipFieldID + "\" value=\"" + strZip + "\" class=\"text zip\" />";
		strOutput += "</li>";
	} else if(strCountry == "Canada") {
		strOutput += "<li>";
		strOutput += "City<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strCityFieldName + "\" id=\"" + strCityFieldID + "\" value=\"" + strCity + "\" class=\"text city\" />";
		strOutput += "</li>";
		strOutput += "<li>";
		strOutput += "Province<span class=\"cssRequired\">*</span><br />";
		strOutput += "<select name=\"" + strStateFieldName + "\" id=\"" + strStateFieldID + "\" class=\"province\">";
		strOutput += fGetCanadianProvinceList(strState);
		strOutput += "</select>";
		strOutput += "</li>";
		strOutput += "<li>";
		strOutput += "Postal Code<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strZipFieldName + "\" id=\"" + strZipFieldID + "\" value=\"" + strZip + "\" class=\"text postalCode\" />";
		strOutput += "</li>";
	} else {
		strOutput += "<li>";
		strOutput += "Additional Mailing Information<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strCityFieldName + "\" id=\"" + strCityFieldID + "\" value=\"" + strCity + "\" class=\"text\" />";
		strOutput += "</li>";
	}
	document.getElementById(strTarget).innerHTML = strOutput;
}

/*
	Update the unordered list of phone number fields, depending on whether the 
	chosen country is the United States, Canada or a different country.
	@param strCountry the chosen country
	@param strTarget the id attribute of the unordered list.  Defaults to "phoneFields" if not specified
	@param strPhoneA (optional) the area code
	@param strPhoneB (optional) the first three digits of the phone number
	@param strPhoneC (optional) the last four digits of the phone number
*/
function fUpdatePhoneFields(strCountry, strTarget, strPhoneA, strPhoneB, strPhoneC) {
	var strOutput = "";
	if(strTarget == "" || strTarget == undefined) {
		strTarget = "phoneFields";
	}
	var strPhoneAFieldID = strDotNetIDPrefix + "txtPhoneA";
	var strPhoneAFieldName = strDotNetNamePrefix + "txtPhoneA";
	var strPhoneBFieldID = strDotNetIDPrefix + "txtPhoneB";
	var strPhoneBFieldName = strDotNetNamePrefix + "txtPhoneB";
	var strPhoneCFieldID = strDotNetIDPrefix + "txtPhoneC";
	var strPhoneCFieldName = strDotNetNamePrefix + "txtPhoneC";
	if(strPhoneA == undefined) {
		strPhoneA = "";
	}
	if(strPhoneB == undefined) {
		strPhoneB = "";
	}
	if(strPhoneC == undefined) {
		strPhoneC = "";
	}
	
	if(strCountry == "United States" || strCountry == "Canada" || strCountry == "" || strCountry == undefined) {
		strOutput += "<li>";
		strOutput += "Phone<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strPhoneAFieldName + "\" id=\"" + strPhoneAFieldID + "\" value=\"" + strPhoneA + "\" class=\"text phone1\" /> - <input type=\"text\" name=\"" + strPhoneBFieldName + "\" id=\"" + strPhoneBFieldID + "\" value=\"" + strPhoneB + "\" class=\"text phone1\" /> - <input type=\"text\" name=\"" + strPhoneCFieldName + "\" id=\"" + strPhoneCFieldID + "\" value=\"" + strPhoneC + "\" class=\"text phone2\" />";
		strOutput += "</li>";
	} else {
		strOutput += "<li>";
		strOutput += "Phone<span class=\"cssRequired\">*</span><br />";
		strOutput += "<input type=\"text\" name=\"" + strPhoneAFieldName + "\" id=\"" + strPhoneAFieldID + "\" value=\"" + strPhoneA + "\" class=\"text\" />";
		strOutput += "</li>";
	}
	document.getElementById(strTarget).innerHTML = strOutput;
}