/*  ================================================================
FUNCTION:  isCreditCard(st)
INPUT:     st - a string representing a credit card number
RETURNS:  true, if the credit card number passes the Luhn Mod-10 test.
	      false, otherwise
    ================================================================ */
function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19) return (false);
  if (st.length < 10) return (false);

//  sum = 0; mul = 1; l = st.length;
//  for (i = 0; i < l; i++) {
//    digit = st.substring(l-i-1,l-i);
//    tproduct = parseInt(digit ,10)*mul;
//    if (tproduct >= 10)
//      sum += (tproduct % 10) + 1;
//    else
//      sum += tproduct;
//    if (mul == 1)
//      mul++;
//    else
//      mul--;
//  }
//	// Uncomment the following line to help create credit card numbers
//	// 1. Create a dummy number with a 0 as the last digit
//	// 2. Examine the sum written out
//	// 3. Replace the last digit with the difference between the sum and
//	//    the next multiple of 10.
//	
//	//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//	//  alert("Sum      = " + sum);

//  if ((sum % 10) == 0)
    return (true);
//  else
//    return (false);
} // END FUNCTION isCreditCard()




// whitespace characters
var whitespace = " \t\n\r";


// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace(s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}




// THIS FUNCTION IS USED BY APPLY2.ASP TO CHECK THE
// COUNTRY OF BIRTH AND DECIDE IF TO SHOW OR HIDE NATIVITY COUNTRY.
// *********** A DIFFERENT FUNCTION IS HANDLING APPLY2.ASP **************
function CheckCountry() {

	// set var for ms-ie
	var nativityDiv = document.getElementById("nativity");
	var nativityDiv1 = document.getElementById("nativity1");

	// set var for browsers
	var IE = (document.all) ? true : false;
	var NS = (document.layers) ? true : false;

	// hide nativity (just incase)
	if (NS) {
		document.all.nativity.style.visibility = "hide";
		document.all.nativity1.style.visibility = "hide";
		document.formName.nativity.value = 0
	} else {
		nativityDiv.style.visibility = "hidden";
		nativityDiv1.style.visibility = "hidden";
		document.formName.nativity.value = 0
	}

	//	get selected country
	var SelectedBornCountry = formName.cbirth[formName.cbirth.selectedIndex].value;

	// search for selected country in eligble countries string
	var foundCountry = formName.eligibleCountries.value.indexOf("/" + SelectedBornCountry + "/")

	// if not first line (caption)
	if (formName.cbirth.selectedIndex != 0) {

		if (NS) {
			if (foundCountry == -1) {
				document.all.nativity.style.visibility = "show";
				document.all.nativity1.style.visibility = "show";
			} else {
				document.all.nativity.style.visibility = "hide";
				document.all.nativity1.style.visibility = "hide";
			}
		} else {
			if (foundCountry == -1) {
				nativityDiv.style.visibility = "visible";
				nativityDiv1.style.visibility = "visible";
			} else {
				nativityDiv.style.visibility = "hidden";
				nativityDiv1.style.visibility = "hidden";
			}
		}
	}
}
// END OF CHECK COUNTRY FUNCTION



// THIS FUNCTION IS USED BY AND APPLY_SPOUSE.ASP TO CHECK THE
// COUNTRY OF BIRTH AND DECIDE IF TO SHOW OR HIDE NATIVITY COUNTRY.
// *******************************************************************
// IT IS THE SAME AS ABOVE FUNCTION (HANDLING APPLY2.ASP) BUT HAD TO
// BE DUPLICATED BECAUSE OF DIFFERENT INPUT-FIELD NAMES.
// *******************************************************************
function sCheckCountry() {

	// set var for ms-ie
	var nativityDiv = document.getElementById("nativity");
	var nativityDiv1 = document.getElementById("nativity1");

	// set var for browsers
	var IE = (document.all) ? true : false;
	var NS = (document.layers) ? true : false;

	// hide nativity (just incase)
	if (NS) {
		document.all.nativity.style.visibility = "hide";
		document.all.nativity1.style.visibility = "hide";
	} else {
		nativityDiv.style.visibility = "hidden";
		nativityDiv1.style.visibility = "hidden";
	}

	//	get selected country
	var SelectedBornCountry = formName.scbirth[formName.scbirth.selectedIndex].value;

	// search for selected country in eligble countries string
	var foundCountry = formName.eligibleCountries.value.indexOf("/" + SelectedBornCountry + "/")

	// if not first line (caption)
	if (formName.scbirth.selectedIndex != 0) {

		if (NS) {
			if (foundCountry == -1) {
				document.all.nativity.style.visibility = "show";
				document.all.nativity1.style.visibility = "show";
			} else {
				document.all.nativity.style.visibility = "hide";
				document.all.nativity1.style.visibility = "hide";
			}
		} else {
			if (foundCountry == -1) {
				nativityDiv.style.visibility = "visible";
				nativityDiv1.style.visibility = "visible";
			} else {
				nativityDiv.style.visibility = "hidden";
				nativityDiv1.style.visibility = "hidden";
			}
		}
	}
}
// END OF CHECK COUNTRY FUNCTION




