// JavaScript Document check_required_fields_js.tpl
function checkRequired(id,required_arr) {
if(required_arr)  requiredFields=required_arr; //޸
var errors = new Array();
var x, true_id, type;
var cnt = 0;
var id_element = -1;

	if(!requiredFields)
		return true;
	if(requiredFields.length == 0)
		return true;

	if(id) {
		for(x = 0; x < requiredFields.length; x++)
			if(requiredFields[x][0] == id) {
				id_element = x;
				break;
			}
	}

	for(x = 0; x < requiredFields.length; x++) {
		if(x != id_element && id_element != -1)
			continue;
		if(requiredFields[x].length > 2)
			true_id = ((requiredFields[x][2] && document.getElementById("_"+requiredFields[x][0]))?"_":"")+requiredFields[x][0];
		else
			true_id = ((document.getElementById("_"+requiredFields[x][0]))?"_":"")+requiredFields[x][0];
		if(document.getElementById(true_id)) {
			type = document.getElementById(true_id).type;
			if(document.getElementById(true_id).value == '' && (type == 'text' || type == 'password' || type == 'textarea'))
				errors[cnt++] = new Array(requiredFields[x][1],true_id);
		}
	}

	if(errors.length == 0)
		return true;

	if(lbl_required_field_is_empty != '')
		alert(lbl_required_field_is_empty.replace(/~~field~~/gi, errors[0][0]));
	else
		alert(errors[0][0]);

	if(!document.getElementById(errors[0][1]).disabled && document.getElementById(errors[0][1]).type != 'hidden')
		document.getElementById(errors[0][1]).focus();
	
	return false;
}

