//$('#LoadPage').load('Default.aspx');
$(document).ready(function(){
	$('a#priceshow').click(function() {
			$('#priceedit').toggle(400);
			return false;
	  });	
	$("#cata_id").change(function(){
    		document.cat_form.submit();
	});
	//select all the a tag with name equal to modal
		$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
		var url = $(this).attr('id');
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		//transition effect		
		$('#mask').fadeIn(200);	
		$('#mask').fadeTo("slow",0.8);	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		//Set the popup window to center
		$(id).css('top',  (winH/2-$(id).height()/2)-50);
		$(id).css('left', (winW/2-$(id).width()/2)-10);
		//transition effect
		$(id).load("/parts/" + url + "") ;
		$(id).fadeIn(300); 
	});
	//if close button is clicked
	$('.window .closeit').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask').hide();
		$('.window').hide();
	});		
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});		
	

	
	$("a#savepriceform").click(function() {
		$('div.success').fadeOut('slow');
		var thevdata    		= $('#price_form').serialize();
		//alert(thevdata);
			$.ajax({
				type: "POST",
				url: "/parts/ajax_functions.php",
				data: thevdata ,
				success: function(id){
					if (id>0){
						var url = '/parts/ajax_prices.php?id=' + id ;
						$('#pricetable').load(url);
						$('div.success').fadeIn('slow');
						$('#price_form :input:text').val('');
						$('#priceedit').fadeOut('slow');
					}else{
						alert('please review the fields, All Fields Required  ' + id + '  ' );
					}
					
				}
			});
		return false;
	});
	
	$("a#search_button").click(function() {
		$('#result_div').fadeOut();
		var thevdata   = $("#search_form").serialize();
		//alert(thevdata);
			$.ajax({
				type: "POST",
				url: "/parts/ajax_searches.php",
				data: thevdata ,
				success: function(result){
				$('#result_div').html(result); 
				$('#result_div').fadeIn('slow');
					
				}
			});
		return false;
	});
	
	
	
	$("a#saveuser").click(function() {
		//$('div.success').fadeOut();
		var id    	= $('#id').attr('value');
		var name    	= $('#name').attr('value');
		var phone    	= $('#phone').attr('value'); 
		var email    	= $('#email').attr('value'); 
		var address    	= $('#address').attr('value'); 
		var city    	= $('#city').attr('value'); 
		var region    	= $('#region').attr('value'); 
		var country    	= $('#country').attr('value');
		var zip    		= $('#zip').attr('value');
		var password    = $('#password').attr('value');
		var cnumber    = $('#cnumber').attr('value');
		var languaje    = $('#languaje').attr('value');
		
		
			$.ajax({
				type: "POST",
				url: "/parts/ajax_functions.php",
				data: "name="+ name 
						+ "& id=" + id 
						+ "& action=saveuser" 
						+ "& cnumber=" + cnumber
						+ "& phone=" + phone
						+ "& email=" + email
						+ "& address=" + address
						+ "& city=" + city
						+ "& region=" + region
						+ "& country=" + country
						+ "& zip=" + zip
						+ "& newpassword=" + password
						+ "& languaje=" + languaje
						,
				success: function(id){
					var url = '/parts/ajax_user.php?id=' + id ;
					$('#clidetaledit').toggle('slow');
					$('#clidetal').toggle('slow');
					$('#clidetal').load(url);
					
				}
			});
		return false;
	});
	
	$("a#saverights").click(function() {
		$('#successdiv').fadeOut('slow');
		$('#errordiv').fadeOut('slow');
		var thedata    		= $("#rights_form :input[value]").serialize();
			$.ajax({
				type: "POST",
				url: "/parts/ajax_functions.php",
				data: "action=saverights& " + thedata,
				success: function(response){
					if (response='oskey'){
						$('#successdiv').toggle('slow');
						$('#user_rights').toggle('slow');
					}else{
						alert(response);
						$('#errordiv').toggle('slow');
					}
				}
			});
		return false;
	});
	
});	
	
function printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}
function checkAll(field){
	for (i = 0; i < field.length; i++)
		field[i].checked = true;
	return false;
}
function deleteprices(price_id,product_id) {
		$.ajax({ 
					type: "POST", 
					url: "/parts/ajax_functions.php", 
					data: "price_id="+ price_id 
							+ "& product_id="  + product_id
							+ "& ajax_del_price=1" 
							,
					error:function(xhr,err,e){ alert( "Error: " + err ); }	,
					success: function(result){	
							var url = '/parts/ajax_prices.php?id=' + result ;
							$('div.success').hide();
							$('div.success').text('File deleted succesfully');	
							$('div.success').fadeIn();
							$('#pricetable').load(url);
					}
				});
		}
