
function recomputeTotals(_cost) {

    var form = document.transMiles;

    var totalMiles = 0;
    var totalCost = 0.00;

    var max = form.rec_count.value;
    var cost = parseFloat(_cost);

    if (max != 0) {

        var no = 1;
        for (no = 1; no <= max; no++){
            miles = eval("form.input_miles_" + no + ".value");
            totalMiles = parseFloat(totalMiles) + parseFloat(miles);
        }
        totalCost = formatAmount ( totalMiles * cost );

    }

    //fill form
    form.input_totalMiles.value = totalMiles;
    form.input_totalCost.value = totalCost;

    if (form.input_totalMiles.value == 'NaN') {
        document.getElementById('divTotalMiles').innerHTML = '0';
        document.getElementById('divTotalCost').innerHTML = '0';
    }
    else {
        document.getElementById('divTotalMiles').innerHTML = totalMiles;
        document.getElementById('divTotalCost').innerHTML = totalCost;
    }
}

function clearForm(no) {
	var form = document.transMiles;
	eval("form.input_first_" + no).value = "";
	eval("form.input_last_" + no).value = "";
	eval("form.input_no1_" + no).value = "";
	eval("form.input_no2_" + no).value = "";
	eval("form.input_no3_" + no).value = "";
	eval("form.input_miles_" + no).selectedIndex = 0;

	clearEmailDiv(no);
}

function clearEmailDiv(no) {
	var form = document.transMiles;
	eval("form.input_sendemail_" + no).checked = false;
	eval("form.input_sendemail_hid_" + no).value = "0";
	eval("form.input_emailAddress_" + no).value = "";
	var lang = eval("form.input_emailLang_" + no);
	lang[0].checked = true;
}

function showHideDiv( toShow, divName ){

    mode = toShow ? '' : 'none';

    if (document.getElementById){ // DOM3 = IE5, NS6
        document.getElementById(divName).style.display = mode;
    }
    else {
        if (document.layers){ // Netscape 4
            document.hideshow.display = mode;
        }
        else { // IE 4
            document.all.hideshow.style.display = mode;
        }
    }

    setTimeout('correctLabelSize()', 0);
    setTimeout('correctFormNo()', 0);
}

function showHideDivPlace( toShow, divName ){

    if(toShow){
    	enableButton(divName);
    }else{
    	disableButton(divName);
    }
    

}

function loadOptions(_min, _max, _inc, _cost){

    var amount;
    var min = parseInt(_min);
    var max = parseInt(_max);
    var inc = parseInt(_inc);
    var baseCost = parseFloat(_cost);
    var cCost;

    document.write("<option value=\'\'></option>");
    for (amount = min; amount <= max; ){
        cost = formatAmount(baseCost * amount);
        document.write("<option value=\'" + amount + "\'>" + amount + "  (" + cost + ")</option>");
        amount = amount + inc;
    }
}

function formatAmount(_amnt){

    var amnt = _amnt.toString();

    if (amnt.indexOf(".") == -1)
        amnt = amnt + ".00";
    else{
      if ((amnt.indexOf(".") - amnt.length) == 1)
            amnt = amnt + "0";
    }
    return amnt;
}

function initAddLink(){

    var count = document.transMiles.rec_count;
    var divNo = parseInt(count.value);
    if (isNaN(divNo))
        divNo = 1;
    count.value = divNo;
}

function addRecipient(){

    var count = parseInt(document.transMiles.rec_count.value);
    count++;
    document.transMiles.rec_count.value = count;

    var divisionName = "'recipient" + count.toString() + "'";
    showHideDiv(true, eval(divisionName));

    showHideAddLink();
    showHideRemoveLink();

    //Scroll to recipient's DIV
    scrollTo(count);

    //Focus on first name
    var elem = eval("'document.transMiles.input_first_" + count + "'");
    eval(elem).focus();

    correctLabelSize();
}

function scrollTo(no){
    document.location.href = getURLWithoutAnchor(document.location.href) + "#" + no;
}

function getURLWithoutAnchor(url){

    var urlOut = url;
    if (urlOut.indexOf("#") > 0){
        urlOut = urlOut.substring(0, urlOut.indexOf("#"));
    }
    return urlOut;
}

