/* Уже добавленные в корзину */
function disableAddToBasket(id, mess) {
	var strBasketID = $("#basket_id").text();
	if(strBasketID != "") 
		var arBasket = explode(",", strBasketID);
	else 
		return false;
		
	$("a.addcart").each(function() {
		var ID = explode("id_", $(this).attr("id"));
		if(in_array(ID[1], arBasket)) {
			$(this).addClass("in_basket");
			$(this).text(mess);
			$(this).attr("title", "");
			$(this).attr("href",'javascript:void(0)');	
		} 
	});	
}

/* Уже добавленные в сравнение */
function disableAddToCart() {
	var strCompareID = $("#compare_id").text();
	if(strCompareID != "") 
		var arCompareID = explode(",", strCompareID);
	else 
		return false;
	
	$("p.addcompare a").each(function() {
		var ID = explode("compare_id_", $(this).attr("id"));
		if(in_array(ID[1], arCompareID)) {
			$(this).parent().attr("class", "delcompare");
			new_href = str_replace("ADD_TO_COMPARE_LIST", "DELETE_FROM_COMPARE_LIST", $(this).attr("href"));
			$(this).attr("href", new_href);	
			$(this).text("Удалить из сравнения");	
		} 
	});
}



function isValidEmailAddress(emailAddress)
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

/**** функция in_array ****/
function in_array(what, where) {
    for(var i=0; i<where.length; i++)
        if(what == where[i]) 
            return true;
    return false;
}

/*** функция explode ***/
function explode( delimiter, string ) {
	var emptyArray = { 0: '' };	
	if ( arguments.length != 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined' )
	{
		return null;
	}
	if ( delimiter === '' || delimiter === false || delimiter === null )
	{
		return false;
	}
	if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' )
	{
		return emptyArray;
	}
	if ( delimiter === true ) {
		delimiter = '1';
	}
	return string.toString().split ( delimiter.toString() );
}

/*** функция implode ***/
function implode( glue, pieces ) {
	return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

/*** функция array_keys ***/
function array_keys( input, search_value, strict ) {	// Return all the keys of an array
	var tmp_arr = new Array(), strict = !!strict, include = true, cnt = 0;

	for ( key in input ){
		include = true;
		if ( search_value != undefined ) {
			if( strict && input[key] !== search_value ){
				include = false;
			} else if( input[key] != search_value ){
				include = false;
			}
		}

		if( include ) {
			tmp_arr[cnt] = key;
			cnt++;
		}
	}

	return tmp_arr;
}

/*** функция str_replace ***/
function str_replace ( search, replace, subject ) {
	if(!(replace instanceof Array)){
		replace=new Array(replace);
		if(search instanceof Array){
			while(search.length>replace.length){
				replace[replace.length]=replace[0];
			}
		}
	}
	if(!(search instanceof Array))search=new Array(search);
	while(search.length>replace.length){
		replace[replace.length]='';
	}
	if(subject instanceof Array){
		for(k in subject){
			subject[k]=str_replace(search,replace,subject[k]);
		}
		return subject;
	}
	for(var k=0; k<search.length; k++){
		var i = subject.indexOf(search[k]);
		while(i>-1){
			subject = subject.replace(search[k], replace[k]);
			i = subject.indexOf(search[k],i);
		}
	}
	return subject;
}

/********************* Генерилка паролей ****************************/
// getPass( input.id1, input.id2, [, pass_len] )
function getPass( obj_1, obj_2){
   var i, interval, words = "";
   words += "qwertyuiopasdfghjklzxcvbnm";
   words += "QWERTYUIOPASDFGHJKLZXCVBNM";
   words += "1234567890";
   //words += "~!@#$%^&*()_+-=.,;:";
   ( obj1 = document.getElementById(obj_1)).value = "";
   
   if(obj_2)
   	( obj2 = document.getElementById(obj_2)).value = "";
   	
   var new_word_timeout = 100; // время между появлением новых букв.
   var word_timeout = 10; // время между сменой букв
   var word_count = 10; // количество букв
   if( arguments.length > 1 ){
       word_count = parseInt( arguments[2], 10 );
   }
   new function(){
       this.getNextWord = function(){
           var val_t = getRand(1,9);
		   obj1.value += val_t;
		   if(obj_2)
		   		obj2.value += val_t;
       }
       this.getWord = function(){
           var val_passw = obj1.value.substring( 0, obj1.value.length - 1) + words.charAt(getRand(0,words.length -1));
           
         obj1.value = val_passw;
         if(obj_2)
		 	obj2.value = val_passw;  
       }
       this.stop = function(){
           clearInterval( interval );
       }
       for( i = 0; i < word_count; i ++ ){
           setTimeout( this.getNextWord, i * new_word_timeout );
       }
       interval = setInterval( this.getWord, word_timeout );
       setTimeout( this.stop, new_word_timeout * word_count );
   }
}
function getRand( min, max ){
   return Math.round( Math.random( ) * ( max - min ) ) + min;
}
/*****************************************************************/

function get_correct_str(num, str1, str2, str3) {
    val = num % 100;
    if (val > 10 && val < 20) return str3;
    else {
        val = num % 10;
        if (val == 1) return str1;
        else if (val > 1 && val < 5) return str2;
        else return str3;
    }
}

function number_format( number, decimals, dec_point, thousands_sep ) {
	var i, j, kw, kd, km;
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
	return km + kw + kd;
}

