/*
 * JAVASCRIPT FILE FOR SPLIT ORDER PROCESS
 * 5/11/10 dh
 */
$(document).ready(function(){    
    $("a#split_click").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	200,
		'speedOut'		:	200,
		'modal'         :	true,
        'easingIn'      :   'swing',
        'easingOut'     :   'swing',
        'centerOnScroll':   true,
        'opacity'       :   true,
        'width'         :   785,
        'height'        :   590,
        'autoDimensions':   false,
        'overlayOpacity':   .9
	});     
});

//*****
function hideIt(id, state){
	if (state == 'show'){
		if (navigator.appName.indexOf("Microsoft") > -1){
     		 document.getElementById(id).style.display = 'block';
		} else {
     		document.getElementById(id).style.display = 'table-row';
		}
    }else{
      document.getElementById(id).style.display = 'none';
    }
}
//*****

function deleteStudent(student_id, id, val) {
    $.get("../Scripts/split_order_ajax.php?deleteStudent=1&student="+student_id+"&product_id="+id, function(data) {
        if (data != '') {
            alert(data);
        } else {
            showAttendeeList('0', id, val);
        }
    });
}

function quantityCatch(val, id) {
    $("#split_click").attr('href', '../Scripts/split_order_ajax.php?id='+id+'&quant='+val);
    if (val != 1) {
        $("#split_click").click();
    }
}

function subtractClassSeat(id, val, student_id) {
    var question = confirm("You are about to remove a seat from your order. Are you sure you want to do this?");
    if (question) {
        $.get("../Scripts/split_order_ajax.php?subtractClassSeat=1&id="+id+"&quant="+val+"&student="+student_id, function(data) {
            showAttendeeList('0', id, data);
        });
    }
}

function subStudent(id, val, student_id) {
    $.get("../Scripts/split_order_ajax.php?substituteStudent=1&id="+id+"&quant="+val+"&student="+student_id, function(data) {
        nextStep('insert_new_attendee', id, val);
    });
}

function addClassSeat(id, val) {
    $.get("../Scripts/split_order_ajax.php?addClassSeat=1&id="+id+"&quant="+val, function(data) {
        showAttendeeList('0', id, data);
    });
}

function handleEnter(inField, e) {
    var charCode;

    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
        //alert("Enter was pressed on " + inField.id);
        $("#verify_button").click();
    }
}

function cancelOrder(urlString) {
    urlString += "&execute=0";
    $.get("../Scripts/split_order_ajax.php?cancel_order=1&url_string="+urlString, function(data) {
        //$('#product_avail').selectOptions('1');
        //$.fancybox.close();
        window.location.href = urlString;
    });
}

function addThisStudent(prod_id, quantity, email) {
    $.get("../Scripts/split_order_ajax.php?id="+prod_id+"&quant="+quantity+"&addThisStudent="+email, function(data) {
        if (data == '0') {
            alert('There was an error adding this student!');
        } else if (data == 'already_registered') {
        	//do nothing here
        } else {
            showAttendeeList('0', prod_id, quantity);
        }
    });
}

function nextStep(step, id, val) {
    $.get("../Scripts/split_order_ajax.php?id="+id+"&quant="+val+"&step="+step, function(data) {
        $("#fancybox-inner").html(data);
    });
}

function showAttendeeList(student_id, id, val) {
    var step = 'attendee_list';
    if (student_id == '0') {
        $.get("../Scripts/split_order_ajax.php?id="+id+"&quant="+val+"&step="+step, function(data) {
            $("#fancybox-inner").html(data);
        });
    } else {
        $.get("../Scripts/split_order_ajax.php?id="+id+"&quant="+val+"&step="+step+"&add_student="+student_id, function(data) {
            $("#fancybox-inner").html(data);
        });
    }
}

function verify_email(id, val) {
    var email = $("#check_email").val();
    if (email != '') {
        $.get("../Scripts/split_order_ajax.php?verify_email="+email+"&id="+id+"&quant="+val, function(data) {
            $("#verify_message").html(data);
            $("#verify_message").slideDown();
        });
    } else {
        $("#verify_message").html('You must enter an email before clicking VERIFY.');
        $("#verify_message").slideDown();
    }
}

function showAddProfile(id, val, profile_flag) {
    if (profile_flag == 1) {
        var typedEmail = '';
    } else {
        var typedEmail = $("#check_email").val();
    }
    $.get("../Scripts/split_order_ajax.php?showProfileForm=1&typedEmail="+typedEmail+"&id="+id+"&quant="+val, function(data) {
        $("#fancybox-inner").html(data);
    });
}

function showEditProfile(student, id, val) {
    var typedEmail = '';
    $.get("../Scripts/split_order_ajax.php?showProfileForm=1&typedEmail="+typedEmail+"&id="+id+"&quant="+val+"&student_id="+student+"&mode=MODIFY", function(data) {
        $("#fancybox-inner").html(data);
    });
}