function removeRecipient(no, baseCost){

    var form = document.transMiles;

    clearForm(no);

    var count = form.rec_count;
    var noToRemove = no;

    if (count.value != no) {
		eval("form.input_first_"+no).value = eval("form.input_first_"+count.value).value;
		eval("form.input_last_"+no).value = eval("form.input_last_"+count.value).value;
		eval("form.input_no1_"+no).value = eval("form.input_no1_"+count.value).value;
		eval("form.input_no2_"+no).value = eval("form.input_no2_"+count.value).value;
		eval("form.input_no3_"+no).value = eval("form.input_no3_"+count.value).value;
		eval("form.input_miles_"+no).selectedIndex = eval("form.input_miles_"+count.value).selectedIndex;

        eval("form.input_sendemail_"+no).checked = eval("form.input_sendemail_"+count.value).checked;
        eval("form.input_sendemail_hid_"+no).value = eval("form.input_sendemail_hid_"+count.value).value;
		eval("form.input_emailAddress_"+no).value = eval("form.input_emailAddress_"+count.value).value;

        var langNew = eval("form.input_emailLang_" + no);
        var langOld = eval("form.input_emailLang_"+count.value);
	    langNew[0].checked = langOld[0].checked;
	    langNew[1].checked = langOld[1].checked;

		var divisionName = "'divEmail_" + no + "'";
        showHideDiv(eval("form.input_sendemail_"+no).checked, eval(divisionName));

		noToRemove = count.value;
	}

	clearForm(noToRemove);

    var divisionName = "'divEmail_" + noToRemove.toString() + "'";
    showHideDiv(false, eval(divisionName));

    if (noToRemove != 1){
        divisionName = "'recipient" + noToRemove.toString() + "'";
        showHideDiv(false, eval(divisionName));
    }

    divisionName = "'removeLink_" + noToRemove.toString() + "'";
    showHideDiv(false, eval(divisionName));

    if (count.value > 1)
        count.value = count.value - 1;

    recomputeTotals(baseCost);

    showHideAddLink();
    showHideRemoveLink()
}

function showHideAddLink(){

    var count = parseInt(document.transMiles.rec_count.value);
    if (count >= 3)
        showHideDiv(false, 'addLink');
    else
        showHideDiv(true, 'addLink');
}

function showHideRemoveLink(){

    var count = parseInt(document.transMiles.rec_count.value);

    var i = 1;
    for (i = 1; i <= count; i++){
        var divisionName = "'removeLink_" + i.toString() + "'";
        if (count == 1)
            showHideDiv(false, eval(divisionName));
        else
            showHideDiv(true, eval(divisionName));
    }
}

function adjustEmailDiv(showDiv, divName, no){

    if (!showDiv)
        clearEmailDiv(no);
    else
        eval("document.transMiles.input_sendemail_hid_" + no).value = "1";

    showHideDiv(showDiv, divName);
    correctLabelSize();
}

