function changeIframeSrc(url, frame_name) {

    frames[frame_name].location = url;
}

//Plugin detect
function showPluginDetectDiv(){
    var JavaStatus;
    var minVersion = '1,6,0';
    JavaStatus = PluginDetect.isMinVersion('Java', minVersion,'getJavaInfo.jar',[2,2,2]);

    if(JavaStatus == 1){
        $('#launch-recorder-div').show();
    }
    else{
        $('#java-installer-div').show();
        if (JavaStatus == 0){
            $('#java-installer-message').html("Java present in your system is of lower version. Java 1.6 is required to run the UserThoughts Feedback Recorder. Installation instructions are given below. Please complete them <strong>BEFORE Accepting this HIT</strong>.");
        }
        else if (JavaStatus == -0.2){
            $('#java-installer-message').html("Java installed but not enabled. Please follow instructions below, Please complete them <strong>BEFORE Accepting this HIT</strong>.");
        }
        else {
            $('#java-installer-message').html("Java 1.6 is required to run the UserThoughts Feedback Recorder. Installation instructions are given below. Please complete them <strong>BEFORE Accepting this HIT</strong>.");
        }
        $('#install-java-button').click(function(){
            window.open("http://javadl.sun.com/webapps/download/GetFile/1.6.0_13-b03/windows-i586/xpiinstall.exe");
        });
    }
}

//Flash message showing
function showFlashMessage(message_type, message_text){// FYI :message_type: "error"/"success"/"notice"
    $("#notice-div").find("p").remove();
    $("#notice-div").append("<p id='"+ message_type +"' class='"+ message_type +" span-18'>"+
        '<a id="flash-message" href="#">Close</a>'+ message_text +"</p>");
    $("#notice-div").show();
    $("#flash-message").click(function() {
        $("#notice-div").hide("blind",500);
        return false;
    });
}

// Datepicker
function setDatePicker(){
    $("#user_date_of_birth").datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2010',
        dateFormat : 'dd MM yy',
        showOn: 'button',
        buttonImage: '/images/calendar.gif',
        buttonImageOnly: true,
        beforeShow: function(){
            $("#user_date_of_birth").attr("readonly","readonly");
        },
        onClose: function(){
            $("#user_date_of_birth").removeAttr("readonly");
        }
    })
}

function setTesterSignupEffects(){
    //set effect for Tester signup button
    $("#Signup").click(function() {
        $("#give-feedback-text").hide();
        $("#get-feedback-text").hide();
        $('#mturk-signup-div').show("slide",500);
    });
    $("#disabled-tester-signup").click(function() {
        alert("This feature is not enabled. Please write to us at 'contactus@userthoughts.com' if you're interested in signing up as a Beta tester.");
    });
}

function setCustomerSignupFormEffects(){
    //set effect for customer signup button
    $("#customer-signup").click(function() {
        $("#give-feedback-text").hide();
        $("#get-feedback-text").hide();
        $('#get-feedback-form-div').show("slide",500);
        setTimeout(function(){
            $('#customer_email').focus();
        }, 1000);
        return false;
    });
    $("#disabled-customer-signup").click(function() {
        alert("This feature is not enabled. Please write to us at 'contactus@userthoughts.com' if you're interested in signing up as a Beta customer.");
    });
}

function closeMturkSignUp(){
    if(confirm('Are you sure you want to cancel?')) {
        $('#mturk-signup-div').hide();
        $("#give-feedback-text").show();
        $("#get-feedback-text").show();
    }
}

function closeGetFeedbackForm(){
    if(confirm('Are you sure you want to cancel?')) {
        $('#get-feedback-form-div').hide();
        $("#get-feedback-text").show();
        $("#give-feedback-text").show();
        $("div.formError").each(function(){
            $(this).hide();
        })
    }
}

