var FieldsList = new Array();
var xMousePos;
var yMousePos;
//Array('ID поля', 'Правило', 'Сообщение', 'Тип сообщения');
FieldsList['name'] = new Array('name', '^[А-Яа-яІіЇїЄє\u0020-]{3,100}$', 'Заполните поле <b>"Имя и фамилия"</b><br> Например: Василий Иванов', '0');
FieldsList['date'] = new Array('date', '^[0-3]{1}[0-9]{1}[.]{1}[0-1]{1}[0-9]{1}[.]{1}[1-2]{1}[0-9]{3}[\u0020]+[0-9]{2}:[0-9]{2}$', 'Заполните поле <b>"Дата и время доставки"</b><br> в формате dd.mm.yyyy hh:mm<br>Например: 12.12.2009 12:33', '0'); //[0-9:.\u0020-]{3,50}
FieldsList['email'] = new Array('email', '^[0-9a-z-_.]+@[0-9a-z-_.]+[.]{1}[a-z]{2,4}$', 'Заполните поле <b>"Контактный e-mail"</b><br> Например: vasily@gmail.com', '0');
FieldsList['adress'] = new Array('adress', '^[0-9A-Za-zА-Яа-яІіЇї,Єє\u0020/.-]{10,200}$', 'Заполните поле <b>"Адрес доставки"</b>', '1');
FieldsList['phone'] = new Array('phone', '^[0-9)(\u0020-]{7,30}$', 'Заполните поле <b>"Контактный телефон"</b><br> Например: 044 235-35-46', '0');
FieldsList['comment'] = new Array('comment', '^[0-9А-Яа-яІіЇїЄє\u0020-,.]{10,500}$|^$', 'Заполните поле <b>"Комментарии"</b>', '1');


//java-скрипт, открывающий новое окно со спсиком товаров в корзине
function open_window(link,w,h) {
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
}
function confirmDelete(url, msg) { //подтверждение удаления пользователя
	temp = window.confirm(msg);
	if (temp) { //удалить
		window.location=url;
	};
}
function validate() { // новости
	if (document.form1.email.value.length<1) {
		alert("Пожалуйста введите Ваш e-mail");
		return false;
	};
	if (document.form1.email.value == 'Email') {
		alert("Пожалуйста введите Ваш e-mail");
		return false;
	};
	return true;
}
function validate_disc() { // форма ввода сообщения
	if (document.formD.nick.value.length<1) {
		alert("Пожалуйста введите имя или псевдоним");
		return false;
	};

	if (document.formD.topic.value.length<1) {
		alert("Пожалуйста введите тему сообщения");
		return false;
	};

	return true;
}
function validate_search() {

	if (document.Sform.price1.value!="" && ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value))) {
		alert("В качестве цены1 должно быть положительное число");
		return false;
	};
	if (document.Sform.price2.value!="" && ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value))) {
		alert("В качестве цены2 должно быть положительное число");
		return false;
	};

	return true;
}
function ShowPhoto(URL, W, H){
	ScreenW = window.screen.width;
	ScreenH = window.screen.height;
	Left = Number((ScreenW - W) / 2);
	Top  = Number((ScreenH - H) / 2);
	if(W > ScreenW || H > ScreenH){
		Wnd = open('', '_blank', 'width='+ W +', height='+ H +', left=0, top=0, fullscreen=no, '+
		'location=no, menubar=no, toolbar=no, resizable=yes, scrollbars=yes, status=no');
	}
	else{
		Wnd = open('', '_blank', 'width='+ W +', height='+ H +', left='+ Left +', top='+ Top +', fullscreen=no, '+
		'location=no, menubar=no, toolbar=no, resizable=no, scrollbars=no, status=no');
	}
	Wnd.document.write('<HTML><HEAD><TITLE>Предварительный просмотр</TITLE>');
	Wnd.document.write('<STYLE>BODY {padding: 0px; margin: 0px;}</STYLE></HEAD>');
	Wnd.document.write('<BODY><IMG src="'+ URL +'" ondrag=\"return false;\" oncontextmenu=\"return false;\"></BODY></HTML>');
}

function NextPage(path, id){
	document.SearchPage.action = path + id + '/';
    document.SearchPage.submit();
}

function getSearchPath(value){
	document.searchForm.action = value;
}

