
var FRIENDLY_URLS='true';
function sortFocus(obj){
	if(isIE){
		obj.value ='';
	}
	else{
		o=new Option('','-1');
	    obj.options.add(o);
		obj.value ='-1';
	}	
}
function sortBlur(obj, value){
	if(isIE){
		obj.value ='value';
	}
}
function changeSort(obj, sort_url){
	if(obj.value != '-1'){
		if(sort_url.indexOf('?') > -1){
			window.location.href= sort_url + "&productsort=" + obj.value;
		}
		else{
			window.location.href= sort_url + "?productsort=" + obj.value;
		}
	}
}
function changePagesize(obj, sort_url){
	if(obj.value != '-1'){
		if(sort_url.indexOf('?') > -1){
			window.location.href= sort_url + "&pagesize=" + obj.value;
		}
		else{
			window.location.href= sort_url + "?pagesize=" + obj.value;
		}
	}
}
function changePage(obj, sort_url){
	if(obj.value != '-1'){
		if(obj=="1"){
			window.location.href= sort_url +".html";
		}else{
			window.location.href= sort_url +"-"+obj.value+".html";
		}
	}	
}

function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
  // a name/value pair (a crumb) is separated by an equal sign
  var aCrumb = aCookie[i].split("=");
  if (sName == aCrumb[0])
    return unescape(aCrumb[1]);
  }
    // a cookie with the requested name does not exist
    return null;
}
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
  var expires = new Date();
  expires.setTime(expires.getTime() + seconds);
  document.cookie = escape(cookieName) + '=' + escape(cookieValue)
    + (expires ? '; expires=' + expires.toGMTString() : '')
    + (path ? '; path=' + path : '/')
    + (domain ? '; domain=' + domain : '')
    + (secure ? '; secure' : '');
}

var FRIENDLY_URLS = 'true';
var symbolLeft = 'US$';
var symbolRight = '';
var min_quantity = 1;

var discount = new Array();
discount[0] = "1-3.73-0-0";
function formatC(s, flag){
    if (flag == null) {
        flag = true;
    }
    s = s + '';
    if (/[^0-9\.]/.test(s)) 
        return "invalid value";
    s = s.replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s)) 
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    if (flag) {
        return symbolLeft + ' ' + s.replace(/^\./, "0.") + symbolRight;
    }
    else {
        return s.replace(/^\./, "0.") + symbolRight;
    }
    return symbolLeft + ' ' + s.replace(/^\./, "0.") + symbolRight;
}

function check_product(frm){
    if ($('cart_quantity').value < min_quantity) {
        alert('The Quantity you submitted is invalid.');
        return false;
    }
    return true;
}

function in_array(needle, haystack){
    var type = typeof needle;
    if (type == 'string' || type == 'number') {
        for (var i in haystack) {
            if (haystack[i] == needle) {
                return true;
            }
        }
    }
    return false;
}

function is_array(array){
    if (typeof array == 'object' && typeof array.sort == 'function' && typeof array.length == 'number') {
        return true;
    }
    else {
        return false;
    }
}

function changePrice(){
    var qty = jQuery('#cart_quantity').value;
    var tmp;
    for (var i = discount.length - 1; i >= 0; i--) {
        tmp = discount[i].split("-");
        if (qty >= parseInt(tmp[0])) {
        	jQuery('#products_price_unit').innerHTML = formatC(tmp[1], false);
        	jQuery('#products_price_all').innerHTML = '&nbsp;' + formatC(parseInt(qty) * parseFloat(tmp[1])) + ' ';
            if (parseInt(tmp[2]) > 0) {
            	jQuery('#shipping_rule').innerHTML = ("+ " + formatC(parseFloat(tmp[3])) + " Free Shipping ");
            }
            else {
            	jQuery('#shipping_rule').innerHTML = ("+ Shipping Cost");
            }
            break;
        }
        else {
        	jQuery('#products_price_unit').innerHTML = formatC(tmp[1], false);
        }
    }
}

