﻿

$(document).ready(function(){
	$('#main_nav > ul > li > ul').each(function(){
			$(this).hover(function(){$(this).parent().attr('id', 'current').children('a').css({'margin-left':'0'});$(this).parent().children('ul').find('a').css({'background-color':'#fff','color':'#6884c5','text-shadow':'none','margin':'0'});}, function(){$(this).parent().attr('id', '');$(this).hide();});});
	$('#main_nav > ul > li > a').hover(function(){$('#menu > ul > li > ul').hide();$(this).parent().children('ul').show();}, function(){});
})
function get_form(code, description, type, codes) {
    var url = '/get_form.php';
	var back_url = encodeURIComponent(location.href);
	
	if (code != '') {
		if (!$('#cart_' + code + '_' + type).attr('action')) $('#cart_' + code + '_' + type).attr('action', $('#cart_' + code + '_' + type + ' > script').html());
		$.ajax({
			type:"GET",
			url:url,
			cache:false,
			data:"code=" + code + "&description=" + description + "&type=" + type + "&back_url=" + back_url,
			success:function(data) {
				$('#cart_' + code + '_' + type).html(data);
			}
		});
	} else {
		if (!$('#cart_' + type).attr('action')) $('#cart_' + type).attr('action', $('#cart_' + type + ' > script').html());
		var dataStr = "codes=" + codes + "&description=" + description + "&type=" + type + "&back_url=" + back_url;
			dataStr += $('#spec_volt').length != 0 ? '&volt=' + $('#spec_volt').html() : '';
			dataStr += $('#spec_capacity').length != 0 ? '&capacity=' + $('#spec_capacity').html() : '';
			dataStr += $('#spec_color').length != 0 ? '&color=' + $('#spec_color').html() : '';
			dataStr += $('#spec_layout').length != 0 ? '&layout=' + $('#spec_layout').html() : '';
		$.ajax({
			   type: "GET",
			   url:url,
			   cache:false,
			   data:dataStr,
			   success:function(data){
				$('#cart_' + type).html(data);
			   }		
		});
	}
}

function get_model_list(brand, series, model_type, type, jqueryObj) {	
	jqueryObj.next().html('<img src="/images/loading.gif" alt="loading" style="margin-left:300px"/>');
	var url = '/get_form.php';
	$.ajax({
		type:"GET",
		url:url,
		cache:false,
		data:"brand=" + brand + "&series=" + series + "&model_type=" + model_type + "&type=" + type,
		success:function(data) {
			jqueryObj.next().html(data);
		}
	});
}
function change_attribute(obj, specName) {
	obj.parent().children().each(function(i){
		$(this).removeClass('current');
	});
	obj.addClass('current');
	$('#spec_' + specName).html(obj.attr('title'));
	if ($('#cart_form').length != 0) eval($('#cart_form').attr('action'));
	if (specName.indexOf('color') >= 0) {
		var arr = new Array();
			arr[1] = 0;
			arr = specName.split('_');
		$("input[name='item_name']").each(function(i){
			$(this).val($(this).attr('des') + ' - ' + arr[0] + ':' + obj.attr('title'));
		})
	}
}
function del_error_qty_number(obj) {
    var qty = obj.value;
    var reg = /\d/;
    for (var i = 0; i < qty.length; i++) {
        if (!reg.test(qty.charAt(i))) {
            qty = qty.substr(0, i);
        }
    }
	qty = parseInt(qty) >= 1 ? qty : 1;
	obj.value = qty;
	$.get('/get_form.php', {'type':'total', 'qty':qty, 'price':$('input[name=item_base_price]').val()}, function(data){
		$('#total').html(data);									
	})
}
function subscribe() {
	var email = $('.emailUpd').val();
	var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (email.length > 0 && reg.test(email)) {
		var url = '/subscribe.php';
		$.get(url, {"email":encodeURIComponent(email)},
			function(data){
				alert(data);
			}
		);
	} else {
		alert('Please enter a Validated email.');
		$('#newsletter > input:first').focus();
	}
}

function change_currency(currency, currency_flag, code) {
	$('#span_' + code + '_currency').bind('mouseenter', 
		function(){
			$('#'+code + '_currencies').show().css({'left':$(this).offset().left - 1, 'top':$(this).offset().top - 1}).bind('mouseleave', function(){$('#'+code + '_currencies').hide()});
	});
	
	if (currency) {
		setCookie('salebatt_currency', currency, 10 * 365 * 24 * 3600 * 1000, '/', null, false);
		eval($('#cart_form').attr('action'));
		eval($('#cart_' + code + '_form').attr('action'));
	}
}

function setCookie(name,value,time,path,domain,secure){
	var exp = new Date();
	exp.setTime(exp.getTime() + parseInt(time));
	var expiryString = time ? ";expires=" + exp.toGMTString() : '';
	var pathString = (path == null) ? "" : " ;path = "+ path;
	var domainString = (domain == null) ? "" : " ;domain = "+ domain;
	var secureString = (secure) ? ";secure" : "";
	document.cookie = name + "=" + escape(value) + expiryString + pathString + domainString + secureString;
}

function getCookie (name) {
	var CookieFound = false;
	var start = 0;
	var end = 0;
	var CookieString = document.cookie;
	var i = 0;

	while (i <= CookieString.length) {
		start = i ;
		end = start + name.length;
		if (CookieString.substring(start, end) == name){
			CookieFound = true;
			break;
		}
		i++;
	}

	if (CookieFound){
		start = end + 1;
		end = CookieString.indexOf(";",start);
		if (end < start) end = CookieString.length;
		return unescape(CookieString.substring(start, end));
	}
	return "";
}
function update_top_cart_info() {
	var url = '/cart/cart_info.php';
	$.get(url,
		function(data){
			var temp_arr = data.split('|');
			$('#num-items').html(temp_arr[0]);
			$('#cart-total').html(temp_arr[1]);
		}
	);
}