function ShowShippingMethod(){
	var ShipComment = document.getElementById('shipping_comment') ? document.getElementById('shipping_comment') : document.all.shipping_comment;
	var srcComment = document.getElementById('src_shipping_comment') ? document.getElementById('src_shipping_comment') : document.all.src_shipping_comment;
    var divsArr = srcComment.getElementsByTagName('div');
    ShipComment.innerHTML = divsArr[document.form1.shipping.selectedIndex].innerHTML;
    var price = divsArr[document.form1.shipping.selectedIndex].getAttribute('price');
    var rules = divsArr[document.form1.shipping.selectedIndex].getAttribute('rule');
    CalculatePrice(price, rules);
    ShowPaymentMethod();
    ShowPaymentComment();
}

function clearOptions(obj) {
	for (var a = 0; a < obj.options.length; a++) {
		obj.options[a] = null;
		a = -1;
	}
}

function ShowPaymentMethod(){
	clearOptions(document.form1.payment);
	var srcPayment = document.getElementById('src_payment_comment') ? document.getElementById('src_payment_comment') : document.all.src_payment_comment;
    var divsArr = srcPayment.getElementsByTagName('div');
    var selectShip = document.form1.shipping[document.form1.shipping.selectedIndex].value;
    for (i = 0; i < divsArr.length; i++) {
    	var name = divsArr[i].getAttribute('pay_name');
    	var ship = divsArr[i].getAttribute('ship_id');
    	var comment = divsArr[i].innerHTML;
    	var id = divsArr[i].getAttribute('id');
    	if(ship != selectShip){continue;}
        document.form1.payment.options[document.form1.payment.options.length] = new Option(name, id);
    }
    ShowPaymentComment();
}

function ShowPaymentComment(){
	var PayComment = document.getElementById('payment_comment') ? document.getElementById('payment_comment') : document.all.payment_comment;
	var srcPayment = document.getElementById('src_payment_comment') ? document.getElementById('src_payment_comment') : document.all.src_payment_comment;
    var divsArr = srcPayment.getElementsByTagName('div');
    var selectPay = document.form1.payment[document.form1.payment.selectedIndex].value;
    for (i = 0; i < divsArr.length; i++) {
    	var comment = divsArr[i].innerHTML;
    	var id = divsArr[i].getAttribute('id');
    	if(id == selectPay){
        	PayComment.innerHTML = comment;
        	break;
    	}
    }
}

function CalculatePrice(price, rules){
	var AllPrice = document.getElementById('all_price') ? document.getElementById('all_price') : document.all.all_price;
	var ShipPrice = document.getElementById('ship_price') ? document.getElementById('ship_price') : document.all.ship_price;
	var ResultPrice = document.getElementById('result_price') ? document.getElementById('result_price') : document.all.result_price;

	ShipPrice.innerHTML = (parseFloat(AllPrice.innerHTML) < parseFloat(rules)) ? price : '0.00';
	var Summ = parseFloat(AllPrice.innerHTML) + parseFloat(price);
    var result = (parseFloat(AllPrice.innerHTML) < parseFloat(rules)) ? Summ : parseFloat(AllPrice.innerHTML);
    ResultPrice.innerHTML = result;
}

function WinError(label, xy, myObj){
    var width = 255;
    var height = 85;

    var Wnd = document.getElementById("popupWnd");
    var cnt = '';
    if(!Wnd){

        Wnd = document.createElement("DIV");
        Wnd.id = "popupWnd";
        Wnd.style.border = "0";
        Wnd.style.position = "absolute";
        Wnd.style.overflow = "hidden";
        Wnd.style.zIndex = "20";
        Wnd.style.display = "none";
        document.body.appendChild(Wnd);
    }
    var ps = (myObj.tagName == 'INPUT') ? 15 : 15 * myObj.rows;
    Wnd.style.left = xy.x;
    Wnd.style.top = xy.y + ps;
    Wnd.style.width = width;
    Wnd.style.height = height;
    //Wnd.style.background = "#F9F4D8";
    Wnd.style.padding = "8px";
    Wnd.style.display = "block";

    cnt += GetMessage(label);

    Wnd.innerHTML = cnt;
}

function CloseWinError(){
	var ErrObj = document.getElementById('popupWnd') ? document.getElementById('popupWnd') : document.all.popupWnd;
	if(ErrObj){
		ErrObj.style.display = "none";
	}
}

function getAbsolutePos(el){
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent)
		{
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
		}
	return r;
}

