$(document).ready(function() {

	// hide confirm and error boxes after 3 seconds
	setTimeout ( "hideConfirmBoxes()", 3000 );
	setTimeout ( "hideErrorBoxes()", 3000 );
	
	// offset where the bottom module starts based on the height of the top module
	$("#content_bottom").css('top',($("#content_top").height()+50));
	if (jQuery.support.opacity){
		$("#content_bottom").fadeIn('slow');
	} else {
		$("#content_bottom").show();
	}
	
	$('#username').focus();
	

	
	// supposed to fix .png transparency in IE 6
	$('body').supersleight({shim: 'https://www.my-internet-payday.com/scripts/x.gif'});
	

	// squeeze form button
	$("#formbutton01").mousedown(function () { $(this).attr('class','formbutton01-down'); });
	$("#formbutton01").mouseup(function () { $(this).attr('class','formbutton01-up'); });
	$("#formbutton01").click(function () { document.frmSqueeze.submit(); });
	
	// squeeze form button on the landing 4 page (with no squeeze form)
	$("#formbutton01a").mousedown(function () { $(this).attr('class','formbutton01-down'); });
	$("#formbutton01a").mouseup(function () { $(this).attr('class','formbutton01-up'); });
	$("#formbutton01a").click(function () { window.location="landing4.php?module=orderconfirm4" });

	// order button
	$("#formbutton02").mousedown(function () { $(this).attr('class','formbutton02-down'); });
	$("#formbutton02").mouseup(function () { $(this).attr('class','formbutton02-up'); });
	$("#formbutton02").click(function () {  
		// launch the v authentication window
		cctype = getCardType($("#cardnumber").val());
		if (cctype == ''){
			alert('You must enter a valid credit card number.');
		} else {
			VerifiAuthentication.manualStart(document.frmOrder);
		}
	});

	// order button (on the order2 and order4 form)
	$("#formbutton02a").mousedown(function () { $(this).attr('class','formbutton02-down'); });
	$("#formbutton02a").mouseup(function () { $(this).attr('class','formbutton02-up'); });
	$("#formbutton02a").click(function () { 
		//document.frmOrder.submit(); 
		// launch the v authentication window
		cctype = getCardType($("#cardnumber").val());
		if (cctype == ''){
			alert('You must enter a valid credit card number.');
		} else {
			VerifiAuthentication.manualStart(document.frmOrder);
		}
	});
	
	// order confirm buttons
	$("#formbutton02b").mousedown(function () { $(this).attr('class','formbutton02b-down'); });
	$("#formbutton02b").mouseup(function () { $(this).attr('class','formbutton02b-up'); });
	$("#btnConfirmOrder").click(function () { document.frmOrderConfirm.submit(); });
	$("#btnConfirmOrder2").click(function () { document.frmOrderConfirm.submit(); });
	$("#btnConfirmOrder3").click(function () { document.frmOrderConfirm.submit(); });
	$("#btnConfirmOrder4").click(function () { document.frmOrderConfirm.submit(); });
	
	// register button
	$("#formbutton03").mousedown(function () { $(this).attr('class','formbutton03-down'); });
	$("#formbutton03").mouseup(function () { $(this).attr('class','formbutton03-up'); });
	$("#formbutton03").click(function () { document.frmUserCreate.submit(); });
	
	// login button
	$("#loginbutton").mousedown(function () { $(this).attr('class','loginbutton-down'); });
	$("#loginbutton").mouseup(function () { $(this).attr('class','loginbutton-up'); });
	$("#loginbutton").click(function () { document.frmLogin.submit(); });

	$("#logoff").click(function () { window.location="http://theprofitvault.com/modules/login/doLogoff.php" });
	$("#logon").click(function () { window.location="index.php" });
	$("#gotosales").click(function () { window.location="cb.php" });
	
	// order now buttons in the bottom of the landing4 page
	$(".btnOrderNowLanding4").click(function () { window.location="landing4.php?module=orderconfirm4" });
	
	// we need some code here to check if they enter a visa or mc CC on the order form
	// and display the appropriate learn more graphic
	$("#cardnumber").blur(function () {
		// if there is not a cc number we want to be sure to hide our learn more graphics
		// if there is a cc number, we need to show the appropriate graphic
		if ($(this).val() == ''){
			$("#verifiVisa").hide();
			$("#verifiMC").hide();
		} else {
			// get our credit card type
			cctype = getCardType($(this).val());
			if (cctype == 'visa'){
				$("#verifiVisa").slideDown();
			} else if (cctype == 'mastercard'){
				$("#verifiMC").slideDown();
			} else if (cctype == ''){
				alert('That is not a valid credit card number.');
			}
		}
	});
	
	
	$("#btnFlashExit").click(function () {hideFlash();});
	
	// FLASH WINDOWS //
	$("#landing1flash").flash({src: 'flash/Squeeze_WalkOn_150x282.swf',width:150,height:282});
	$("#order1flash").flash({src: 'flash/Order_WalkOn.swf',width:300,height:565});
	$("#thanksflash").flash({src: 'flash/Thanks_Page.swf',width:300,height:250});
	$("#landing2flash").flash({src: 'flash2/Squeeze_WalkOn.swf',width:150,height:282});
	
	// set a timer to hide the flash
	var flash1 = document.getElementById('landing1flash');
	var flash2 = document.getElementById('order1flash');
	var flash3 = document.getElementById('thanksflash');
	if (flash1) setTimeout ( 'hideFlash()', 110000 );
	if (flash2) setTimeout ( 'hideFlash()', 150000 );
	if (flash3) setTimeout ( 'hideFlash()', 100000 );
	
	$("#flshIntroVideo").flash({
	    src: '../flash/MGP_Welcome.swf',
	    width: 342,
	    height: 278
	})
	
	// links with a rel=external need to open in a popup
	$('A[rel="external"]').click( function() {
		var className = $(this).attr("class");
		var sizeArray = className.split(",");
		var width = sizeArray[0];
		var height = sizeArray[1];

        window.open( $(this).attr('href'),"Window1","menubar=no,width="+width+",height="+height+",toolbar=no" );
        return false;
    });
	
	// links with a rel=external need to open in a popup
	$('A[rel="externaltool"]').click( function() {
		var className = $(this).attr("class");
		var sizeArray = className.split(",");
		var width = sizeArray[0];
		var height = sizeArray[1];

        window.open( $(this).attr('href'),"Window1","menubar=0,width="+width+",height="+height+",toolbar=1,scrollbars=1" );
        return false;
    });
	
	// if our browser supports opacity then we set the glow in motion over the form button
	// if it doesn't then we hide the glow layer
	if (jQuery.support.opacity){
		fadeOutGlow();
	} else {
		$("#formbuttonglow").hide();
	}
});
function hideConfirmBoxes()
{
	$('.confirm ').slideUp('slow');
}
function hideErrorBoxes()
{
	$('.error ').slideUp('slow');
}
function fadeOutGlow(){
	$("#formbuttonglow").fadeOut("slow", function (){
		fadeInGlow();
	});
}
function fadeInGlow(){
	$("#formbuttonglow").fadeIn("slow", function (){
		fadeOutGlow();
	});
}
function hideFlash(){
	$("#btnFlashExit").remove();
	$("#landing1flash").remove();
	$("#landing2flash").remove();
	$("#order1flash").remove();
	$("#thanksflash").remove();
}
//The following function is used to identify the card type.
function getCardType(num) {
    var cctype = "";
    if (num.substring(0, 1) == '4')
        if ((num.length == '13') || (num.length == '16')) {
        cctype = "visa";
    }
    if ((num.substring(0, 2) >= '51') && (num.substring(0, 2) <= '55'))
        if (num.length == '16') {
        cctype = "mastercard";
    }
    if (num.substring(0, 4) == '6011')
        if (num.length == '16') {
        cctype = "discover";
    }
    if ((num.substring(0, 1) == '3') || (num.substring(0, 4) == '1800') || (num.substring(0, 4) == '2131'))
        if ((num.length == '15') || (num.length == '16')) {
        cctype = "jcb";
    }
    if ((num.substring(0, 2) == '34') || (num.substring(0, 2) == '37'))
        if (num.length == '15') {
        cctype = "amex";
    }
    if ((num.substring(0, 3) == '300') || (num.substring(0, 3) == '305') || (num.substring(0, 2) == '36') || (num.substring(0, 2) == '38'))
        if (num.length == '14') {
        cctype = "diners";
    }
    return cctype;
}