function SetNewSizes(div)
{
	$("#units option[value=1]").attr("selected",true);
    var value = $('#' + div).val();

    if(value == "1")
    {
        $('#resize_width').val("400");
        $('#resize_height').val("300");
    }

    if(value == "2")
    {
        $('#resize_width').val("640");
        $('#resize_height').val("480");
    }

    if(value == "3")
    {
        $('#resize_width').val("800");
        $('#resize_height').val("600");
    }

    if(value == "4")
    {
        $('#resize_width').val("1024");
        $('#resize_height').val("768");
    }

    if(value == "5")
    {
        $('#resize_width').val("1600");
        $('#resize_height').val("1200");
    }

    if(value == "6")
    {
        $('#resize_width').val("");
        $('#resize_height').val("");
    }
}

function SetNewSizesTemp(width, height)
{
	var landscape;
	var ar = (width / height);
	
	ar >= 1 ? landscape = true : landscape = false;
	$("#units option[value=1]").attr("selected",true);

	switch( $("#preset").val() )
	{
		case "1":

			if( landscape )
			{
				$("#resize_width").val("400");
				$("#resize_height").val( Math.round( (400 / ar), 2).toString() );
			}
			else
			{
				$("#resize_width").val( Math.round( (400 * ar), 2 ).toString() );
				$("#resize_height").val("400");
			}

		break;

		case "2":

			if( landscape )
			{
				$("#resize_width").val("640");
				$("#resize_height").val( Math.round( (640 / ar), 2).toString() );
			}
			else
			{
				$("#resize_width").val( Math.round( (640 * ar), 2 ).toString() );
				$("#resize_height").val("640");
			}

		break;

		case "3":

			if( landscape )
			{
				$("#resize_width").val("800");
				$("#resize_height").val( Math.round( (800 / ar), 2).toString() );
			}
			else
			{
				$("#resize_width").val( Math.round( (800 * ar), 2 ).toString() );
				$("#resize_height").val("800");
			}

		break;

		case "4":

			if( landscape )
			{
				$("#resize_width").val("1024");
				$("#resize_height").val( Math.round( (1024 / ar), 2).toString() );
			}
			else
			{
				$("#resize_width").val( Math.round( (1024 * ar), 2 ).toString() );
				$("#resize_height").val("1024");
			}

		break;

		case "5":

			if( landscape )
			{
				$("#resize_width").val("1600");
				$("#resize_height").val( Math.round( (1600 / ar), 2).toString() );
			}
			else
			{
				$("#resize_width").val( Math.round( (1600 * ar), 2 ).toString() );
				$("#resize_height").val("1600");
			}

		break;

		default:

			$("#resize_width").val( width.toString() );
			$("#resize_height").val( height.toString() );

		break;
	}
}

function SetNewSizesPercent(div)
{
    var value = $('#' + div).val();
    var units = $('#units').val();

    if(value == "1")
    {
        $('#resize_width').val("400");
        $('#resize_height').val("300");
    }

    if(value == "2")
    {
        $('#resize_width').val("640");
        $('#resize_height').val("480");
    }

    if(value == "3")
    {
        $('#resize_width').val("800");
        $('#resize_height').val("600");
    }

    if(value == "4")
    {
        $('#resize_width').val("1024");
        $('#resize_height').val("768");
    }

    if(value == "5")
    {
        $('#resize_width').val("1200");
        $('#resize_height').val("1600");
    }

    if(value == "6")
    {
        $('#resize_width').val("");
        $('#resize_height').val("");
    }
}

function CheckOutputFormat()
{
    if( $('#type').val() != "1" )
    {
        return false;
    }
    else if( $('#type').val() != "2" )
    {
        return false;
    }
    else if( $('#type').val() != "3" )
    {
        return false;
    }
    else
    {
        return true;
    }
}

function SetRotateAngle(id)
{
    var value = $('#'+id).val();

	switch(value)
	{
		case "-90":		$("#rotateAngle").val("-90");	break;
		case "90":		$("#rotateAngle").val("90");	break;
		case "180":		$("#rotateAngle").val("180");	break;
	}
}