// THIS ARRAY HOLDS ALL IMAGES AND CALL function "preloadimages" with list of images.
function call_load_proc() {
	preloadimages(
	"http://www.usafis.org/images/space.gif",
	"http://www.usafis.org/images/logo.gif",
	"http://www.usafis.org/images/right_top.gif",
	"http://www.usafis.org/images/link_top_divider.gif",
	"http://www.usafis.org/images/index_promo_top.jpg",
	"http://www.usafis.org/images/index_promo_bot.jpg",
	"http://www.usafis.org/images/i_a.gif",
	"http://www.usafis.org/images/firstgov_logo.gif",
	"http://www.usafis.org/images/apply.gif",
	"http://www.usafis.org/images/i_b.gif",
	"http://www.usafis.org/images/i_c.gif",
	"http://www.usafis.org/images/ssl1.gif",
	"http://www.usafis.org/images/i_d.gif",
	"http://www.usafis.org/images/blink_divider.gif",
	"http://www.usafis.org/images/link_home.gif",
	"http://www.usafis.org/images/link_greencard.gif",
	"http://www.usafis.org/images/link_visa.gif",
	"http://www.usafis.org/images/link_forms.gif",
	"http://www.usafis.org/images/link_employer.gif",
	"http://www.usafis.org/images/link_publication.gif",
	"http://www.usafis.org/images/link_tools.gif",
	"http://www.usafis.org/images/blink_about.gif",
	"http://www.usafis.org/images/blink_sitemap.gif",
	"http://www.usafis.org/images/blink_legal.gif",
	"http://www.usafis.org/images/blink_privacy.gif",
	"http://www.usafis.org/images/blink_contact.gif",
	"http://www.usafis.org/images/link_home1.gif",
	"http://www.usafis.org/images/link_greencard1.gif",
	"http://www.usafis.org/images/link_visa1.gif",
	"http://www.usafis.org/images/link_forms1.gif",
	"http://www.usafis.org/images/link_employer1.gif",
	"http://www.usafis.org/images/link_publication1.gif",
	"http://www.usafis.org/images/link_tools1.gif",
	"http://www.usafis.org/images/blink_about1.gif",
	"http://www.usafis.org/images/blink_sitemap1.gif",
	"http://www.usafis.org/images/blink_legal1.gif",
	"http://www.usafis.org/images/blink_privacy1.gif",
	"http://www.usafis.org/images/blink_contact1.gif");
}
// END OF FUNCTION & ARRAY



// THIS FUNCTION  (called by call_load_proc) LOAD ALL IMAGES
function preloadimages() {
	var myimages = new Array();
	for (i=0; i < preloadimages.arguments.length; i++) {
		myimages[i] = new Image()
		myimages[i].src = preloadimages.arguments[i]
	}
}
// END OF PRELOAD FUNCTION




// OPEN NEW WINDOW
function open_new_window(url) { 
	new_window = window.open(url,'new','toolbar=0,menubar=0,resizable=0,dependent=0,scrollbars=1,status=0,width=320,height=320,left=25,top=25')
}
// END NEW WINDOW FUNCTION


