var ns=(document.getElementById&&!document.all);



function minquantity(qty, minqty){
	if (minqty>qty) {
		alert('Product quantity less then minimum quantity!');
		return false;
	}
	return true;
}



function prodstyleprice(theForm, act, styles_cnt, colors_cnt){
	// flag = 0 - no styles
	// flag = 1 - one style
	// flag = 2 - many styles
	//alert ('hi colors_cnt='+colors_cnt+"styles_cnt="+styles_cnt);
	var fld_price, fld_link, price, fld_id, fld_name, style_name_opt;
	if (styles_cnt == 0)
	var id=''; //
	else if (styles_cnt == 1)
	var id=theForm.prod_style_id.value; //
	else
	var id=theForm.prod_style_id.options[theForm.prod_style_id.selectedIndex].value; //
	if(ns) {
		fld_price = document.getElementById('prod_price');
		if (colors_cnt > 1){
			for (var k=1; k<=colors_cnt; k++){
				eval("var fld_link"+k+", fld_id"+k+", fld_name"+k+";");
				eval("fld_link"+k+" = document.getElementById('style_link"+k+"');");
				eval("fld_id"+k+" = document.getElementById('color_id"+k+"');");
				eval("fld_name"+k+" = document.getElementById('color_name"+k+"');");
			}
		} else {
			fld_link = document.getElementById('style_link');
			fld_id = document.getElementById('color_id');
			fld_name = document.getElementById('color_name');
		}
		price = document.getElementById('price'+id);
		style_name_opt = document.getElementById('style_name_opt');
	}
	else {
		fld_price = document.all.prod_price;
		if (colors_cnt > 1){
			for (var k=1; k<=colors_cnt; k++){
				eval("var fld_link"+k+", fld_id"+k+", fld_name"+k+";");
				eval("fld_link"+k+" = document.all.style_link"+k+";");
				eval("fld_id"+k+" = document.all.color_id"+k+";");
				eval("fld_name"+k+" = document.all.color_name"+k+";");
			}
		} else {
			fld_link = document.all.style_link;
			fld_id = document.all.color_id;
			fld_name = document.all.color_name;
		}
		price = eval("theForm.price"+id);
		style_name_opt = document.all.style_name_opt;   //text
	}   //alert('2');
	if (id != '')	{//alert('id != "" '+styles_cnt);
		fld_price.innerHTML = "$" + price.value;
		if (colors_cnt > 1){
			for (var k=1; k<=colors_cnt; k++){
				//alert("fld_link"+k);
				//eval("fld_link"+k+".innerHTML = \"<input type='button' id='choosediv"+k+"' value='Choose Color' onclick='openWin(\"+'\"viewcolors.php?style_id="+id+"&\",\"color_id"+k+"\",\"color_name"+k+"\",\"color_name_opt"+k+"\"'+\")'>\";");
				//alert();
				//eval("fld_link"+k).innerHTML = "<input type='button' id='choosediv"+k+"' value='Choose Color' onclick='openWin(\"+'\"viewcolors.php?style_id="+id+"&\",\"color_id"+k+"\",\"color_name"+k+"\",\"color_name_opt"+k+"\"'+\")'>";
				//eval("fld_link"+k).innerHTML = "<input type='button' id='choosediv"+k+"' value='Choose Color' onclick='openWin(\"viewcolors.php?style_id="+id+"&\",\"color_id"+k+"\",\"color_name"+k+"\",\"color_name_opt"+k+"\")'>"
			}
		} else {
			fld_link.innerHTML = "<input type='button' value='Choose Color' onclick='openWin("
			+'"viewcolors.php?style_id='+id+'&","color_id","color_name","color_name_opt"'+")'>";
		}
		if (styles_cnt == 1) {//alert('id == "1" '+theForm.style_name_opt.value);
			style_name_opt.value = theForm.style_name_opt.value;
		}
		else
		{ //alert('id != "1"');
			style_name_opt.value = theForm.prod_style_id.options[theForm.prod_style_id.selectedIndex].text;
		}
	}
	else if (colors_cnt == 1){
		fld_price.innerHTML = "please select color style first"; //alert('3 '+fld_link);
		fld_link.innerHTML = ""; //alert('4');
	}
	if (act != 'updateincart'){
		//alert(act+'  '+fld_name.innerHTML);
		if (colors_cnt > 1){
			for (var k=1; k<=colors_cnt; k++){
				eval("fld_id"+k+".value = '';");
				eval("fld_name"+k+".innerHTML = '';");
			}
		} else if (colors_cnt == 1){
			fld_id.value = "";
			fld_name.innerHTML = "";
		}
	}
}

