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 = 250;
    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 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 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];

	if(ProductColorList[id]){		MyPicture.src = '/uploads/'+ProductColorList[id][1];
		MyPictureLarge.src = '/uploads/large/'+ProductColorList[id][1];
		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;

		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];
    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;

		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 width = 250;
    	var height = 85;
    	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 = xMousePos;
    	WndImg.style.top = yMousePos;
    	WndImg.style.width = width;
    	WndImg.style.height = height;
    	WndImg.style.padding = 0;
    	WndImg.style.display = "block";
    	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.onmousemove = function(event) {
    var event = event || window.event;
    var srcEl = event.target || event.srcElement;
    large = srcEl.getAttribute('large');
    if(large != null){    	LargeImage('show',large);
    }
    else{    	LargeImage('hide',large);
    }
    xMousePos = defPosition(event).x + 15;
    yMousePos = defPosition(event).y + 15;
}
*/