function checkFields(obj){

	document.form1.RegisterSubmit.disabled = true;

	for(var key in FieldsList){
        var myObj = document.getElementById(FieldsList[key][0]) ? document.getElementById(FieldsList[key][0]) : document.all[FieldsList[key][0]];
        if(myObj.disabled == true){continue;}
        var Value = myObj.tagName == 'INPUT' ? myObj.value : myObj.value;
        var Rule = new RegExp(FieldsList[key][1]);
        if(!Rule.test(document.all[FieldsList[key][0]].value)){
        	var pos = getAbsolutePos(myObj);
            myObj.style.background = "#F6DFCF";
            myObj.focus();
            WinError(FieldsList[key][2], pos, myObj);
            document.form1.RegisterSubmit.disabled = true;
			return false;
        }
        else{
            myObj.style.background = "#FFFFFF";
            CloseWinError();
			document.form1.RegisterSubmit.disabled = false;
		}
	}
}

function resetFields(){
	for(var key in FieldsList){
        var myObj = document.getElementById(FieldsList[key][0]) ? document.getElementById(FieldsList[key][0]) : document.all[FieldsList[key][0]];
        if(myObj.disabled == true){continue;}
        var Value = myObj.tagName == 'INPUT' ? myObj.value : myObj.value;
        myObj.style.background = "#FFFFFF";
        CloseWinError();
		document.form1.RegisterSubmit.disabled = false;
	}
}

function CheckTextField(obj){

	var myObj = document.getElementById(obj) ? document.getElementById(obj) : document.all[obj];
	if(myObj.disabled == true){return false;}
	var key = myObj.name;
	var Value = myObj.tagName == 'INPUT' ? myObj.value : myObj.value;
	var Rule = new RegExp(FieldsList[key][1]);
 	if(!Rule.test(Value)){
  		var pos = getAbsolutePos(myObj);
    	myObj.style.background = "#F6DFCF";
    	myObj.focus();
    	WinError(FieldsList[key][2], pos, myObj);
    	//document.form1.RegisterSubmit.disabled = true;
     }
     else{
     	myObj.style.background = "#FFFFFF";
     	CloseWinError();
     	//document.form1.RegisterSubmit.disabled = false;
	 }

}

function GetMessage(Label){
	var cnt = '';
	cnt += '<table width="250" border="0" cellspacing="0" cellpadding="0" height="80">';
  	cnt += '<tr>';
    cnt += '<td align="right" valign="bottom" width="8" height="34"><img src="/images/error/error_01.gif" width="8" height="34"></td>';
    cnt += '<td valign="bottom" background="/images/error/error_top_03.gif"><img src="/images/error/error_02.gif" width="30" height="34"></td>';
    cnt += '<td align="left" valign="bottom" width="13" height="34"><img src="/images/error/error_04.gif" width="13" height="34"></td>';
  	cnt += '</tr>';
  	cnt += '<tr>';
    cnt += '<td width="8" background="/images/error/error_left_05.gif">&nbsp;</td>';
    cnt += '<td background="/images/error/error_center_06.gif" valign="top">';
    cnt += '<div title="Закрыть" style=" position:absolute; top:35px; left:240px; width:10px; text-align:right; font: bold 11px Arial; color:#FF0000; cursor:pointer;" onclick="resetFields();">x</div>';
    cnt += '<div class="error_text">' + Label + '</div>';
    cnt += '</td>';
    cnt += '<td width="13" background="/images/error/error_right_07.gif">&nbsp;</td>';
  	cnt += '</tr>';
  	cnt += '<tr>';
    cnt += '<td align="right" valign="top" width="8" height="14"><img src="/images/error/error_08.gif" width="8" height="14"></td>';
    cnt += '<td valign="top" background="/images/error/error_bottom_09.gif" height="14"><img src="/images/error/error_bottom_09.gif" width="1" height="14"></td>';
    cnt += '<td valign="top" width="13" height="14"><img src="/images/error/error_10.gif" width="13" height="14"></td>';
  	cnt += '</tr>';
	cnt += '</table>';
    return cnt;
}