function changeShippingCost(){
    var standardArray = new Array();
    var expeditedArray = new Array();
    var standardSkipArray = new Array();
    var expeditedSkipArray = new Array();
    var tmpa;
    var tmpb;
    var country_ida;
    var country_idb;
    var tmpCosta;
    var tmpCostb;
    var costa;
    var costb;
    var weight = 0.048;
    var productsprice = 3.73;
    var standardSkip = '';
    var standard = '';
    var expeditedSkip = '';
    var expedited = '';
    var country = $('country_id').value;
    var qty = $('shippingCostQty').value;
    var units = ' kg';
    var maxweight = 300000;
    
    standardSkipArray = standardSkip.split(',');
    expeditedSkipArray = expeditedSkip.split(',');
    
    standardArray = standard.split('-');
    expeditedArray = expedited.split('-');
    
    $('weight').innerHTML = parseInt(qty) * parseFloat(weight) + units;
    
    
    
    if (qty == '' || qty == 0 || country == '-1' || maxweight <= parseInt(qty) * parseFloat(weight)) {
        if (maxweight <= parseInt(qty) * parseFloat(weight)) {
            alert('Has exceeded the weight limit, please re-enter');
            return false;
        }
        else {
            alert('Please Input Qty. Or Select Country');
            return false;
        }
    }
    else {
        if (in_array(country, standardSkipArray) || country == standardSkipArray) {
            $('standard').innerHTML = 'sorry,The countries do not provide transportation';
        }
        else {
            /* Standard Shipping Cost - Start */
            Outer: for (var i = 0; i < 3; i++) {
                if (standardArray[i] != '' && standardArray[i] != null) {//����if�ж�
                    tmpa = standardArray[i].split('|');
                    country_ida = tmpa[0].split(',');
                    if (in_array(country, country_ida)) {
                        tmpCosta = tmpa[1].split(',');
                        Inner: for (var i = 0, n = tmpCosta.length; i < n; i++) {
                            costa = tmpCosta[i].split(':');
                            if (parseInt(qty) * parseFloat(weight) <= parseFloat(costa[0])) {
                                if (costa[1].indexOf('%') == -1) {
                                    $('standard').innerHTML = formatC(parseFloat(costa[1]));
                                }
                                else {
                                
                                    $('standard').innerHTML = formatC((parseFloat(costa[1]) / 100) * parseFloat(productsprice));
                                }
                                break Inner;
                            }
                        }
                        break Outer;
                    }//end if
                    else 
                        if (in_array('00', country_ida)) {
                            tmpCosta = tmpa[1].split(',');
                            InnerTwo: for (var i = 0, n = tmpCosta.length; i < n; i++) {
                                costa = tmpCosta[i].split(':');
                                if (parseInt(qty) * parseFloat(weight) <= parseFloat(costa[0])) {
                                    if (costa[1].indexOf('%') == -1) {
                                        $('standard').innerHTML = formatC(parseFloat(costa[1]));
                                    }
                                    else {
                                        $('standard').innerHTML = formatC((parseFloat(costa[1]) / 100) * parseFloat(productsprice));
                                    }
                                    break InnerTwo;
                                }
                            }
                            break Outer;
                        }//end if
                        else {
                            $('standard').innerHTML = '';
                        }
                }//if	
            }/* Standard Shipping Cost - End */
        }
        
        if (in_array(country, expeditedSkipArray) || country == expeditedSkipArray) {
            $('express').innerHTML = 'sorry,The countries do not provide transportation';
        }
        else {
            /* Express Shipping Cost - Start */
            OuterTwo: for (var i = 0; i < 3; i++) {
                if (expeditedArray[i] != '' && expeditedArray[i] != null) {//
                    tmpb = expeditedArray[i].split('|');
                    country_idb = tmpb[0].split(',');
                    
                    if (in_array(country, country_idb)) {
                        tmpCostb = tmpb[1].split(',');
                        InnerThree: for (var i = 0, n = tmpCostb.length; i < n; i++) {
                            costb = tmpCostb[i].split(':');
                            if (parseInt(qty) * parseFloat(weight) <= parseFloat(costb[0])) {
                                if (costb[1].indexOf('%') == -1) {
                                    $('express').innerHTML = formatC(parseFloat(costb[1]));
                                }
                                else {
                                    $('express').innerHTML = formatC((parseFloat(costb[1]) / 100) * parseFloat(productsprice));
                                }
                                break InnerThree;
                            }
                        }
                        break OuterTwo;
                    }//end if
                    else 
                        if (in_array('00', country_idb)) {
                            tmpCostb = tmpb[1].split(',');
                            InnerFour: for (var i = 0, n = tmpCostb.length; i < n; i++) {
                                costb = tmpCostb[i].split(':');
                                if (parseInt(qty) * parseFloat(weight) <= parseFloat(costb[0])) {
                                    if (costb[1].indexOf('%') == -1) {
                                        $('express').innerHTML = formatC(parseFloat(costb[1]));
                                    }
                                    else {
                                        $('express').innerHTML = formatC((parseFloat(costb[1]) / 100) * parseFloat(productsprice));
                                    }
                                    break InnerFour;
                                }
                            }
                            break OuterTwo;
                        }//end if
                        else {
                            $('express').innerHTML = '';
                        }
                }//
            }/* Express Shipping Cost - End */
        }
    }
}
//显示购物车
jQuery(document).ready(function(){
	jQuery.ajax({
	url:"index.php?main_page=display_cart",
	success:function(data){jQuery("#s_menu").html(jQuery(data).find('#s_menu').html());}
});
});

//鼠标放上去会出现一个跟随的tip
//var rate_len_s;
//var rate_len_e;
//var rate_len;
//var products_price;
//jQuery.ajax({
//	type:"POST",
//	url:"index.php",
//	data:"main_page=ajax_rate&ajax_rate_price="+products_price,
//	success:function(msg){
//		rate_len_s=msg.indexOf("<ul id='productQuantityDiscounts'>");
//		rate_len_e=msg.indexOf("</ul>");
//		rate_len=msg.substring(rate_len_s,rate_len_e+5);
//		jQuery("#ajax_rate").append(rate_len);
//	}
//});
//var tip={
//	$:function(ele){ 
//	    if(typeof(ele)=="object") 
//	        return ele; 
//	    else if(typeof(ele)=="string"||typeof(ele)=="number") 
//	        return document.getElementById(ele.toString()); 
//	        return null; 
//	    }, 
//    mousePos:function(e){ 
//        var x,y; 
//        var e = e||window.event; 
//        return{x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop}; 
//    }, 
//    start:function(obj){ 
//        var self = this; 
//        var t = self.$("ajax_rate"); 
//        obj.onmousemove=function(e){ 
//            var mouse = self.mousePos(e);     
//            t.style.left = mouse.x + 10 + 'px'; 
//            t.style.top = mouse.y + 10 + 'px'; 
//            t.innerHTML = rate_len; 
//            t.style.display = ''; 
//        } 
//        obj.onmouseout=function(){ 
//            t.style.display = 'none'; 
//        } 
//    } 
//} 