function checkEmailAddress(field, empty_err) {
var err = false;
var res, x;
	if(!field)
		return true;

	if(field.value.length == 0) {
		if(empty_err != 'Y')
			return true;
		else
			err = true;
	}

	var arrEmail = field.value.split('@');
	if(arrEmail.length != 2 || arrEmail[0].length < 1)
		err = true;
	if(!err) {
		if(arrEmail[0].length > 2)
			res = arrEmail[0].search(/^[-\w][-\.\w]+[-\w]$/gi);
		else
			res = arrEmail[0].search(/^[-\w]+$/gi);
		if(res == -1)
			err = true;
	}
	if(!err) {
		var arr2Email = arrEmail[1].split('.');
		if(arr2Email.length < 2)
			err = true;
	}
	if(!err) {
		var domenTail = arr2Email[arr2Email.length-1];
		var _arr2Email = new Array();
		for(x = 0; x < arr2Email.length-1; x++)
			_arr2Email[x] = arr2Email[x];
		arr2Email = _arr2Email;
		var domen = arr2Email.join('.');
		res = domen.search(/^[-!#\$%&*+\\\/=?\.\w^`{|}~]+$/gi);
		if(res == -1)
			err = true;
		res = domenTail.search(/^[a-zA-Z]+$/gi);
		if(res == -1 || domenTail.length < 2 || domenTail.length > 6)
			err = true;
	}
//	/^([-\w][-\.\w]*)?[-\w]@([-!#\$%&*+\\\/=?\w^`{|}~]+\.)+[a-zA-Z]{2,6}$/gi

	if (err) {
		alert(email_err);
		field.focus();
		field.select();
	}
	return !err;
}
// check_options.tpl
function check_options() {
var cnt, flag;

	var local_taxes = taxes.slice(0);
	price = default_price;
	var wp_found = -1;

	var select_avail = 1;
	if (document.getElementById('product_avail'))
		document.getElementById('product_avail').options[document.getElementById('product_avail').selectedIndex].value;;

	// Find wholesale price
	if (product_wholesale.length  > 0 && variants.length == 0) {
		for (var x = 0; x < product_wholesale.length; x++) {
			if (product_wholesale[x][0] <= select_avail && (product_wholesale[x][1] >= select_avail || product_wholesale[x][1] == 0)) {
				price = product_wholesale[x][2];
				local_taxes = product_wholesale[x][3].slice(0);
				wp_found = x;
				break;
			}
		}
	}

	// Find variant
	for (var x = 0; x < variants.length; x++) {
		if (variants[x][1].length == 0)
			continue;
		flag = true;
		for (var c in variants[x][1]) {
			if (document.getElementById('po'+c)) {
				if (document.getElementById('po'+c).value != variants[x][1][c]) {
					flag = false;
					break;
				}
			}
		}

		// If variant found ...
		if (flag) {
			price = variants[x][0][0];
			orig_price = variants[x][0][4];
			avail = variants[x][0][1];
			for (var t in local_taxes)
				if (variants[x][2][t])
					local_taxes[t] = parseFloat(variants[x][2][t]);
			if (variants[x][0][2] != '' && document.getElementById('product_thumbnail'))
				document.getElementById('product_thumbnail').src = image_dir+variants[x][0][2];
			if (document.getElementById('product_weight'))
				document.getElementById('product_weight').innerHTML = variants[x][0][3];
			break;
		}
	}

	if (pconf_price > 0)
		price = pconf_price;

	// Find modifiers
	var add_price = 0;
	for (var x in modifiers) {
		if (document.getElementById('po'+x).value) {
			if (modifiers[x][document.getElementById('po'+x).value]) {
				add_price += parseFloat(modifiers[x][document.getElementById('po'+x).value][1] == '$'?modifiers[x][document.getElementById('po'+x).value][0]:(price*modifiers[x][document.getElementById('po'+x).value][0]/100));
				for (var t in local_taxes) {
					if (modifiers[x][document.getElementById('po'+x).value][2][t]) {
						local_taxes[t] = parseFloat(local_taxes[t])+parseFloat(modifiers[x][document.getElementById('po'+x).value][1] == '$'?modifiers[x][document.getElementById('po'+x).value][2][t]:(orig_price*modifiers[x][document.getElementById('po'+x).value][0]/100*modifiers[x][document.getElementById('po'+x).value][2][t]/100));
					}
				}
			}
		}
	}
	price += add_price;

	// Update taxes
	for (var t in local_taxes) {
		if(document.getElementById('tax_'+t))
			document.getElementById('tax_'+t).innerHTML = currency_symbol+price_format(local_taxes[t], 2);
		current_taxes[t] = local_taxes[t];
	}

	// Update price
	if (document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price, 2);

	// Update alt. price
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price'))
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(price*alter_currency_rate, 2)+")";

	// Update Save %
	if (document.getElementById('save_percent') && list_price > 0 && dynamic_save_money_enabled == 'Y')
		document.getElementById('save_percent').innerHTML = (price < list_price) ? Math.round(100-(price/list_price)*100) : '0';

	if ((mq > 0 && avail > mq) || is_unlimit == 'Y')
		avail = mq;

	// Update product quantity
	if (document.getElementById('product_avail_txt'))
		document.getElementById('product_avail_txt').innerHTML = ((avail > 0)?(flag?avail:product_avail):txt_no);
	select_avail = min_avail;
	// Update product quantity selector
	if (document.getElementById('product_avail')) {
		if (document.getElementById('product_avail').options) {
			if (!isNaN(min_avail) && !isNaN(avail)) {
				var first_value = -1;
				if (document.getElementById('product_avail').options[0])
					first_value = document.getElementById('product_avail').options[0].value;
				if (first_value == min_avail) {
					if ((avail-min_avail+1) != document.getElementById('product_avail').options.length) {
						if (document.getElementById('product_avail').options.length > avail) {
							cnt = document.getElementById('product_avail').options.length;
							for (var x = avail; x < cnt; x++)
								document.getElementById('product_avail').options[document.getElementById('product_avail').options.length-1] = null;
						} else {
							cnt = document.getElementById('product_avail').options.length;
							for (var x = cnt+1; x <= avail; x++)
								document.getElementById('product_avail').options[cnt++] = new Option(x, x);
						}
					}
				} else {
					while (document.getElementById('product_avail').options.length > 0)
						document.getElementById('product_avail').options[0] = null;
					cnt = 0;
					for (var x = min_avail; x <= avail; x++)
						document.getElementById('product_avail').options[cnt++] = new Option(x, x);
				}
				if (document.getElementById('product_avail').options.length == 0)
					document.getElementById('product_avail').options[0] = new Option(txt_out_of_stock, 0);
			}
		}
		select_avail = document.getElementById('product_avail').options[document.getElementById('product_avail').selectedIndex].value;;
	}

	// Update wholesale prices table
	if (product_wholesale.length  > 0 && variants.length == 0) {
		found = false;
		for (var x = 0; x < product_wholesale.length; x++) {
			if (!document.getElementById('wp'+x))
				continue;
			var wp_add_price = product_wholesale[x][2];
			for (var y in modifiers) {
				if (document.getElementById('po'+y).value) {
					if (modifiers[y][document.getElementById('po'+y).value]) {
						wp_add_price += parseFloat(modifiers[y][document.getElementById('po'+y).value][1] == '$' ? modifiers[y][document.getElementById('po'+y).value][0] : (product_wholesale[x][2]*modifiers[y][document.getElementById('po'+y).value][0]/100));
					}
				}
			}
			document.getElementById('wp'+x).innerHTML = currency_symbol+price_format(wp_add_price, 2);
		}
	}

	if ((alert_msg == 'Y') && (min_avail > avail))
		alert(txt_out_of_stock);
	
	// Check exceptions
	var ex_flag = check_exceptions();
	if (!ex_flag && (alert_msg == 'Y'))
		alert(exception_msg);
			
	if (document.getElementById('exception_msg'))
		document.getElementById('exception_msg').innerHTML = (ex_flag ? '' : exception_msg+"<BR><BR>");

	return true;
}

function check_exceptions() {
var x, cnt;

	for(x in exceptions) {
        flag = true;
		if(exceptions[x].length != names.length)
			continue;
        for(c in exceptions[x])
            if(document.getElementById('po'+c).value != exceptions[x][c]) {
                flag = false;
                break;
            }
		if(flag)
			return false;
	}
	return true;
}

function price_format(price, precision) {
var x, cnt, top, botom;
	precision = Math.pow(10, precision);
	price = Math.round(price*precision)/precision;
	top = Math.floor(price);
	bottom = Math.round((price-top)*precision)+precision;
	top = top+"";
	bottom = bottom+"";
	cnt = 0;
	for(x = top.length; x >= 0; x--) {
		if(cnt%3 == 0 && cnt > 0 && x > 0)
			top = top.substr(0,x)+","+top.substr(x,top.length);
		cnt++;
	}
	price = top+"."+bottom.substr(1,bottom.length);
	return price;
}

function product_option(classid) {
	if(!isNaN(classid)) {
		return document.getElementById(classid);
	} else if(names) {
		var x;
		for(x in names) {
			if(names[x] == classid) {
				if(document.getElementById('po'+x)) {
					return document.getElementById('po'+x);
				}
				break;
			}
		}
	}
	return false;
}

function product_options(name) {
var x;
	if(names)
		for(x in names)
			if(names[x] == name) {
				if(document.getElementById('po'+x)) {
					var obj = document.getElementById('po'+x);
					return (obj.type == 'select-one')?obj.options[obj.selectedIndex].text:obj.value;
				}
				break;
			}
	return false;
}
//产品option变更,在点图片时候
function colorChange(elementid,index){
	$("#po"+elementid).get(0).selectedIndex=index;
	combine_your_choice();
}
//
function AutoScroll(obj){
        $(obj).find("ul:first").animate({
                marginTop:"-25px"
        },200,function(){
                $(this).css({marginTop:"0px"}).find("li:first").appendTo(this);
        });
}


function combine_your_choice(elementid){
  var my_custom_made_var = new String;

  var my_custom_made_vars = $("#pro_option select");
  for(i=0;i<my_custom_made_vars.length; i++){
    my_custom_made_vars[i].value?my_custom_made_var+="   "+$("#"+my_custom_made_vars[i].id).parent().attr("id")+":"+$("#"+my_custom_made_vars[i].id+" :selected").text():"";
	
  }
  
  var my_custom_made_vars = $("#pro_option input not hidden");
  for(i=0;i<my_custom_made_vars.length; i++){
    my_custom_made_vars[i].value?my_custom_made_var+="   "+my_custom_made_vars[i].name+":"+my_custom_made_vars[i].value:"";
	
  }
  
  var my_custom_made_vars = $(".custom-made-size input");
  var my_custom_made_var1 = new String;
  for(i=0;i<my_custom_made_vars.length; i++){
    $.trim(my_custom_made_vars[i].value)?my_custom_made_var1+="   "+my_custom_made_vars[i].name+":"+my_custom_made_vars[i].value+"cm ":"";
  }
   $("#"+elementid).val(my_custom_made_var1);
   $("#your_choose").html("Your Chioce:"+my_custom_made_var+"</br>"+my_custom_made_var1);
}
function my_txt_scroll_pause(){
clearInterval(var_scroll);
}

function my_txt_scroll_start(){
  var_scroll=setInterval('AutoScroll("#scrollDiv")',2100)
}