function GetColor(id, prod){
	var MyPicture = document.getElementById('product_image_'+prod) ? document.getElementById('product_image_'+prod) : document.all['product_image_'+prod];
	var MyLink = document.getElementById('product_link_'+prod) ? document.getElementById('product_link_'+prod) : document.all['product_link_'+prod];
	var MyAdd = document.getElementById('product_add_'+prod) ? document.getElementById('product_add_'+prod) : document.all['product_add_'+prod];
    //var MyPictureLarge = document.getElementById('product_large_image_'+prod) ? document.getElementById('product_large_image_'+prod) : document.all['product_large_image_'+prod];
    var MyPictureLargeLink = document.getElementById('product_large_image_link_'+prod) ? document.getElementById('product_large_image_link_'+prod) : document.all['product_large_image_link_'+prod];

	var PrintLink = document.getElementById('product_print_'+prod) ? document.getElementById('product_print_'+prod) : document.all['product_print_'+prod];


	if(ProductColorList[id]){
		MyPicture.src = '/uploads/'+ProductColorList[id][1];
		//MyPictureLarge.src = '/uploads/large/'+ProductColorList[id][1];
		MyPictureLargeLink.href = '/uploads/large/'+ProductColorList[id][1];

		// product delail link
		MyLinkList = MyLink.href.split('/');
		newLink = '';
		for(i = 0; i < MyLinkList.length - 1 ; i++){
			//if(MyLinkList[i].length == 0){continue;}
			val = i == 5 ? id : MyLinkList[i];
			newLink += val + '/';
		}
		MyLink.href = newLink;

		// print link
		PrintLinkList = PrintLink.href.split('/');
		newPrintLink = '';
		for(i = 0; i < PrintLinkList.length - 1 ; i++){
			val = i == 5 ? id : PrintLinkList[i];
			newPrintLink += val + '/';
		}
		PrintLink.href = newPrintLink;

        // Add to cart link
		MyAddList = MyAdd.href.split('/');
		newAdd = '';
		pos = GetPositionForPath(MyAddList, 'add') + 2;
		for(i = 0; i < MyAddList.length - 1 ; i++){
			//if(MyAddList[i].length == 0){continue;}
			val = i == pos ? id : MyAddList[i];
			newAdd += val + '/';
		}
		MyAdd.href = newAdd;
	}
}

function GetSize(id, prod){
	var MyLink = document.getElementById('product_link_'+prod) ? document.getElementById('product_link_'+prod) : document.all['product_link_'+prod];
	var MyAdd = document.getElementById('product_add_'+prod) ? document.getElementById('product_add_'+prod) : document.all['product_add_'+prod];
	var MyPrice = document.getElementById('product_price_'+prod) ? document.getElementById('product_price_'+prod) : document.all['product_price_'+prod];
    var PrintLink = document.getElementById('product_print_'+prod) ? document.getElementById('product_print_'+prod) : document.all['product_print_'+prod];

    if(ProductSizeList[id]){
    	MyPrice.innerHTML = ProductSizeList[id][1];
        MyLinkList = MyLink.href.split('/');
		newLink = '';
		for(i = 0; i < MyLinkList.length - 1 ; i++){
			//if(MyLinkList[i].length == 0){continue;}
			val = i == 6 ? id : MyLinkList[i];
			newLink += val + '/';
		}
		MyLink.href = newLink;

		// print link
		PrintLinkList = PrintLink.href.split('/');
		newPrintLink = '';
		for(i = 0; i < PrintLinkList.length - 1 ; i++){
			val = i == 6 ? id : PrintLinkList[i];
			newPrintLink += val + '/';
		}
		PrintLink.href = newPrintLink;

		MyAddList = MyAdd.href.split('/');
		pos = GetPositionForPath(MyAddList, 'add') + 3;
		newAdd = '';
		for(i = 0; i < MyAddList.length - 1 ; i++){
			//if(MyAddList[i].length == 0){continue;}
			val = i == pos ? id : MyAddList[i];
			newAdd += val + '/';
		}
		MyAdd.href = newAdd;

    }
}

function GetPositionForPath(List, item){
	for(i = 0; i < List.length; i++){
		if(List[i] == item){return i;}
	}
	return -1;
}

