/******************************************************************************************/
var shopbag = 0;

var arrexclude = new Array();

function openimg(src,width,height){
    width = width + 20;
	height = height + 20;
	window.open(src,"","width="+width+", height="+height+", toolbar=no, scrollbars=no");
}

function valid_feedback_form()
{
	var testname = document.forms['feedback'].elements['properties[usname]'].value;
	var notvalid = 0;
	var alertmsg = "";
	
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Ваше имя'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['feedback'].elements['properties[us_email]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Email'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['feedback'].elements['properties[us_title]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Tema сообщения'!\n\r";
		notvalid = 1;
	}	

	testname = document.forms['feedback'].elements['properties[msg]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Сообщение'!\n\r";
		notvalid = 1;
	}
	
	if(notvalid==1){
	    alertmsg = alertmsg + "\n\rЗаполните, пожалуйста, все поля!"
	    window.alert(alertmsg);
		return 0;
	}
	
	document.forms['feedback'].submit();	

}

function valid_viporder_form()
{
	var testname = document.forms['vip_order'].elements['properties[usname]'].value;
	var notvalid = 0;
	var alertmsg = "";
	
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Ваше имя'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['vip_order'].elements['properties[us_email]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Email'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['vip_order'].elements['properties[us_title]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Tema сообщения'!\n\r";
		notvalid = 1;
	}	

	testname = document.forms['vip_order'].elements['properties[msg]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Сообщение'!\n\r";
		notvalid = 1;
	}

	if(notvalid==1){
	    alertmsg = alertmsg + "\n\rЗаполните, пожалуйста, все поля!"
	    window.alert(alertmsg);
		return 0;
	}
	
	document.forms['vip_order'].submit();	

}

//order_form
function valid_order_form()
{
	var testname = document.forms['order_form'].elements['properties[usname]'].value;
	var notvalid = 0;
	var alertmsg = "";

	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Ваше имя'!\n\r";
		notvalid = 1;
	}	

	testname = document.forms['order_form'].elements['properties[usaddress]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Адрес'!\n\r";
		notvalid = 1;
	}	

	testname = document.forms['order_form'].elements['properties[usphone]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Телефон'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['order_form'].elements['properties[us_email]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'E-mail'!\n\r";
		notvalid = 1;
	}	
	
	testname = document.forms['order_form'].elements['properties[msg]'].value;
	if(testname.length==0){
		alertmsg = alertmsg + "Не заполнено поле 'Комментарии'!\n\r";
		notvalid = 1;
	}

	if(notvalid==1){
	    alertmsg = alertmsg + "\n\rЗаполните, пожалуйста, все поля!"
	    window.alert(alertmsg);
		return 0;
	}
	
	document.forms['order_form'].submit();	

}

/*******************************************************************************************/
function eval_(object){
if(document.getElementsByName)
	return document.getElementsByName(object); 
if(document.getElementById)
	return eval('document.getElementById('+object+')'); 
	
	return false;
}