$(document).ready(function(){
 $("#checkboxall").click(function()
  {
   var checked_status = this.checked;
   $("input[name=check_row[]]").each(function()
   {
    this.checked = checked_status;
   });
  });
});

function confirmDel(){
	var agree=confirm("Are you Sure?");
	if (agree)
		return true ;
	else
		return false ;
}
function confirmDeltext(text){
	var agree=confirm(text);
	if (agree)
		return true ;
	else
		return false ;
}


function ConfirmJava(){
	var agree=confirm("Seguro que quiere eliminarlo permanentemente?");
	if (agree)
		return true ;
	else
		return false ;
}
function ConfirmAjaxJava(form){
	var agree=confirm("Seguro que quiere eliminarlo/s permanentemente?");
	if (agree)
		$('#'+form).submit();
	else
		return false ;
}


function makeSublist(parent,child,isSubselectOptional,childVal){
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));
	var parentValue = $('#'+parent).attr('value');
	$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child).val(childVal).attr('selected','selected');
	$('#'+parent).change(function(){
		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
		if(isSubselectOptional) $('#'+child).prepend("<option value='' selected='selected'> -- Select -- </option>");
		$('#'+child).trigger("change");
		$('#'+child).focus();
	});
}	




//VALODATIONS PROTOTYPES o no ES


function Validator(form) {
    this.form = form;
    this.valid = true;
    this.text = "";
}

Validator.prototype.form;
Validator.prototype.valid;
Validator.prototype.text;

Validator.prototype.getForm = function() {
    return this.form;
}

Validator.prototype.reset = function() {
	this.valid = true;
	this.text = "";
	$(this).removeClass("error");
	
}

Validator.prototype.empty = function(q) {
	for (i = 0; i < q.length; i++) {
	   if (q.charAt(i) != " ") {
		   $(q).addClass("error");
	   	return false
	   }
	}
	$(q).removeClass("error");
	return true
}

Validator.prototype.required = function(control,msg) {
	if (this.empty(control.value) == true) {
		this.valid = false;
		this.text = this.text+""+msg+"\r\n";
		$(control).addClass("error");
		return false;
	}
	else{
		$(control).removeClass("error");
		return true;
	}
}

Validator.prototype.checked = function(control,msg) {
	if (control.checked == false) {
		this.valid = false;
		this.text = this.text+""+msg+"\r\n";
		return false;
	}
	else
		return true;
}

Validator.prototype.date = function(control,msg) {
   var expr = /^(\d{4})[/-](0[1-9]|1[012])[/-]([012][1-9]|3[01])$/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+""+msg+"\r\n";
      return false;
   }
   else
      return true;
}
Validator.prototype.numeric = function(control,msg) {
  //check for numeric characters
   var expr =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+""+msg+"\r\n";
      return false;
   }
   else
      return true;
}
Validator.prototype.integrer = function(control,msg) {
  //check for integrer numbers
   var expr =  /(^-?\d\d*$)/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+""+msg+"\r\n";
      return false;
   }
   else
      return true;
}
Validator.prototype.expiredate = function(control,msg) {
  //check for integrer numbers
  var expr =  /^\d{1,2}\-\d{4}$/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+""+msg+"\r\n";
      return false;
   }
   else
      return true;
}

Validator.prototype.datetime = function(control,msg) {
   var expr = /^(\d{4})[/-](0[1-9]|1[012])[/-]([012][1-9]|3[01])\s*(\s(0\d|1\d|2[0-3]):([0-5]\d)(:([0-5]\d))?)?$/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+""+msg+"\r\n";
      return false;
   }
   else
      return true;
}

Validator.prototype.email = function(control,msg) {
	if ((control.value.indexOf("@") == -1) || (control.value.indexOf(".") == -1)) {
		this.valid = false;
		this.text = this.text+""+msg+"\r\n";
		$(control).addClass("error");
		return false;
	}
	else{
		$(control).removeClass("error");
		return true;
	}
}

Validator.prototype.equal = function(campoa,campob,msg) {
	if (campoa.value != campob.value) {
		this.valid = false;
		this.text = this.text+""+msg+"\r\n";
		$(campoa).addClass("error");
		$(campob).addClass("error");
		return false;
	}
	else{
		$(campoa).removeClass("error");
		$(campob).removeClass("error");
		return true;
	}
}

Validator.prototype.validate = function() {
	if (this.valid == false) {
		alert("Error/s found:\r\n\r\n"+this.text+"\r\n");

	}
	var valid = this.valid;
	this.reset();
	return valid;
}

//############################################################
//#############     INMOBITOOLS        #######################
//############################################################

// Cookie TBox