function UpdateFinfo()
{
	$("#finfo_msg").html("<strong>Please upload a photo in Step 1.</b>");
}

function UpdateHeight(aspect_checkbox, units_select)
{
    var keepAr;
    var units = parseInt($('#' + units_select).val());
    var px_ar = parseFloat($('#image_ar').val());
    var width;
    
    if($('#' + aspect_checkbox).is(':checked')) {
        keepAr = true;
    } else {
        keepAr = false;
    }

    switch(units)
    {
        case 1:
            width = parseFloat($('#resize_width').val());
            break;

        case 2:
            width = parseFloat($('#resize_width').val());
            break;
    }

    if(keepAr == true) {

        if(units == 1) {
            h = (width / px_ar);
            h = Math.round(h, 2);
        } else {
            h = (width / 1);
            h = Math.round(h, 2);
        }
		
		$('#resize_height').val(h.toString());
    }
}

function UpdateWidth(aspect_checkbox, units_select)
{
    var keepAr;
    var units = parseInt($('#' + units_select).val());
    var px_ar = parseFloat($('#image_ar').val());
    var height;

    if($('#' + aspect_checkbox).is(':checked')) {
        keepAr = true;
    } else {
        keepAr = false;
    }

    switch(units)
    {
        case 1:
            height = parseFloat($('#resize_height').val());
            break;

        case 2:
            height = parseFloat($('#resize_height').val());
            break;
    }

    if(keepAr) {
        if(units == 1) {
            w = (height * px_ar);
            w = Math.round(w, 2);
        } else {
            w = (height * 1);
            w = Math.round(w);
        }

		$('#resize_width').val(w.toString());
    }
}

function UpdateSizes(id, org_w, org_h)
{
    var ini_w = org_w;
    var ini_h = org_h;
    var value = $('#' + id).val();

    switch(parseInt(value))
    {
        case 1:
            $('#resize_width').val(ini_w);
            $('#resize_height').val(ini_h);
            break;

        case 2:
            $('#resize_width').val('100');
            $('#resize_height').val('100');
            break;
    }
}

function ToggleQuality()
{
    if( $('#conversionFormat').val() == '7') {
        $('#convert_qlty').removeAttr("disabled");
        $('#convert_qlty').attr("enable", "enable");
    } else {
        $('#convert_qlty').removeAttr("enable");
        $('#convert_qlty').attr("disabled", "disabled");
    }
}

function ToggleQuality2(format, quality)
{
    if( $('#' + format).val() == '1') {
        $('#' + quality).removeAttr("disabled");
        $('#' + quality).attr("enable", "enable");
    } else {
        $('#' + quality).removeAttr("enable");
        $('#' + quality).attr("disabled", "disabled");
    }
}

function CropFormValidate()
{
    if($('#uploadedfile').val().length != 0)
    {
        AjaxUploadPicture('uploadedfile');
        return true;
    }
    else
    {
        window.alert('eroare ba!');
        return false;
    }
}

function SubmitConvert(file, ext, format, quality)
{
	if( $('#' + format).val() !== null && $('#' + quality).val() !== null)
	{
		window.location = 'process.php?module=convert-it&file=' + file + '&ext=' + ext + '&format=' + $('#' + format).val() + '&quality=' + $('#' + quality).val();
	}
}

function SubmitCrop(file, ow, oh, ext)
{
	window.location = 'process.php?module=crop-it&file='+ file +'&ow='+ow+'&oh='+oh+'&x=' + $('#x').val() + '&y='+ $('#y').val() +'&w=' + $('#w').val() + '&h=' + $('#h').val() + '&ext='+ext+'&format='+ $('#crop_format').val() +'&quality=' + $('#crop_qlty').val();
}

function ToggleCheckIt(id)
{
	if( !$('#' + id).is(':checked') ) {
		$('#' + id).removeAttr("checked");
		$('#' + id).attr("checked", "checked");
	}
	
	switch( id )
	{
		case "selectAngle3":	$("#rotateAngle").val("180");	break;
		case "selectAngle2":	$("#rotateAngle").val("-90");	break;
		case "selectAngle1":	$("#rotateAngle").val("90");	break;
	}
}

