/**
 * 在线询盘
 * @param name 默认hs_basket_product
 * @param check 是否验证
 * @param basketSize 询盘篮数量
*/
function inquiryNow(name, check, basketSize) {
	if(name == null){
		name = "hs_basket_product";
	}

	if(check && document.inquiryForm && !Util.isChecked(name)){
		if(!basketSize || basketSize <= 0){
			alert("No item(s) selected");
			return;
		}
	}

	document.inquiryForm.action="/inquiry/";
	document.inquiryForm.submit();
}

/**
 * 添加到询盘篮
 * @param name 默认hs_basket_product
 * @param check 是否验证
*/
function addBasket(name, check) {
	if(name == null){
		name = "hs_basket_product";
	}
	
	if (check && document.inquiryForm && !Util.isChecked(name)) {
		alert("No item(s) selected");
		return;
	}

	document.inquiryForm.action="/inquiry_basket_add.htm";
	document.inquiryForm.submit();
}
			
/**
 * 验证Administrator Login表单
 * @param form 表单对象
 */
function validateCASLoginForm(form) {
	var email = form.email;
	var password = form.password;
	var buffer = new Array();
	var fieldName = "";
	if (email.value == "") {
		buffer.push("Please enter your Member ID or Email Address.");
		if (fieldName == "") {
			fieldName = email.name;
		}
	}
	if (password.value == "") {
		buffer.push("Please enter your password.");
		if (fieldName == "") {
			fieldName = password.name;
		}
	}
	if (buffer.length > 0) {
		alert(buffer.join("\n\n"));
		form.elements[fieldName].focus();
		return false;
	}
	return true;
}

function changeTqBox(tq_chat_flag, addCookie){
	if(tq_chat_flag == "min"){
		$(".chatBoxMain").hide();
		$(".chatBoxSmall").show();
	}else{
		$(".chatBoxSmall").hide();
		$(".chatBoxMain").show();
		tq_chat_flag = "max";
	}
	
	if(addCookie){
		// $.get("set_tq_chat_flag.htm",{"hs_indie_chat_flag": tq_chat_flag});
		Util.setCookie("hs_indie_chat_flag", tq_chat_flag, 365*24*60);
	}
}

function translator(pattern) {
	/// Configuration parameters //////////////
	var open_in_same_window = 0;
	//////////// End Of Configuration /////////////
	var my_location = unescape(document.location.toString());
	var new_location = "";
	var new_pattern = "";
	if (my_location.indexOf("translate_c?") != -1) {
		/// From google...
		var indexof_u = my_location.indexOf("u=");
		if (indexof_u == -1) {
			new_location = document.location;
		} else {
			var subs = my_location.substring(indexof_u, my_location.length);
			var ss = subs.split("&");
			new_location = ss[0].substring(2, ss[0].length);
		}
	} else {
		new_location = document.location;
	}
	indexof_p = pattern.indexOf("|");
	var isen = "";
	if (indexof_p == -1) {
		indexof_p1 = pattern.indexOf("><");
		if (indexof_p1 == -1) {
			new_pattern = pattern;
			if (pattern == "en") {
				isen = 1;
			}
		} else {
			var psplit = pattern.split("><");
			new_pattern = psplit[0] + "|" + psplit[1];
			if (psplit[1] == "en") {
				isen = 1;
			}
		}
	} else {
		var psplit = pattern.split("|");
		new_pattern = psplit[0] + "|" + psplit[1];
		if (psplit[1] == "en") {
			isen = 1;
		}
	}
	var thisurl = "";
	if (isen == 1) {
		thisurl = new_location;
	} else {
		thisurl = "http://translate.google.com/translate_c?langpair=" + new_pattern + "&u=" + new_location;
	}
	if (open_in_same_window == 1) {
		window.location.href = thisurl;
	} else {
		msgWindow = window.open("", "subwindow", "toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes,left=0,top=0");
		msgWindow.focus();
		msgWindow.location.href = thisurl;
	}
}
/**
* 统计字符个数
*/
function textLimitCheck(thisArea, showArea, maxLength) {
	if (thisArea.value.length > maxLength) {
		alert(maxLength + " characters limit. \r Excessive data will be truncated.");
		thisArea.value = thisArea.value.substring(0, maxLength - 1);
		thisArea.focus();
	}
	showArea.value = thisArea.value.length;
}
function setTipShow(flag) {
	var value = "";
	if (flag == 0) {	// 小窗口
		value = "min";
		$(".chatBoxMain").hide();
		$(".chatBoxSmall").show();
		$(".chatBoxSmall").floating({targetX:"right", targetY:"bottom"});
	} else {	// 大窗口
		value = "max";
		$(".chatBoxSmall").hide();
		$(".chatBoxMain").show();
		$(".chatBoxMain").floating({targetX:"right", targetY:"bottom"});
	}
	$.get("set_tq_chart_flag.htm",{"hs_indie_chat_flag":value});
}

function showTqBox(tq_chart_flag){
	if (tq_chart_flag.indexOf("min") != -1) {	// 小窗口
		$(".chatBoxMain").hide();
		$(".chatBoxSmall").show();
		$(".chatBoxSmall").floating({targetX:"right", targetY:"bottom"});
	} else {	// 大窗口
		$(".chatBoxSmall").hide();
		$(".chatBoxMain").show();
		$(".chatBoxMain").floating({targetX:"right", targetY:"bottom"});
	}
}


