/*
 * File: functions.js
 * Author: Cosmin Calciu cosmin_calciu@pixelfind.net
 * Copyright (c) pixelfind.net 2009. All rights reserved.
 * Date: 07/07/2009
 * Version: 1.00
 * Email: office@pixelfind.net
 * Web: http://www.pixelfind.net
*/

var SITE_URL = 'http://localhost/pixelfind4/';
//
function relocate(url) {
  window.location = url;
}

function preloading() {
	for ( i = 0; i < preloading.arguments.length; i++ ) {
		my_images[i] = new Image();
		my_images[i].src = preloading.arguments[i];
	}
}

function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ( !filter.test(email) ) {
    return false;
  } else {
    return true;
  }
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
    var expires = "";
  }

  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
  
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
      c = c.substring(1,c.length);
    }

		if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length);
    }
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function delete_user(id) {
  var r = confirm('Are you sure you want to delete this user?');

  if ( r == true ) {
    window.location = 'admin_users.php?action=delete&id=' + id;
  }
}

function change_option(value, action, to_content) {
  if ( action == 1 ) {
    $('#'+ value + 'on').attr('src', 'images/checked.jpg');
    $('#'+ value + 'off').attr('src', 'images/not_checked.jpg');

    $('#' + to_content).val(action);
  } else {
    $('#'+ value + 'off').attr('src', 'images/checked.jpg');
    $('#'+ value + 'on').attr('src', 'images/not_checked.jpg');
    
    $('#' + to_content).val(action);
  }
}

function show_portfolio() {
  if ( $('#featured_projects').css('display') == 'none' ) {
     $('#featured_projects').fadeIn(500);
     $('#img_img').attr('src', 'images/arrow_up.gif');
   } else {
    $('#featured_projects').fadeOut(500);
     $('#img_img').attr('src', 'images/arrow_down.gif');
  }
}

function swtichPannel() {
  if ( $('#hiddenTable').css('display') == 'none' ) {
     $('#hiddenTable').show();
   } else {
    $('#hiddenTable').hide();
  }
}

function displayTourPanel(div) {
  if ( $('#tourPanel_' + div + '').css('display') == 'none' ) {
     $('#tourPanel_' + div + '').show();
   } else {
    $('#tourPanel_' + div + '').hide();
  }
}

function showCartiere(id_oras) {
  $.ajax({
     type: "POST",
     data: "id_oras=" + id_oras,
     url: "ajax_afisare_cartiere.php",
     success: function(msg){
       if(msg != '') {
         $('#cartier').removeAttr('disabled');
         $('#cartier').find('option').remove().end();
         $('#cartier').append(msg);
         showZone($('#cartier').val());
       } else {
         $('#cartier').find('option').remove().end();
         $('#cartier').append('<option>Cartier</option>');
         $('#cartier').attr('disabled', 'disabled');
         $('#zona').find('option').remove().end();
         $('#zona').append('<option>Zona</option>');
         $('#zona').attr('disabled', 'disabled');
       }
     }
   });
}

function showZone(id_cartier) {
  $.ajax({
     type: "POST",
     data: "id_cartier=" + id_cartier,
     url: "ajax_afisare_zone.php",
     success: function(msg){
       if(msg != '') {
         $('#zona').removeAttr('disabled');
         $('#zona').find('option').remove().end();
         $('#zona').append(msg);
       } else {
         $('#zona').find('option').remove().end();
         $('#zona').attr('disabled', 'disabled');
       }
     }
   });
}

function showCartiere_expanded(id_oras) {
  $.ajax({
     type: "POST",
     data: "id_oras=" + id_oras,
     url: "ajax_afisare_cartiere.php",
     success: function(msg){
       if(msg != '') {
         //$('#cartier').removeAttr('disabled');
         //$('#cartier_nume').removeAttr('disabled');
         //$('#cartier_add').removeAttr('disabled');
         $('#cartier').find('option').remove().end();
         $('#cartier').append(msg);
         showZone($('#cartier').val());
       } else {
         $('#cartier').find('option').remove().end();
         //$('#cartier').attr('disabled', 'disabled');
         //$('#cartier_nume').attr('disabled', 'disabled');
         //$('#cartier_add').attr('disabled', 'disabled');
       }
     }
   });
}