function reloadForm(count,
                    input_first_1,
                    input_first_2,
                    input_first_3,
                    input_last_1,
                    input_last_2,
                    input_last_3,
                    input_miles_1,
                    input_miles_2,
                    input_miles_3,
                    input_no1_1,
                    input_no1_2,
                    input_no1_3,
                    input_no2_1,
                    input_no2_2,
                    input_no2_3,
                    input_no3_1,
                    input_no3_2,
                    input_no3_3,
                    input_sendemail_hid_1,
                    input_sendemail_hid_2,
                    input_sendemail_hid_3,
                    input_emailAddress_1,
                    input_emailAddress_2,
                    input_emailAddress_3,
                    input_emailLang_1,
                    input_emailLang_2,
                    input_emailLang_3   ){

    var form = document.transMiles;

    /*var no = 1;
    for (no = 1; no <= count; no++){

        eval("form.input_first_" + no).value = eval("input_first_" + no);
        eval("form.input_last_" + no).value = eval("input_last_" + no);
        eval("form.input_no1_" + no).value = eval("input_no1_" + no);
        eval("form.input_no2_" + no).value = eval("input_no2_" + no);
        eval("form.input_no3_" + no).value = eval("input_no3_" + no);
        eval("form.input_miles_" + no).selectedIndex = getIndexSelected(eval("form.input_miles_" + no), eval("input_miles_" + no));

        eval("form.input_sendemail_hid_" + no).value = eval("input_sendemail_hid_" + no);

        eval("form.input_sendemail_" + no).checked = ( eval("form.input_sendemail_hid_" + no).value == '1' );

        showHideDiv( (eval("form.input_sendemail_" + no).checked), "'divEmail_" + no + "'");

        eval("form.input_emailAddress_" + no).value = eval("input_emailAddress_" + no);

        var lang = eval("form.input_emailLang_" + no);
        if (eval("input_emailLang_" + no) == 'fr')
            lang[1].checked = true;
        else
            lang[0].checked = true;
    }*/

    form.input_first_1.value = input_first_1;
    form.input_first_2.value = input_first_2;
    form.input_first_3.value = input_first_3;
    form.input_last_1.value = input_last_1;
    form.input_last_2.value = input_last_2;
    form.input_last_3.value = input_last_3;
    form.input_no1_1.value = input_no1_1;
    form.input_no1_2.value = input_no1_2;
    form.input_no1_3.value = input_no1_3;
    form.input_no2_1.value = input_no2_1;
    form.input_no2_2.value = input_no2_2;
    form.input_no2_3.value = input_no2_3;
    form.input_no3_1.value = input_no3_1;
    form.input_no3_2.value = input_no3_2;
    form.input_no3_3.value = input_no3_3;
    form.input_miles_1.value = input_miles_1;
    form.input_miles_2.value = input_miles_2;
    form.input_miles_3.value = input_miles_3;

    form.input_sendemail_hid_1.value = input_sendemail_hid_1;
    form.input_sendemail_hid_2.value = input_sendemail_hid_2;
    form.input_sendemail_hid_3.value = input_sendemail_hid_3;

    form.input_sendemail_1.checked = form.input_sendemail_hid_1.value == '1';
    form.input_sendemail_2.checked = form.input_sendemail_hid_2.value == '1';
    form.input_sendemail_3.checked = form.input_sendemail_hid_3.value == '1';

    showHideDiv( (form.input_sendemail_1.checked), 'divEmail_1');
    showHideDiv( (form.input_sendemail_2.checked), 'divEmail_2');
    showHideDiv( (form.input_sendemail_3.checked), 'divEmail_3');

    form.input_emailAddress_1.value = input_emailAddress_1;
    form.input_emailAddress_2.value = input_emailAddress_2;
    form.input_emailAddress_3.value = input_emailAddress_3;

    var lang = eval("form.input_emailLang_1");
    if (input_emailLang_1 == 'fr')
        lang[1].checked = true;
    else
        lang[0].checked = true;
    lang = eval("form.input_emailLang_2");
    if (input_emailLang_2 == 'fr')
        lang[1].checked = true;
    else
        lang[0].checked = true;
    lang = eval("form.input_emailLang_3");
    if (input_emailLang_3 == 'fr')
        lang[1].checked = true;
    else
        lang[0].checked = true;

    showHideDiv( (parseInt(count) >= 2), 'recipient2');
    showHideDiv( (parseInt(count) >= 3), 'recipient3');
}

function getIndexSelected(selectList, item){

    if (item == null || item.length == 0)
        return -1;

    var i = 0;
    var max = selectList.options.length;
    var itemToFind = parseInt(item);
    var currentVal;

    for (i = 0; i < max; i++){

        currentVal = selectList.options[i].value;
        if (currentVal == null || currentVal.length == 0)
            currentVal = -1;
        currentVal = parseInt(currentVal);
        if (currentVal == itemToFind){
            return i;
        }
    }
}

function getCost(_cost, _miles){

    var cost = parseFloat(_cost);
    var miles = parseFloat(_miles);
    document.write( formatAmount ( miles * cost ));
}

function editIt(norecipient, rooturl) {
    document.forms.transMiles.mode.value = "EDIT";
    document.forms.transMiles.norecipient.value = norecipient;
    document.forms.transMiles.action = rooturl + "/share_miles/step1.do?redirect=true";
    document.forms.transMiles.submit();
}

function deleteIt(norecipient, question, rooturl){
    if (confirm(question)){
        document.forms.transMiles.mode.value = "DELETE";
        document.forms.transMiles.norecipient.value = norecipient;
        document.forms.transMiles.action = rooturl + "/share_miles/delete.do";
        document.forms.transMiles.submit();
    }
}

function canSubmit( ) {

    var form = document.transMiles;

    var validTerms = form.chkTermsAndCond.checked;
    var validCC = true;

    showHideDivPlace( (validTerms && validCC) , 'divSubmit');
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function validDateCC(currentYear, currentMonth, month, year){

    var valid = false;
    var currentYY = parseInt(currentYear, 10);
    var currentMM = parseInt(currentMonth, 10);
    var ccMM = parseInt(month.options[month.selectedIndex].value, 10);
    var ccYY = parseInt(year.options[year.selectedIndex].value, 10);

    if (ccYY > currentYY)
        valid = true;
    else if (ccYY = currentYY)
        valid = ( ccMM >= currentMM );

    return valid;
}