//		// OPEN NEW APPLICATION FORM WINDOW
//		// USED BY /psts/main.asp TO OPEN THE FORM WINDOW
//		function open_new_apps_window(url) { 
//			alert("A new window will open with your application form. Please print the form and close the window (you can repeat this operation any time later).")
//			new_window = window.open(url,'new','toolbar=0,menubar=1,resizable=1,dependent=0,scrollbars=1,status=0,width=650,height=400,left=10,top=10')
//		}
//		// END NEW APPLICATION FORM WINDOW FUNCTION



// OPEN NEW WINDOW FOR BROWSING (FOR EXTERNAL LINKS)
// USED BY view_embassies.asp
function open_new_browse_window(url) { 
	new_window = window.open(url,'new','toolbar=0,menubar=0,resizable=1,dependent=0,scrollbars=1,status=1,width=600,height=400,left=15,top=15')
}
// END NEW WINDOW FOR BROWSING FUNCTION




// THIS FUNCTION SUBMITS APPLICATION FORM ACCORDING TO RADIO BOX
// (CALLED BY preSubmitFunctions) 
	function selectDestination() {
		if (document.myform.payment[0].checked)
			document.myform.action = "https://secure.tranzila.com/cgi-bin/tranzila2.cgi"; // CC ON
		if (document.myform.payment[1].checked)
			document.myform.action = "../user/index.html"; // C
		if (document.myform.payment[2].checked)
			document.myform.action = "../user/index.html"; // CC OFF
	}
// END SUBMIT FUNCTION




// THIS FUNCTION SELECT THE SUM (HIDDEN INPUT) BASED ON THE PRODUCT SELECTED
// (CALLED BY preSubmitFunctions) 
	function selectSum() {
		for (var i = 0; i < myform.product.length; i++)
			if (myform.product[i].checked) {
				var prod = myform.product[i].value
				if (prod == 1) myform.sum.value = 35 - myform.theCoupon.value;
				if (prod == 2) myform.sum.value = 50 - myform.theCoupon.value;
				if (prod == 3) myform.sum.value = 50 - myform.theCoupon.value;
				if (prod == 4) myform.sum.value = 70 - myform.theCoupon.value;
				// **********************************************
				// FOLLOWING LINES FOR DEBUG ONLY:
				// **********************************************
				//alert("product: " + myform.product[i].value);
				//alert("coupon: " + myform.theCoupon.value);
				//alert("sum (after coupon): " + myform.sum.value);	
				// **********************************************
			}
	}
// END OF SUM SELECT FUNCTION



// THIS FUNCTION CALL THE FUNCTIONS NEEDED BEFORE PAYMENT FORM IS SUBMITED
// (CALLED BY apply_payment.asp) 
	function preSubmitFunctions() {
			//select sum based on product
			selectSum();
			//select form action based on payment method
			selectDestination();
			//if cc-online
			if (document.myform.payment[0].checked) {
				//check if real credit card (based on cc number)
				if (isCreditCard(myform.cc.value) != true) 
					{
						alert("A valid credit card number must be used.");
						//myform.action = "";
						return false;
					}
				//check if card holder name is not empty
				if (isWhitespace(myform.cardHolder.value) == true) 
					{
						alert("A valid card holder name must be used.");
						//myform.action = "";
						return false;
					}
			}
	}
//END PRE SUBMIT FUNCTION


// THIS FUNCTION CALL THE FUNCTIONS NEEDED BEFORE PAYMENT FORM IS SUBMITED
// (CALLED BY apply_payment.asp) 
	function OLDpreSubmitFunctions() {
			selectSum();
			selectDestination();
	}
//END OLD PRE SUBMIT FUNCTION



// THIS FUNCTION  SUBMITS LINK URL'S USED IN THE MAIN.ASP PAGE TO CHANGE PAYMENT METHOD
function submit() {
document.myform.submit() 
}
// END SUBMIT FUNCTION



// THIS FUNCTION OPENS THE POPUP whyusafis.asp PAGE
function open_window(url)
{ 
new_window = window.open(url,'window_name','toolbar=0,menubar=0,resizable=0,dependent=0,scrollbars=0,status=0,width=330,height=320,left=25,top=25')
}
// END POPUP WHYUSAFIS FUNCTION