function backPage(url){
    if (!url) var url = document.location;
    location.href="" + url + "";
}
function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    
    if (cookie.length > 0) {
	offset = cookie.indexOf(search);
	if (offset != -1) {
	    offset += search.length;
	    end = cookie.indexOf(";", offset)
	    if (end == -1) {
		end = cookie.length;
	    }
	    setStr = unescape(cookie.substring(offset, end));
	}
    }
    return(setStr);
}					    

function eraseCookie(remove, order) {
    var name = "FMshop";
    var tmp_val = " ";
    var url = document.location;
    if (remove){
		
        var days = 10;
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
        var str = getCookie(name);
        tmp_val = str.replace(remove + '|', '');
        tmp_val = tmp_val.replace('|' + remove, '');
        tmp_val = tmp_val.replace(remove, '');
        tmp_val = tmp_val.replace('undefined','');
        tmp_val = tmp_val.replace('||','|');
        alert('Артикул ' + remove + ' удалён ');
						
    } else { var expires = ""; }
	if (order) alert('Ваш заказ отправлен менеджеру');									    
        document.cookie = name+"="+tmp_val+expires+"; path=/";
        if (url) location.href="" + url + "";
}
													    								

function createCookie(name, value, days) {
    var cLenght = document.cookie.length;
    var tmp_val = value;
    if (cLenght > 0){
	var oldStr = getCookie(name);
	if (oldStr != null) tmp_val = '' + value + '|' + oldStr + '';
    }
    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+"="+tmp_val+expires+"; path=/";
    alert('Артикул ' + value + ' добавлен в корзину');
}

function addProd(value){
    var name = 'FMshop';
    createCookie(name, value, 10);
    getCookie(name);
    
}

function summ_order(out_field){
    var cnt_summ = 0;
    var tmp_cnt_summ = 0;
    var in_field = document.forms.FMorder.elements.cost;
    var in_field_cnt = in_field.length;

    if (!in_field_cnt) in_field_cnt = 0;

    if (in_field_cnt > 0) {
	for(i = 0; i < in_field_cnt; i++){
	    tmp_cnt_summ = parseFloat(in_field[i].value);
	    if (!tmp_cnt_summ) tmp_cnt_summ = 0;
	    cnt_summ = cnt_summ + tmp_cnt_summ;
	}
	out_field.value = cnt_summ;
    } else { 
	out_field.value = in_field.value;
    }
}


function price(out_field, in_field, in_row, in_left,  h_name){
//    in_field.onClick = true;
    var tmp_summ = 0;
    var cnt, price, row_value, in_field_cnt;
    for (i = 0; i < in_field.length; i++) {
	cnt = parseFloat(in_field[i].value);
	if (!cnt) cnt = 0;
	price = parseFloat(in_field[i].id);
	if (!price) price = 0;
	tmp_summ = tmp_summ + ( cnt * price );
    }
    if (!i) {
        cnt = parseFloat(in_field.value);
        if (!cnt) cnt = 0;
        price = parseFloat(in_field.id);
        if (!price) price = 0;
        tmp_summ = ( cnt * price );
    }
    
    if (!tmp_summ || isNaN(tmp_summ)) tmp_summ = 0;
    in_field_cnt =  document.forms.FMorder.elements.cost.length;
    if (!in_field_cnt) in_field_cnt = 0;

    if (in_field_cnt > 0) {
	if (in_left < 0) {
	    out_field[in_row].value = tmp_summ;
	} else {
	    out_field.value = tmp_summ;
	}
    } else {
	document.forms.FMorder.elements.cost.value = tmp_summ;
	out_field = document.forms.FMorder.elements.cost;
	out_field.value = tmp_summ;
    }

    summ_order(document.forms.FMorder.elements.summ);

    if (in_left >= 0){
	row_value = parseFloat(in_field[in_left].value);
    } else {
	row_value = parseFloat(in_field.value);
    }
    if (!row_value) row_value = 0;
    
    
    document.forms.FMorder.elements["fm_tmp[" + in_row + "][" + h_name   + "]"].value = row_value;
}

function isValidEmail(email) {
    re = /^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
    return re.test(email);
}
				
function d_smail() {
    var address = document.upd_order.order_mail.value;

    if (document.upd_order.order_fio.value == '') {
	document.upd_order.order_fio.focus();
        alert ('Пожалуйста, укажите Ваши Ф.И.О.');
	return false;
    } else if (document.upd_order.order_tel.value == '' && document.upd_order.order_mail.value == '') {
        document.upd_order.order_tel.focus();
	alert ('Одно из полей Телефон или e-mail должно быть заполнено');
	return false;
    } else if (address != '') {
        if (!isValidEmail(address)) {
    	    alert('Введите корректный e-mail');
    	    return false;
	}
    } else {
        document.upd_order.submit ();
	return true;
    }
}
																																			            
																																			    