var dbug = 0;
//var timeToKeep = 7200000 ; // two hours
//var expires = new Date();
//expires.setTime(expires.getTime() + timeToKeep);
var expires = new Date();
var timeToKeep = expires.getTime() + (30 * 24 * 60 * 60 * 1000);
expires.setTime(timeToKeep);

function posinarray(ar,item) {
	var myfind=-1;
	for (var i=1; i<ar.length; i++) {
		if (ar[i]==item) myfind=i;
	}
	if(myfind == -1) {
		item = item.replace(/ /g, '+');
		for (var i=1; i<ar.length; i++) {
			if (ar[i]==item) myfind=i;
		}
	}
	if(myfind == -1) {
		item = item.replace(/ /g, '%20');
		for (var i=1; i<ar.length; i++) {
			if (ar[i]==item) myfind=i;
		}
	}
	return myfind;
}

function d_a(ary) {
	var beg = next_entry(ary) - 1;
	for (var i = beg ; i > -1; i--) {
		ary[i] = null;
	}
	}

function init_array() {
	var ary = new Array(null);
	return ary;
	}

function set_cookie(name,value,expires) {
//	if (!expires) expires = new Date();
//#############################################################fonsi changes
	if (!expires) {
		var expires = new Date();
		var timeToKeep = expires.getTime() + (30 * 24 * 60 * 60 * 1000);
		expires.setTime(timeToKeep);
	}
//#############################################################fonsi changes
	document.cookie = name + '=' + escape(value) + ';	expires=' + expires.toGMTString() + '; path=/';
	}

function get_cookie(name) {
	 var dcookie = document.cookie;
	 var cname = name + "=";
	 var clen = dcookie.length;
	 var cbegin = 0;

	 while (cbegin < clen) {
	 	  var vbegin = cbegin + cname.length;
      if (dcookie.substring(cbegin, vbegin) == cname) {
    	   var vend = dcookie.indexOf (";", vbegin);
    	   if (vend == -1) vend = clen;
    	   return unescape(dcookie.substring(vbegin, vend));
    	   }
      cbegin = dcookie.indexOf(" ", cbegin) + 1;
      if (cbegin == 0) break;
      }
    return null;
   }

function del_cookie(name) {
	 document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
	 return true;
	 }

function get_array(name, ary) {
	 d_a(ary);
	 var ent = get_cookie(name);
	 if (ent) {
	 	  i = 1;
	    while (ent.indexOf('^') != '-1') {
	 	     ary[i] = ent.substring(0,ent.indexOf('^'));
	 	     i++;
         ent = ent.substring(ent.indexOf('^')+1, ent.length);
         }
      }
    }

function set_array(name, ary, expires) {
	 var value = '';
	 for (var i = 1; ary[i]; i++) {
	 	   value += ary[i] + '^';
	 	   }
	 set_cookie(name, value, expires);
	 }

function del_entry(name, ary, pos, expires) {
	var value = '';
	get_array(name, ary);
	for (var i = 1; i < pos; i++) {
		value += ary[i] + '^';
		}
	for (var j = pos + 1; ary[j]; j++) {
		value += ary[j] + '^';
		}
	set_cookie(name, value, expires);
 	}

function next_entry(ary) {
	  var j = 0;
	  for (var i = 1; ary[i]; i++) {
	  	j = i
	  }
	  return j + 1;
	  }

function debug_on() {dbug = 1;}

function debug_off() {dbug = 0;}

function dump_cookies() {
	if (document.cookie == '') document.write('No Cookies Found');
	else
		{
			thisCookie = document.cookie.split('; ');
			for (i=0; i<thisCookie.length; i++)
			   {
			   	document.write(thisCookie[i] + '<br \/>');
			   }
		}
	}

function addproperty(propertylist,ref) {
  var myarray = init_array(); // if it isn't already defined

  get_array(propertylist, myarray);
  if ( posinarray(myarray,ref) == -1) myarray.push(ref);
  set_array(propertylist, myarray, expires);
}

function removeproperty(propertylist,ref) {
  var myarray = init_array(); // if it isn't already defined
  var todelete = -1;

  get_array(propertylist, myarray);

  todelete = posinarray(myarray,ref);
  if (todelete != -1) myarray.splice(todelete,1);

  set_array(propertylist, myarray, expires);
}

function isselected(propertylist,ref) {  // ,resultdiv) {
	var myarray = init_array(); // if it isn't already defined
  var myfind=-1;

  get_array(propertylist, myarray);
  myfind = posinarray(myarray,ref);
  //if (resultdiv)
    // if (myfind == -1) resultdiv.innerHTML='NOOO!'; else resultdiv.innerHTML='SIIII!';
  return myfind;
}