function LargeImage(action, id){
	var cont = document.getElementById(id) ? document.getElementById(id) : document.all[id];
	if(action == 'show'){
    	if(cont.innerHTML == 'NONE'){return;}
        var picID = id.split('_');
		var contPicture = document.getElementById('product_large_image_'+picID[1]) ? document.getElementById('product_large_image_'+picID[1]) : document.all['product_large_image_'+picID[1]];

    	var picWidth = contPicture.getAttribute('w');
    	var picHeight = contPicture.getAttribute('h');
    	var width = picWidth;
    	var height = Number(picHeight) + 30;
    	ScreenW = window.screen.width;
		ScreenH = window.screen.height;
		var Top = Number((ScreenH - height) / 2) + (document.body.scrollTop / 2);
		var Left = Number((ScreenW - width) / 2);

    	var WndImg = document.getElementById("WndImage");
    	if(!WndImg){
        	WndImg = document.createElement("DIV");
        	WndImg.id = "WndImage";
        	WndImg.style.border = "0";
        	WndImg.style.position = "absolute";
        	//WndImg.style.overflow = "hidden";
        	WndImg.style.zIndex = "20";
        	WndImg.style.display = "none";
        	document.body.appendChild(WndImg);
    	}

    	WndImg.style.left = Left;
    	WndImg.style.top = Top;
    	WndImg.style.background = '#FFFFFF';
    	WndImg.style.width = width;
    	WndImg.style.height = height;
    	WndImg.style.padding = 10;
    	WndImg.style.display = "block";
    	var cnt = '';
    	cnt += '<div align="right" style="widht:100%; text-align:right; padding:4px;"><a href="#" onclick="LargeImage(\'hide\', 0); return false;">[X] закрыть</a></div>';
    	cnt += cont.innerHTML
    	WndImg.innerHTML = cnt;
    	//WndImg.innerHTML .= cont.innerHTML;
	}
	else{
    	var PreviewObj = document.getElementById('WndImage') ? document.getElementById('WndImage') : document.all.WndImage;
		if(PreviewObj){
			PreviewObj.style.display = "none";
		}
	}
}

