/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//////////////////////////////////////////////////////////////////////////////
// abrePopup
//////////////////////////////////////////////////////////////////////////////
function abrePopUp(url,janela,scroll,W,H,T,L){
	if (W==0){
		W = eval(screen.width)-30;
	}
	if (H==0){
		H = eval(screen.height)-90;
	}
	if (T==0){
		T = Math.round((eval(screen.height)-H)/2);
	}
	if (L==0){
		L = Math.round((eval(screen.width)-W)/2);
	}
	if (wnd=window.open(url,janela,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable=yes,copyhistory=no,width="+W+",height="+H+",top="+T+",left="+L)){
		return wnd;	
	}else{
		return false;	
	}
}

function validateAndSubmit() {
	if (!jQuery('#checkbox_contrato').attr('checked')) {
		alert("É preciso ler e concordar com os termos do contrato.");
	}
	else {
	   jQuery("#form_cadastro").submit();
	}
	return false;
}


jQuery.noConflict();
jQuery(document).ready(function(){
  $j = jQuery;

  $j('#busca img').click(function(){
    $j(this).parents('form:first').submit();
    return false;
  });
	
	// Carrega planos de anuncio
	if (jQuery('#input_anuncio')) {
	  jQuery("#input_anuncio")
		  .load("/index.php?action=loadAnuncioOptions&rd="+Math.random()*9999999)
			.change(function () {
				jQuery('#input_valor').val('');
			  var value = jQuery(this).val();
				if (value != '') {
					var splited_value = value.split('|');
					var price = splited_value[2];
					var formated_value = price.replace('.', ',');
					if (formated_value != '') {
						jQuery('#input_valor').val('R$ '+formated_value);
					}
				}
			})
			.change();
	}
	
	$j("input[name='checkbox_contrato']").attr('id','checkbox_contrato');
  
  function enableBtnEnviarAnuncio() {
    $j("#btn_enviar_anuncio").bind("click", function(e){
        $j(this).parents('form:first').submit();
    });
  }
  
  function disableBtnEnviarAnuncio() {
    $j("#btn_enviar_anuncio").unbind("click");
  }
  
  function appendFilesListedInArquivosField() {
    var files = $j("#arquivos_field").text().split("#");
    if (files.length > 0) {
      for (var i = 0; i < files.length; i++) {
        var file_info = files[i].split('|');
        var filename = file_info[0];
        $j('#lista_fotos')
            .css({ margin: '6px', display: 'block'})
            .append('<div>'+filename+'</div>');     
      } 
    }
  }
  
  enableBtnEnviarAnuncio();
  appendFilesListedInArquivosField();
  
  var uploadedFiles = 0;
  new Ajax_upload('#upload_button', {
    // Location of the server-side upload script
    action: '/upload.php',
    // File upload name
    name: 'foto',
    // Additional data to send
    data: {
    },
    // Fired when user selects file
    // You can return false to cancel upload
    // @param file basename of uploaded file
    // @param extension of that file
    onSubmit: function(file, ext) {
      if (! (ext && /^(jpg|png|jpeg|gif|doc)$/.test(ext))){
        // extension is not allowed
        alert('Este tipo de arquivo não é permitido.');
        // cancel upload
        return false;
      }
      else {
        disableBtnEnviarAnuncio();
        $j('#lista_fotos')
        .css({ margin: '6px', display: 'block'})
        .append('<div class="uploading">Uploading...</div>');                
      }
      
    },
    // Fired when file upload is completed
    // @param file basename of uploaded file
    // @param response server response
    onComplete: function(file, response) { 
      if (response != 'error') {
        var aux = response.split('|');
        var filename = aux[0];
        var filepath = aux[1];
        $j('#lista_fotos .uploading:eq(0)').text(filename).removeClass('uploading');
        $j('#arquivos_field').val($j('#arquivos_field').val() + response);
      }
      else {
        alert('Falha no envio do arquivo, favor tentar novamente.');
      }
      enableBtnEnviarAnuncio();
    }
  });  
  
});