/**
 * 调节图片的长度和宽度做限制，自定义高宽，解决ie6的140x140高出2个像素的问题
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * 例：<img src="image.jpg" onload="setImgWH(this,imgW,imgH) >
 * @param img img object
 * @param imgW 限制宽度
 * @param imgH 限制高度
 */
var Indie = new Object();
Indie.setImgWH = function (img, imgW, imgH) {
	if($.browser.msie&&(/MSIE 6.0/.test(navigator.userAgent) || /MSIE 7.0/.test(navigator.userAgent))){
		imgW=imgW-2;
		imgH=imgH-8;
	}
	var imgObj = new Image();
	imgObj.onload = function (){
		if (imgObj.width != 0 && imgObj.height != 0) {
			if (imgObj.width >= imgW || imgObj.height >= imgH) {
				var iHeight = imgObj.height * imgW / imgObj.width;
				if (iHeight <= imgH) {
					img.width = imgW;
					img.height = iHeight;
				} else {
					var iWidth = imgObj.width * imgH / imgObj.height;
					img.width = iWidth;
					img.height = imgH;
				}
			} else {
			}
		} else {
			img.width = imgW;
			img.height = imgH;
		}
	}
    imgObj.src = img.src;
};

var xOffset = 8;
var yOffset = 8;
var timer;
function featureProduct_show(){
	var self_top = $(this).position().top;
	var self_left =$(this).position().left;
	var _id = "_"+this.id;
	$("#"+_id).css("top",self_top-yOffset+"px");
	$("#"+_id).css("left",self_left-xOffset+"px");
	timer=setTimeout(function(){$("#"+_id).show()},100);
}
function featureProduct_hidden(){
	clearInterval(timer);
	var _id = "_"+this.id;
	$("#"+_id).hide();
}
function loadInquiryValidateCode() {
	var key =  new Date().getTime() + Math.random();
	$("#validateCodeKey").val(key);
	$("#inquiryValidateCodeImg").attr("src","/validateCode/getImage?hi_vc_key="+key);
}
$(function (){
	$("#loginWindow").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 180,
		width: 360,
		modal: true
	});
	loadInquiryValidateCode();
				
	//图片预加载处理
	$("img.imgLoadError").error(function(){
		$(this).attr("src", "/images/no_photo.gif");
	});

	$("#loginLink").click(function () {
		$("#loginWindow").dialog('option', 'title', 'Member Account');
		$("#loginWindow").dialog('open');
	});
	$("a").focus(function(){this.blur();});
    $(".mian-menu ul li").hover(function() {
    	$(this).children("ul").show();},
        function(){$(this).children("ul").hide();});
        
	$.fn.validateForm = function (options) {
		var settings = {
			success: function(label) {
				label.text("OK").addClass("success");
			}
		};
		$.extend(settings, options);
		this.validate(settings);
	};
	$(".photo").forbidden();
	$(".image").forbidden();
	$("#featureProduct > ul > li").hover(featureProduct_show,featureProduct_hidden);
	
	$("#inquiryForm").validateForm({
		submitHandler: function(form) {
		   	$("#inquiryFormSubmit").attr("disabled","disabled");
		   	form.submit();
		},
		rules: {
			subject: {required:true,rangelength:[5,120]},
			content: {required:true, rangelength:[20,3000]},
			fromCompany: {required:true,rangelength:[3,120]},
			fromName: {required:true,rangelength:[2,30]},
			fromEmail: {required:true,email:true}
		},
		messages: {
			subject: 'Please enter the subject. The subject must be between 5~120 characters.',
			content: 'Please enter the content. The content must be between 20~3000 characters.',
			fromCompany: 'Your company name must be between 3~120 characters.',
			fromName: 'Your name must be between 2~30 characters.',
			fromEmail: 'Please enter a valid email address.'
		}
	});
	
	$("#comNameHref").click(function(){
		$("#inquiry-list").toggle();
		
		var imgPath = $("#comNameImg").attr("src");
		if(imgPath.indexOf("open") != -1){
			$("#comNameImg").attr("src", imgPath.replace("open", "close"))
		}else{
			$("#comNameImg").attr("src", imgPath.replace("close", "open"))
		}
	});
	$("#uploadSwitch").click(function(){
		if(document.getElementById("uploadSwitch").checked){
			$("#uploadBar").show();
		}else{
			$("#uploadBar").hide();
		}
	});
	$(".chatBoxSmall").floating({targetX:"right", targetY:"bottom"});
	$(".chatBoxMain").floating({targetX:"right", targetY:"bottom"});
	
    $(".mian-menu ul li").hover(function() {
        $(this).children("ul").show();
    },
    function() {
        $(this).children("ul").hide();
    });
	/* style_22 Theme Banner */
	$(".c-aad9e3 .search label,.c-cbe3aa .search label,.c-9dd4ea .search label,.c-eae29d .search label,.c-dcd3a6 .search label,.c-f4ebbe .search label").before("<br />");
	/* style_36 Theme Banner */
	$("body.c-0d2e75").parent().addClass("c-0d2e75");
	$("body.c-047f8b").parent().addClass("c-047f8b");
	$("body.c-222222").parent().addClass("c-222222");
	$("body.c-d14c07").parent().addClass("c-d14c07");
});