function defPosition(event) {
    var x = y = 0;
    if (document.attachEvent != null) { // Internet Explorer & Opera
        x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
    else if (!document.attachEvent && document.addEventListener) { // Gecko
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    else {
        // Do nothing
    }
    return {x:x, y:y};
}
/*
document.onclick = function(event) {
    var event = event || window.event;
    var srcEl = event.target || event.srcElement;
    large = srcEl.getAttribute('large');
    if(large != null){
    	var pos = getAbsolutePos(srcEl);
    	xMousePos = pos.x;
    	yMousePos = pos.y;
    	LargeImage('show',large);
        return false;
    }
    else{
    	LargeImage('hide',large);
    }
    //xMousePos = defPosition(event).x + 15;
    //yMousePos = defPosition(event).y + 15;

}
*/
function ShowOrderHistory(){
	var OrderBlock = document.getElementById('OrderHistoryBlock') ? document.getElementById('OrderHistoryBlock') : document.all['OrderHistoryBlock'];
	var OrderLink = document.getElementById('historyLink') ? document.getElementById('historyLink') : document.all['historyLink'];

	if(OrderBlock.style.display == 'none'){
		OrderBlock.style.display = '';
		OrderLink.innerHTML = 'История заказов (скрыть)';
	}
	else{
        OrderBlock.style.display = 'none';
		OrderLink.innerHTML = 'История заказов (показать)';
	}

}

function showRegisterForm(){
	var CheckObj = document.getElementById('check_register') ? document.getElementById('check_register') : document.all['check_register'];
	var AreaObj = document.getElementById('register_form_area') ? document.getElementById('register_form_area') : document.all['register_form_area'];
    var FormObj = document.getElementById('reg_form_template') ? document.getElementById('reg_form_template') : document.all['reg_form_template'];

    //reg_form_template
    if(!AreaObj){return false;}
    if(CheckObj.checked){
    	AreaObj.innerHTML = FormObj.innerHTML;
    	UpdateRegisterFields();
    }
    else{
    	AreaObj.innerHTML = '';
    }
    document.form1.RegisterSubmit.disabled = false;
}

function UpdateRegisterFields(){
	var AreaObj = document.getElementById('register_form_area') ? document.getElementById('register_form_area') : document.all['register_form_area'];
    if(!AreaObj){return false;}

    var LN = document.getElementById('new_user_login') ? document.getElementById('new_user_login') : document.all['new_user_login'];
    var NE = document.getElementById('new_user_email') ? document.getElementById('new_user_email') : document.all['new_user_email'];

    var ON = document.getElementById('name') ? document.getElementById('name') : document.all['name'];
    var OE = document.getElementById('email') ? document.getElementById('email') : document.all['email'];

    LN.value = OE.value;
    NE.value = OE.value;
    document.form1.RegisterSubmit.disabled = false;
}

function CheckRegisterInfo(){
	document.form1.RegisterSubmit.disabled = false;
	var AreaObj = document.getElementById('register_form_area') ? document.getElementById('register_form_area') : document.all['register_form_area'];
    if(!AreaObj){return false;}
    if(AreaObj.innerHTML == ''){return false;}

	var LN = document.getElementById('new_user_login') ? document.getElementById('new_user_login') : document.all['new_user_login'];
    var NE = document.getElementById('new_user_email') ? document.getElementById('new_user_email') : document.all['new_user_email'];
    var NP = document.getElementById('new_user_passwd') ? document.getElementById('new_user_passwd') : document.all['new_user_passwd'];
    var NP2 = document.getElementById('new_user_passwd2') ? document.getElementById('new_user_passwd2') : document.all['new_user_passwd2'];

    if(NP.value.length == 0){document.form1.RegisterSubmit.disabled = true;}
    if(NP.value != NP2.value){document.form1.RegisterSubmit.disabled = true;}
    if(LN.value.length == 0){document.form1.RegisterSubmit.disabled = true;}
    if(NE.value.length == 0){document.form1.RegisterSubmit.disabled = true;}
}

function resetFormButton(){
	document.form1.RegisterSubmit.disabled = false;
}

function ShowFriendForm(){
	var FriendArea = document.getElementById('friend_form_area') ? document.getElementById('friend_form_area') : document.all['friend_form_area'];
	if(!FriendArea){return;}
	var width = 320;
	var height = 400;
	ScreenW = window.screen.width;
	ScreenH = window.screen.height;
	var Top = Number((ScreenH - height) / 2) + (document.body.scrollTop / 2);
	var Left = Number((ScreenW - width) / 2);
	FriendArea.style.width = width;
	FriendArea.style.height = height;
	FriendArea.style.left = Left;
	FriendArea.style.top = Top;
	FriendArea.style.display = 'block';
}

function CloseFriendForm(){
	CloseWinError();
	var FriendArea = document.getElementById('friend_form_area') ? document.getElementById('friend_form_area') : document.all['friend_form_area'];
	if(!FriendArea){return;}
	FriendArea.style.display = 'none';
	//if(document.FriendForm.FriendPost){document.FriendForm.FriendPost.reset();}
}

function CheckFriendForm(){
	//checkFriendsFields()
	//return false;
}



var FriendFieldsList = new Array();
FriendFieldsList['name'] = new Array('name', '^[А-Яа-яІіЇїЄє\u0020-]{3,100}$', 'Заполните поле <b>"Ваше имя"</b><br> Например: Василий Иванов', '0');
FriendFieldsList['email'] = new Array('email', '^[0-9a-z-_.]+@[0-9a-z-_.]+[.]{1}[a-z]{2,4}$', 'Заполните поле <b>"Ваш e-mail"</b><br> Например: vasily@gmail.com', '0');
FriendFieldsList['friend'] = new Array('friend', '^[А-Яа-яІіЇїЄє\u0020-]{3,100}$', 'Заполните поле <b>"Имя друга"</b><br> Например: Василий Иванов', '0');
FriendFieldsList['email_friend'] = new Array('email_friend', '^[0-9a-z-_.]+@[0-9a-z-_.]+[.]{1}[a-z]{2,4}$', 'Заполните поле <b>"Е-mail друга"</b><br> Например: vasily@gmail.com', '0');
FriendFieldsList['comment'] = new Array('comment', '^[0-9А-Яа-яІіЇїЄє\u0020-,.]{10,500}$|^$', 'Заполните поле <b>"Комментарии"</b>', '1');


function checkFriendsFields(){

	document.FriendForm.FriendPost.disabled = true;

	for(var key in FriendFieldsList){
        var myObj = document.getElementById(FriendFieldsList[key][0]) ? document.getElementById(FriendFieldsList[key][0]) : document.all[FriendFieldsList[key][0]];
        if(myObj.disabled == true){continue;}
        var Value = myObj.tagName == 'INPUT' ? myObj.value : myObj.value;
        var Rule = new RegExp(FriendFieldsList[key][1]);
        if(!Rule.test(myObj.value)){
        	var pos = getAbsolutePos(myObj);
            myObj.style.background = "#F6DFCF";
            myObj.focus();
            WinError(FriendFieldsList[key][2], pos, myObj);
            document.FriendForm.FriendPost.disabled = true;
			return false;
        }
        else{
            myObj.style.background = "#FFFFFF";
            CloseWinError();
			document.FriendForm.FriendPost.disabled = false;
		}
	}
}

function CheckTextFriendField(obj){

	var myObj = document.getElementById(obj) ? document.getElementById(obj) : document.all[obj];
	if(myObj.disabled == true){return false;}
	var key = myObj.name;
	var Value = myObj.tagName == 'INPUT' ? myObj.value : myObj.value;
	var Rule = new RegExp(FriendFieldsList[key][1]);
 	if(!Rule.test(Value)){
  		var pos = getAbsolutePos(myObj);
    	myObj.style.background = "#F6DFCF";
    	myObj.focus();
    	WinError(FriendFieldsList[key][2], pos, myObj);
    	document.FriendForm.FriendPost.disabled = true;
     }
     else{
     	myObj.style.background = "#FFFFFF";
     	CloseWinError();
     	document.FriendForm.FriendPost.disabled = false;
	 }

}

function UpdateQuantity(id){
	var MyAdd = document.getElementById('product_add_'+id) ? document.getElementById('product_add_'+id) : document.all['product_add_'+id];
	var MyQuantity = document.getElementById('product_quantity_'+id) ? document.getElementById('product_quantity_'+id) : document.all['product_quantity_'+id];

    if(parseInt(MyQuantity.value) < 1){MyQuantity.value = 1};


    MyAddList = MyAdd.href.split('/');
	newAdd = '';
	pos = GetPositionForPath(MyAddList, 'add') + 5;
	pos2 = GetPositionForPath(MyAddList, 'add') + 6;

	if(pos >= MyAddList.length){
		MyAdd.href = MyAdd.href + 'q/' + MyQuantity.value + '/';
		//alert(MyAdd.href);
		return;
	}
	for(i = 0; i < MyAddList.length - 1 ; i++){

		if(i == pos - 1 ){
			val = 'q';
		}
		else if(i == pos2 - 1 ){
			val = MyQuantity.value;
		}
		else{
			val = MyAddList[i];
		}
		newAdd += val + '/';
	}
	MyAdd.href = newAdd;
	//alert(MyAdd.href);
}

function GetAuthForm(){
	//alert('Не готово');
    WinPreview();
}

function WinPreview(){
 	document.body.scrollTop = 350;
 	/*
 	if (document.all){
  		document.body.scroll = "no";
  	}
   	else{
    	var oTop = document.body.scrollTop;
     	document.body.style.overflow = "hidden";
      	document.body.scrollTop = oTop;
    }
    */

	var width = 300;
	var height = 160;

	bWidth = document.body.clientWidth;
	bHeight = document.body.clientHeight;

	var top = document.body.scrollTop + 30;
	var left = (document.body.clientWidth  - width) / 2;


    // Create Image
    var Wnd = document.getElementById("popupWnd");
    if(!Wnd){
        Wnd = document.createElement("DIV");
        Wnd.id = "popupWnd";
        Wnd.style.border = 0;
        Wnd.style.position = "absolute";
        //Wnd.style.overflow = "hidden";
        Wnd.style.zIndex = "999";
        Wnd.style.display = "none";
        document.body.appendChild(Wnd);
    }
    Wnd.style.left = left;
    Wnd.style.top = top;
    Wnd.style.width = width;
    //Wnd.style.height = height;
    //Wnd.style.background = "#FFFFFF";
    Wnd.style.display = "block";
    Wnd.style.padding = 0;
    Wnd.style.textAlign = "left";

    var cnt = '';
    cnt += '<div style="width:'+width+'px; height:'+height+'px; border: 1px solid #666666; background: #FFFFFF; padding:0px;">';
    cnt += GetForms();
    cnt += '</div>';
    Wnd.innerHTML = cnt;
    /*
    if(document.attachEvent){
		document.attachEvent('onmousewheel', mScrollBody);
	}
	else if(document.addEventListener){
		document.addEventListener('scroll', mScrollBody, true);
	}
    */
}

function CloseForm(){
	var ErrObj = document.getElementById('popupWnd') ? document.getElementById('popupWnd') : document.all.popupWnd;
	if(ErrObj){
		ErrObj.style.display = "none";
	}
}

function GetForms(){
cnt = '';
cnt += '<table width="100%" border="0" cellspacing="0" cellpadding="5">';
cnt += '  <tr>';
cnt += '    <td bgcolor="#FF0707"><div style="font: bold 13px Arial; color:#FFFFFF;">Авторизация</div></td><td bgcolor="#FF0707" width="20" align="center"><div title="закрыть" style="font: bold 14px Arial; color:#FFFFFF; cursor:pointer;" onclick="CloseForm();" >X</div></td>';
cnt += '  </tr>';
cnt += '  <tr>';
cnt += '    <td align="center">';
cnt += '      <form name="form132" id="form1" method="post" action="index.php?page=register" onsubmit="checkForm(); return false">';
cnt += '        <table width="100%" border="0" cellspacing="0" cellpadding="5" class="text">';
cnt += '          <tr>';
cnt += '            <td>';
cnt += '              Логин:';
cnt += '            </td>';
cnt += '            <td>';
cnt += '              <input type="text" name="userlogin" id="userlogin">';
cnt += '            </td>';
cnt += '</tr><tr>';
cnt += '            <td>';
cnt += 'Пароль:';
cnt += '            </td>';
cnt += '            <td>';
cnt += '<input type="password" name="userpass" id="userpass">';
cnt += '            </td></tr>';
cnt += '          <tr>';
cnt += '            <td colspan="2" align="center">';
cnt += '              <input name="Register" type="hidden" value="1"><input type="submit" name="UserSubmit" value="Вход"><input name="auth" type="hidden" value="1">';
cnt += '            </td>';
cnt += '          </tr>';
cnt += '          <tr>';
cnt += '            <td colspan="2" >';
cnt += '              <a href="/page/register/">Регистрация</a>  <a href="/page/recovery/">Забыли пароль?</a>';
cnt += '            </td>';
cnt += '          </tr>';
cnt += '        </table> ';
cnt += '      </form>';
cnt += '    </td> ';
cnt += ' </tr>';
cnt += '</table>	';

return cnt
}

function checkForm(){
        var myObj1 = document.getElementById('userlogin') ? document.getElementById('userlogin') : document.all['userlogin'];
        var myObj2 = document.getElementById('userpass') ? document.getElementById('userpass') : document.all['userpass'];

        if(myObj1.value.length == 0){alert('Вы не указали логин'); myObj1.focus(); return false;}
        if(myObj2.value.length == 0){alert('Вы не указали пароль'); myObj2.focus(); return false;}


		document.form132.submit();

		//alert('OK');
}


function GetProductPicture(id, prod){
	var MyPicture = document.getElementById('product_image_'+prod) ? document.getElementById('product_image_'+prod) : document.all['product_image_'+prod];
	var MyLink = document.getElementById('product_link_'+prod) ? document.getElementById('product_link_'+prod) : document.all['product_link_'+prod];
    var MyPictureLargeLink = document.getElementById('product_large_image_link_'+prod) ? document.getElementById('product_large_image_link_'+prod) : document.all['product_large_image_link_'+prod];


	if(ProductGalleryList[id]){
		MyPicture.src = '/uploads/'+ProductGalleryList[id][0];
		MyPictureLargeLink.href = '/uploads/large/'+ProductGalleryList[id][1];
	}

}

// Compare function ============================================================
var req;
function UpdateCompareProduct(id, action){
    var url = '/compare.php?id=' + id +'&action=' + action;

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();

        req.onreadystatechange = function() {
        	var cont = document.getElementById('compare_box_'+id) ? document.getElementById('compare_box_'+id) : document.all['compare_box_'+id];

    		if (req.readyState == 4) {
    			if (req.status == 200) {
            		cont.innerHTML = req.responseText;
        		}
        		else{
        			cont.innerHTML = req.statusText;
        		}
    		}
    		else{
    			cont.innerHTML = '<p align="center"><img src="/images/loading.gif"></p>';
    		}
        };
        req.open("GET", url, true);
        req.send(null);

	}

    else if(window.ActiveXObject){
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req){
            req.onreadystatechange = function (){
            	var cont = document.getElementById('compare_box_'+id) ? document.getElementById('compare_box_'+id) : document.all['compare_box_'+id];
    			if (req.readyState == 4) {
    				if (req.status == 200) {
            			cont.innerHTML = req.responseText;
        			}
        			else{
        				alert(req.statusText);
        			}
    			}
    			else{
    				cont.innerHTML = '<p align="center"><img src="/images/loading.gif"></p>';
    			}
        	};

            req.open("GET", url, true);
        	req.send();
        }
    }
}