win=0;

function openWin(winurl,field_id, field_name, field_name_opt) {
	var w=screen.width;
	var h=screen.height;
	var ww=w/2;
	var hh=h-150;
	var top=0;
	var left=ww-10;
	if ((win)&&(win.closed)) {
		win=0;
	}
	if (field_id == 'undefined')
	field_id = '';
	if (field_name == 'undefined')
	field_name = '';
	win=open(winurl+'field_id='+field_id+'&field_name='+field_name+'&field_name_opt='+field_name_opt,'mywin','toolbar=0,scrollbars=1,resizable=yes,location=yes,status=yes,menubar=no,toolbar=no, top='+top+', left='+left+', height='+hh+', width='+ww);
	win.focus();
}



function openPhotoWin(winurl,id) {
	var w=screen.width;
	var h=screen.height;
	var ww=900;
	var hh=800;
	var top=0;
	var left=w-ww-10;
	eval("var win"+id+";");
	eval("if (win"+id+" == null) win"+id+" = 0;");
	eval("if ((win"+id+")&&(win"+id+".closed)) {win"+id+"=0;}");
	eval("win"+id+"=open(winurl,'photowin'+id,'toolbar=0,scrollbars=1,resizable=yes,location=no,status=yes,menubar=no,toolbar=no, top='+top+', left='+left+', height='+hh+', width='+ww);");
	eval("win"+id+".focus();");
}


function removeBRs(str) {
	str = str.replace(/<br>/g," ");
	str = str.replace(/<BR>/g," ");
	return str;
};

function setDataValue(data, field) {
	if(ns) {
		elm = opener.document.getElementById(field);
	}
	else {
		elm = eval("opener.document.all."+field);
	}
	//alert("field="+field);
	elm.value=removeBRs(data);
	opener.testFunction(field);

};

function setDataHTML(data, field) {
	if(ns) {
		elm = opener.document.getElementById(field);
	}
	else {
		elm = eval("opener.document.all."+field);
	}

	elm.innerHTML=removeBRs(data);
};

function remove(){
	//cmsg = "Sure you want to delete these products from cart?";
	//if (confirm(cmsg)) {
	document.location.href = "?act=del";
	//}
}

function show_gallery_desc(desctext){
	var elm;
	if(ns) {
		elm = document.getElementById('description');
	}
	else {
		elm = document.all.description;
	}
	elm.innerHTML= desctext;
	//insertAdjacentHTML ("AfterBegin",'"'+desctext+'"');
	//
}

function formvalidprod(theForm, minqty, styles_cnt, colors_cnt) {
	if (colors_cnt == 0 || styles_cnt == 0) {
		;
	} else if (styles_cnt > 1) { // больше 1 стиля
	if (theForm.prod_style_id.options[theForm.prod_style_id.selectedIndex].value == "")
	{
		alert("Please select \"Color Style\".");
		theForm.prod_style_id.focus();
		return (false);
	}
	}
	if (colors_cnt == 1) {
		if (theForm.color_id.value == "")
		{
			alert("Please choose \"Color\".");
			//theForm.choose_color.focus();
			return (false);
		}
	} else if (colors_cnt > 1) {
		var i=0;
		for (var k=1; k<=colors_cnt; k++){
			if (eval("theForm.color_id"+k+".value") != "")
			{
				//alert("Please choose \"Color "+k+"\".");
				//theForm.choose_color.focus();
				//return (false);
				i++;
			}
		};
		if (!i) {
			alert("Please select at least one color to proceed!");
			return (false);
		};
		
	};/*
	else if (colors_cnt == 1){
	if (theForm.color_id.value == "")
	{
	alert("Please choose \"Color\".");
	//theForm.choose_color.focus();
	return (false);
	}
	} else {
	if (theForm.prod_style_id.options[theForm.prod_style_id.selectedIndex].value == "")
	{
	alert("Please select \"Color Style\".");
	theForm.prod_style_id.focus();
	return (false);
	}
	if (theForm.color_id.value == "")
	{
	alert("Please choose \"Color\".");
	//theForm.choose_color.focus();
	return (false);
	}
	}   */
	if (theForm.quantity.value < minqty)
	{
		alert("The value in field \"Quantity\" must be equal or greater then minimum allowed quantity.");
		theForm.quantity.value =  minqty;
		theForm.quantity.focus();
		return (false);
	}
	//quantity
	return true;
}