function twoPlaces(amount){
	return(amount == Math.floor(amount)) ? amount + '.00':
		((amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
function round(number,X){
	X =(!X ? 2 :X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
function totals(num){
	return twoPlaces(Math.floor((num - 0) * 100) / 100);
}

/*******************************************************************************************************/
function deleteTovar(no){
	a=getCookie("TOVARS");
	arr=splitString(a, '```');
	tovars = new Array();
	j=0;
	for (var i=0; i < arr.length; i++) {
		arr1 = splitString(arr[i], '!!');
		if(i!=no){			
			tovars[j] = arr1.join('!!');		
			j++;
		}
	}
	res = tovars.join("```");
	deleteCookie("TOVARS");
	document.cookie="TOVARS="+res;		
}
/*******************************************************************************************************/
function ifelinarr(Arr,element)
{
	if(Arr==null || element==null)
		return false;
	for (var i=0; i < Arr.length; i++){
	     if(Arr[i] == element)
		    return true;
	}		
	return false;	
  
}
/*******************************************************************************************************/
function redraw_small_korzina_text(cookieName,arrofel){
	a=getCookie(cookieName);
	arr=splitString(a, '```');// !!- разделитель товаров в cookie
	countt = 0;
	tot_summ = 0;
	el = eval_('korzina_status');

	if(arr.length>0){
		for (var i=0; i < arr.length; i++) {
			arr1 = splitString(arr[i], '!!');
			if(!ifelinarr(arrofel,arr1[0])){
			  countt+=round(arr1[1],0);
			  tot_summ+=round(arr1[1],0) * round(arr1[2],2);
			}  
		}
		// отрисовываем значения		
		el[0].innerHTML = ""+countt+" товар(ов) на сумму "+tot_summ+" руб.";	
	}else{
		el[0].innerHTML = "Ваша корзина пуста";
	}
	if(a==""){
		el[0].innerHTML = "Ваша корзина пуста";			
	}
}
/*******************************************************************************************************/
function redraw_big_korzina_text(cookieName,arrofel){
	a=getCookie(cookieName);
	arr=splitString(a, '```');// !!- разделитель товаров в cookie
	countt = 0;
	tot_summ = 0;
	el = eval_('full_shopbag_count');
	el1 = eval_('full_shopbag_price');
	if(arr.length>0){
		for (var i=0; i < arr.length; i++) {
			arr1 = splitString(arr[i], '!!');
			if(!ifelinarr(arrofel,arr1[0])){
			  countt+=round(arr1[1],0);
			  tot_summ+=round(arr1[1],0) * round(arr1[2],2);
			}
		}
		// отрисовываем значения		
		el[0].innerHTML = countt;
		el1[0].innerHTML = tot_summ;	
	}else{
		el[0].innerHTML = "0";
		el1[0].innerHTML = "0";	
	}
	if(a==""){
		el[0].innerHTML = "0";
		el1[0].innerHTML = "0";	
	}
}
/*******************************************************************************************************/
function parseObj(obj) {
	objCount = 0;
	var objStr = '';
		for (prop in obj) {
			objStr += '<TR><TD>Property: </TD><TD><B>' + prop + '</B></TD><TD>Type: </TD><TD><B>' + typeof(obj[prop]) + 
				'</B></TD><TD>Value: </TD><TD><B>'  + obj[prop] + '</B></TD></TR>';
			if (typeof(obj[prop]) == "object") {
				objStr += parseObj(obj[prop]);
				}
			}
	return objStr;
	}
/*******************************************************************************************************/	
function remove_prop(btn){
		tr = btn;
		while (tr.tagName != 'TR') tr = tr.parentNode;
		tr.parentNode.removeChild(tr);		
	}
/*******************************************************************************************************/	
function do_order(){
	document.forms['korzina_integrated'].submit();
}
/*******************************************************************************************************/	
function do_recalc_shopbag(){
	a=getCookie("TOVARS");
	//alert(a);
	arr=splitString(a, '```');// !!- разделитель товаров в cookie
	couItems = 0;
	ids = new Array;
	tovars = new Array;
	if(arr.length>0){
		couItems = 0;
		for (var i=0; i < arr.length; i++) {
			arr1 = splitString(arr[i], '!!');
			elName = "to_delete_" + arr1[0];
			if(document.forms['korzina_integrated'].elements[elName].checked==false){
				
				ids[couItems] = round(arr1[0],0);	
				tovars[couItems] = new Array;
				tovars[couItems][0] = ids[couItems];
				elName = "cou_" + ids[couItems];
			    
				count = document.forms['korzina_integrated'].elements[elName].value;	
				tovars[couItems][1] = count;
				tovars[couItems][2] = round(arr1[2],0);	
				el = eval_('c4row_'+ids[couItems]);	
				el[0].innerHTML = round(count*tovars[couItems][2],2);
				couItems++;
			}else{
				  	for(var j=1; j<=6; j++){
						el = eval_('c'+j+'row_'+arr1[0]);	
						el[0].innerHTML = "";								
				  }
			}
			
		}	
		reorder(tovars);
		a=getCookie("TOVARS");
		if(a!=""){
			redraw_big_korzina_text("TOVARS");	
			redraw_small_korzina_text("TOVARS");
		}else{
			window.location.href = "korzinka.html";
		}
	}	
	
}
/*******************************************************************************************************/
function put2order(b){
	founded = checkTovar(b,"TOVARS");
	if(founded==0){	
		SetCookie(b,"TOVARS");	
	}else{		
		alert ('Товар уже в корзине!');	
	}
	redraw_small_korzina_text("TOVARS");	
}
/*******************************************************************************************************/
function reorder(tovars){
	t = new Array;
	for (var i=0; i < tovars.length; i++) {
		t[i] = tovars[i].join('!!');
	}
	res = t.join("```");
	deleteCookie("TOVARS");
	//alert(res);
	document.cookie="TOVARS="+res;			
}
/*******************************************************************************************************/
function deleteCookie(name){
var D = new Date();
D.setDate(0);
value=getCookie(name);
document.cookie=name+"="+value+"; expires="+D.toGMTString()
};
/*******************************************************************************************************/
function getCookie(name){
var s=name+"=";
var i=0;
if (document.cookie.substring(0, s.length)!=s){s="; "+s; i=document.cookie.indexOf(s)};
if (i>-1){
	i+=s.length;
	var endstr = document.cookie.indexOf(";", i);
	if (endstr == -1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(i, endstr))}
else{return ""}
};
/*******************************************************************************************************/
function splitString (stringToSplit,separator) 	{
	arrayOfStrings = stringToSplit.split(separator)
	return arrayOfStrings;
}	
/*******************************************************************************************************/
function SetCookie(b,cookieName){
	a=getCookie(cookieName);	
	toadd = b.split('!!');
	idd = toadd[0];		
		
			
	if(a!=''){
		a+='```'+b;
	}else{
		a+=b;
	}
	document.cookie=cookieName+"="+a;	
	alert('Товар успешно добавлен в корзину!');			
}
/*******************************************************************************************************/
function checkTovar(b,cookieName){
	a=getCookie(cookieName);
	toadd = b.split('!!');// !!- разделитель полей товара в cookie
	id_add = toadd[0];
	arr=splitString(a, '```');// !!- разделитель товаров в cookie
	founded = 0;
	for (var i=0; i < arr.length; i++) {
		arr1 = splitString(arr[i], '!!');
		if(arr1[0] == id_add){
			founded=1;
		}
	}
	return founded;	
}
/*******************************************************************************************************/
function orderpage(){

	var a=getCookie("TOVARS");
	var arr=splitString(a, '```');// !!- разделитель товаров в cookie
	var countt = 0;
	var tot_summ = 0;
	var el = eval_('full_shopbag_count');
	var el1 = eval_('full_shopbag_price');
	if(arr.length>0){
		for (var i=0; i < arr.length; i++) {
			arr1 = splitString(arr[i], '!!');
			if(!ifelinarr(arrexclude,arr1[0])){
			  countt+=round(arr1[1],0);
			  tot_summ+=round(arr1[1],0) * round(arr1[2],2);
			}
		}
	}
	
    if(countt>0)
	{
		arr=splitString(location.href,'/');		
		if(arr[arr.length-1]=="korzinka.html"){		 
		  var t = arrexclude.length;
		  for(var i=0;i<t;i++)
		   arrexclude.pop();		  
		  do_recalc_shopbag();
	    }
		location.href="order.html";
	}
	else
	    window.alert('Корзина пустая - заказ невозможен!');	 
}
/*********************************************************************************************************/
function redoarray(id){
	
	var newarr = new Array();
	for(var i=0; i<arrexclude.length; i++){
		if(arrexclude[i]!=id)
		  newarr.push(arrexclude[i]);
	}	  
	arrexclude = newarr;
}
/*********************************************************************************************************/
function do_strike_tr(tr,checkbox){
	var tr1 = document.getElementById(tr);
	if(checkbox.checked==true)
	{
		eval('rrow_'+tr + ".style.backgroundColor = \"#E4AFBA\"");		
		arrexclude.push(tr);
		redraw_big_korzina_text("TOVARS",arrexclude);	
		redraw_small_korzina_text("TOVARS",arrexclude);
	}	
	else
	{	
		eval('rrow_'+tr + ".style.backgroundColor = \"White\"");
		redoarray(tr);
		do_recalc_shopbag_editable(tr);
		redraw_big_korzina_text("TOVARS",arrexclude);	
		redraw_small_korzina_text("TOVARS",arrexclude);
	}	
	
}
/*******************************************************************************************************/
function do_recalc_shopbag_editable(id){
    
	if(ifelinarr(arrexclude,id))
		return 0;
	
	a=getCookie("TOVARS");

	arr=splitString(a, '```');// !!- разделитель товаров в cookie
	couItems = 0;
	ids = new Array;
	tovars = new Array;
	if(arr.length>0){
		couItems = 0;
		for (var i=0; i < arr.length; i++) {
				arr1 = splitString(arr[i], '!!');
				elName = "to_delete_" + arr1[0];
			
				ids[couItems] = round(arr1[0],0);	
				tovars[couItems] = new Array;
				tovars[couItems][0] = ids[couItems];
				elName = "cou_" + ids[couItems];
			    
				count = document.forms['korzina_integrated'].elements[elName].value;	
				tovars[couItems][1] = count;
				tovars[couItems][2] = round(arr1[2],0);	
				el = eval_('c4row_'+ids[couItems]);	
				el[0].innerHTML = round(count*tovars[couItems][2],2);
				couItems++;
		}			
		reorder(tovars);
		redraw_big_korzina_text("TOVARS",arrexclude);	
		redraw_small_korzina_text("TOVARS",arrexclude);
		
	}	
}

function selectrow(id)
{
	if(ifelinarr(arrexclude,id))
	{		
		window.alert('Вы не можете изменить количество удаляемого товара!');
		document.forms['korzina_integrated'].elements['to_delete_'+id].focus();
	}	
}