function UpdateRotationAngle(val)
{
	$('#rotateAngle').val(val);
}

function SubmitRotate(file, ext)
{
	var angle = $('#rotateAngle').val();
	
	if( angle !== null )
	{
		window.location = 'process.php?module=rotate-it&file=' + file + '&ext=' + ext + '&angle=' + angle + '&format=' + $('#rotate_format').val() + '&quality=' + $('#rotate_qlty').val();
	}
}

function ShowUploadingAnimation()
{
	$('#finfo_msg').html("<div style=\"padding-top: 15px; height: 50px; visibility: visible;\" align=\"center\"><img id=\"uploading-img\" src=\"images/uploading.gif\" border=\"0\"></div>");
	$("#uploading-img").attr({ src: "images/uploading.gif"});

}

function showSurveyForm(id)
{
     $('#' + id).modal();
}

function CloseNotification()
{
     $.modal.close();
	 window.location = 'index.php';
}

function CheckCaptcha(value, div)
{
    var ajaxres = $.ajax(
    {
        type: "GET",
        url: "http://online.batchphoto.com/inc/php/sendmail.php?module=captcha&val=" + value,
        async: false
    }).responseText;

    if(div != null)
    {
         if(typeof(ajaxres) != null)
         {
             $("#" + div).html(ajaxres);
         }
    }
}

function CheckIfEmpty(value, div)
{
    var ajaxres = $.ajax(
    {
        type: "GET",
        url: "http://online.batchphoto.com/inc/php/sendmail.php?module=emptycheck&val=" + value,
        async: false
    }).responseText;

    if(typeof(ajaxres) != null)
    {
        $("#" + div).html(ajaxres);
    }
}

function CheckEmail(value, div)
{
    var ajaxres = $.ajax(
    {
        type: "GET",
        url: "http://online.batchphoto.com/inc/php/sendmail.php?module=emailcheck&val=" + value,
        async: false
    }).responseText;

    if(typeof(ajaxres) != null)
    {
        $("#" + div).html(ajaxres);
    }
}

function ajaxCaptcha(value)
{
     var current = $.ajax( {
          type: "GET",
          url: "http://online.batchphoto.com/inc/php/sendmail.php?module=returnCaptcha",
          async: false
     } ).responseText;

     if(current == value.toUpperCase())
     {
          return true;
     }
     else
     {
          return false;
     }
}

function isEmpty(value)
{
     if(value.length == 0 || value.length == null)
     {
          return true;
     }
     else
     {
          return false;
     }
}

function isEmail(value)
{
     var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
     return emailPattern.test(value);
}

function ValidateAndSend(div)
{
     var feedbackSubject	= $('#feedbackSubject').val();
     var feedbackEmail		= $('#feedbackEmail').val();
     var feedbackMessage	= $('#feedbackMessage').val();
     var captcha			= $('#code').val();
     var errors				= 0;
     var dataString			= 'feedbackSubject=' + feedbackSubject + '&feedbackEmail=' + feedbackEmail + '&feedbackMessage=' + feedbackMessage + '&captcha=' + captcha;

     if( isEmpty( feedbackSubject ) )
     {
          $('#feedbackSubjectError').html("<span style=\"color: red\">Please enter a subject.</span>");
          errors++;
     }

	 if( isEmail( feedbackEmail ) !== true )
     {
          $('#feedbackEmailError').html("<span style=\"color: red\">Please enter a valid email address.</span>");
          errors++;
     }

     if( isEmpty( feedbackMessage ) )
     {
          $('#feedbackMessageError').html("<span style=\"color: red\">Please specify a title.</span>");
          errors++;
     }

     if( ajaxCaptcha( captcha ) == false )
     {
          $('#captchaErrorHolder').html("<span style=\"color: red\">Verification failed.</span>");
          errors++;
     }

     if(errors == 0)
     {
          var ajax = $.ajax({
              type: "POST",
              url: "http://online.batchphoto.com/inc/php/sendmail.php?module=sendmail",
              data: dataString,
              async: false
          }).responseText;

          $('#' + div).html(ajax);

          errors = 0;
     }
}