function showZone_expanded(id_cartier) {
  $.ajax({
     type: "POST",
     data: "id_cartier=" + id_cartier,
     url: "ajax_afisare_zone.php",
     success: function(msg){
       if(msg != '') {
         //$('#zona').removeAttr('disabled');
         //$('#zona_nume').removeAttr('disabled');
         //$('#zona_add').removeAttr('disabled');
         $('#zona').find('option').remove().end();
         $('#zona').append(msg);
       } else {
         $('#zona').find('option').remove().end();
         //$('#zona').attr('disabled', 'disabled');
         //$('#zona_nume').attr('disabled', 'disabled');
         //$('#zona_add').attr('disabled', 'disabled');
       }
     }
   });
}

function adauga(type) {
  
  var nume = $('#' + type + '_nume').val();

  if(type == 'oras') {
    if(nume != '') {
      $.ajax({
       type: "POST",
       data: "type=" + type + "&nume=" + nume,
       url: "ajax_adauga.php",
       success: function(msg){
         if(msg == 'used') {
           alert('Orasul este dejea in baza de date!');
           $('#' + type + '_nume').val('');
         } else {
           alert('Oras adaugat cu success!');
           $('#' + type + '_nume').val('');
         }
       }
     });
    }
  } else if (type == 'cartier') {
    if(nume != '') {
      var oras = $('#oras').val();
      $.ajax({
       type: "POST",
       data: "type=" + type + "&nume=" + nume + "&oras=" + oras,
       url: "ajax_adauga.php",
       success: function(msg){
         if(msg == 'used') {
           alert('Cartierul este dejea in baza de date!');
           $('#' + type + '_nume').val('');
         } else {
           alert('Cartier adaugat cu success!');
           $('#' + type + '_nume').val('');
         }
       }
     });
    }
  } else if (type == 'zona') {
    if(nume != '') {
      var cartier = $('#cartier').val();
      
      if(cartier != '') {
        $.ajax({
         type: "POST",
         data: "type=" + type + "&nume=" + nume + "&cartier=" + cartier,
         url: "ajax_adauga.php",
         success: function(msg){
           if(msg == 'used') {
             alert('Zona este dejea in baza de date!');
             $('#' + type + '_nume').val('');
           } else {
             alert('Zona adaugata cu success!');
             $('#' + type + '_nume').val('');
           }
         }
       });
      } else {
        alert('Selectati un cartier si un oras!');
      }
    }
  }
}

function sterge(type) {
  var nume = $('#' + type + '_nume').val();
  if(type == 'oras') {
    if(nume != '') {
      $.ajax({
       type: "POST",
       data: "type=" + type + "&nume=" + nume,
       url: "ajax_sterge.php",
       success: function(msg){
         if(msg == 'none') {
           alert('Orasul nu exista in baza de date!');
           $('#' + type + '_nume').val('');
         } else {
           alert('Orasul a fost sters cu success!');
           $('#' + type + '_nume').val('');
         }
       }
     });
    }
  } else if ( type == "cartier") {    
    if(nume != '') {
      var oras = $('#oras').val();
      $.ajax({
       type: "POST",
       data: "type=" + type + "&nume=" + nume + "&oras=" + oras,
       url: "ajax_sterge.php",
       success: function(msg){
         if(msg == 'none') {
           alert('Cartierul nu exista in baza de date!');
           $('#' + type + '_nume').val('');
         } else {
           alert('Cartierul a fost sters cu success!');
           $('#' + type + '_nume').val('');
         }
       }
     });
    }
  } else if ( type == "zona") {
    if(nume != '') {
      var cartier = $('#cartier').val();
      if(cartier != '') {
        $.ajax({
           type: "POST",
           data: "type=" + type + "&nume=" + nume + "&cartier=" + cartier,
           url: "ajax_sterge.php",
           success: function(msg){
             if(msg == 'none') {
               alert('Zona nu exista in baza de date!');
               $('#' + type + '_nume').val('');
             } else {
               alert('Zona a fost sters cu success!');
               $('#' + type + '_nume').val('');
             }
           }
         });
      } else {
        alert('Selectati un cartier si un oras!');
      }      
    }
  }
}

function addBookmark() {
  if( document.all ) {
    window.external.AddFavorite(location.href, document.title);
  } else {
    alert("Sorry, your browser doesn't support this");
  }
}