function showDialogBox(element_id, width){
    $(element_id).dialog("open");
    $(element_id).dialog({
        width: width,
        bgiframe: true,
        modal: true,
        buttons: {
            OK: function() {
                $(this).dialog('close');
            }
        }
    });
}

function showDialogBoxWithTitle(element_id, width,title){
    $(element_id).dialog("open");
    $(element_id).dialog({
        width: width,
        title: title,
        bgiframe: true,
        modal: true,
        buttons: {
            OK: function() {
                $(this).dialog('close');
            }
        }
    });
}

function hideFormErrorOnFocus(formId){
    $("#"+formId).find("input").bind("focus",function(){
        $(this).parent().parent().parent().find(".formError").hide();
    });
}

function validateCustomerSignUpForm(){
    $("#get-feedback-form").validate({//http://plugins.jquery.com/project/validate
        rules:{
            'user[email]':{
                required: true,
                email: true
            },
            'user[password]':{
                required: true,
                minlength: 6
            },
            'user[password_confirmation]':{
                required: true,
                minlength: 6,
                equalTo: "#customer_password"
            }
        },
        messages:{
            'user[email]': {
                required: "Please enter a valid Email Address"
            },
            'user[password]': {
                required: "Provide a password",
                rangelength: jQuery.format("Enter at least {0} characters")
            },
            'user[password_confirmation]':{
                required: "Repeat your password",
                equalTo: "Enter the same password as above"
            },
            'user[first_name]':{
                required: "Enter your First Name"
            },
            'user[last_name]':{
                required: "Enter your Last Name"
            },
            'user[company_name]':{
                required: "Enter your Company Name"
            }
        },
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo ( element.parent().parent().parent().find("p.status") );
            else if ( element.is(":checkbox") )
                error.appendTo ( element );
            else
                error.appendTo( element.parent().parent().parent().find("p.status"));
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            label.html("<img src='/images/accept.png' alt='accept'/>").addClass("checked");
        }
    });
}
function showCustomerTypeInputFields(){
    $("input[type='radio']").click(function(){
        showCustomerTypeInputFields();
    });
    if($("input[@name='user[customer_type]']:checked").val() == "individual"){
        $("#company-customer-type").hide('blind');
        $("#individual-customer-type").show('blind');
    }else if($("input[@name='user[customer_type]']:checked").val() == "company"){
        $("#individual-customer-type").hide('blind');
        $("#company-customer-type").show('blind');
    }    
}

function addRemoveRulesForCustomerForm(){
    if($("input[@name='user[customer_type]']:checked").val() == "individual"){
        $("#customer_company_name").rules("remove", "required");
        $("#customer_first_name").rules("add", "required");
        $("#customer_last_name").rules("add", "required");
    }else if($("input[@name='user[customer_type]']:checked").val() == "company"){
        $("#customer_first_name").rules("remove", "required");
        $("#customer_last_name").rules("remove", "required");
        $("#customer_company_name").rules("add", "required");
    }
}

function validateCustomerEditForm(){
    $("#update-account-form").validate({//http://plugins.jquery.com/project/validate
        rules:{
            'user[company_name]':{
                required: true
            },
            'user[first_name]':{
                required: true
            },
            'user[last_name]':{
                required: true
            }
        },
        messages:{
            'user[first_name]':{
                required: "Enter your First Name"
            },
            'user[last_name]':{
                required: "Enter your Last Name"
            },
            'user[company_name]':{
                required: "Enter your Company Name"
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo( element.parent().parent().parent().find("p.status"));
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) {
            label.html("<img src='/images/accept.png' alt='accept'/>").addClass("checked");
        }
    });
}
function noJavaDetected(){
  $("#recorder-div").hide();
  $("#no-java").show();
}

function appletSuccessfullyLoaded(){
  location.href = "/quick_video?launch_recorder=true"
}
$(document).ready(function(){
    $("#customer-signup-cancel").click(function(){
        $("#customer_email").val = '';
        $("#customer_password").val = '';
      });
  });

