/**
 * common.js Document 
 *
 * @author	DonYue Team
 * @version	$Revision: 0.01 $
 * @copyright	Copyright (C) 2006-2010 DonYue Team
 * @package	DonYue
 */

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = (userAgent.indexOf('opera') != -1);
var is_saf = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv = (userAgent.indexOf('webtv') != -1);
var is_ie = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4 = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz = ((userAgent.indexOf('gecko')!=-1) && (!is_saf));
var is_kon = (userAgent.indexOf('konqueror') != -1);
var is_ns = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4 = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac = (userAgent.indexOf('mac') != -1);

var xml_http_building_link = '请等待，正在建立连接...';
var xml_http_sending = '请等待，正在发送数据...';
var xml_http_loading = '请等待，正在接受数据...';
var xml_http_load_failed = '通信失败，请刷新重新尝试！';
var xml_http_data_in_processed = '通信成功，数据正在处理中...';

var date_str="";
var val_fix=-1;
var val_count=0;
var search_type="show_all";
var menuarray=new Array();
menuarray['obj']=new Array();
menuarray['id']=new Array();
menuarray['timer']=new Array();

function $(id){
	return document.getElementById(id);
}
function $1(name){
	return document.getElementsByName(name);
}
function in_array(needle, haystack){
	if(typeof needle == 'string'){
		for(var i in haystack){
			if(haystack[i] == needle){
				return true;
			}
		}
	}
	return false;
}
function isdefbyid(id){
	if($(id) != null && typeof $(id) !='undefined')return true;
	else return false;
}

function arraypush(a, value){
	a[a.length] = value;
	return a.length;
}
function findtags(parentobj, tag){
	if(typeof parentobj.getElementsByTagName != 'undefined'){return parentobj.getElementsByTagName(tag);}
	else if(parentobj.all && parentobj.all.tags){return parentobj.all.tags(tag);}
	else {return null;}
}
function checkFocus(){
	var obj = (typeof wysiwyg == 'undefined' || !wysiwyg || !is_ie) ? $('postform').message : editwin;
	if(!obj.hasfocus){
		obj.focus();
	}
}
function isUndefined(variable){
	return typeof variable == 'undefined' ? true : false;
}
function trim(str){
	return (str.replace(/(\s+)$/g, '')).replace(/^\s+/g, '');
}
function sprintf(){
	if(ret=arguments[0]){
		var reg=/%(-)?(\d+)?\.?(\d+)?(\w)/g;
		var i=0,l=0;
		while(arr=reg.exec(ret)){
			if(++i>arguments.length)return null;
			switch(arr[4]){
				case "x":tmp=arguments[i].toString(16);break;
				case "f":if(arr[3]){tmp=arguments[i].toFixed(arr[3]);break;}
				case "s":
				case "d":
				default:tmp=arguments[i].toString();
			}
			if((l=Number(arr[2])-tmp.length)>0)
				tmp=arr[1]?tmp+new Array(l+1).join(" "):new Array(l+1).join("   ")+tmp;
			ret=ret.replace(arr[0],tmp);
		}
		return ret;
	}
	return null;
}
function storeCaret(textEl){
	if(textEl.createTextRange){
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}
function mb_strlen(str){
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
function MouseOver(obj){
	$(obj).style.cursor = is_ie ? 'hand' : 'pointer';
}
function arraypop(a){
	if(typeof a != 'object' || !a.length){
		return null;
	}else{
		var response = a[a.length - 1];
		a.length--;
		return response;
	}
}
function ajaxGet(url, type, id, fun){
	var x = new Ajax(type, id);
	x.get(url, function(s){eval(fun);});
}
function ajaxPost(url, type, id, postv, fun){
	var x = new Ajax(type, id);
	x.post(url, postv, function(s){eval(fun);});
}
function changeCSS01(objid){
	for(i=1;i<=4;i++){
		$("indexdisplay"+i).style.display="none";
	}
	$("index"+objid).style.display="";
}
function changeCSS02(objid){
	for(i=1;i<=4;i++){
		if(isdefbyid("txt_description_showTab"+i)) $("txt_description_showTab"+i).style.display="none";
		$("description_showTab"+i).className="nocurrent_tab";
	}
	$("txt_"+objid).style.display="";
	$(objid).className="current_tab";
}
function search_blur(obj){
	if(obj.value==""){
		obj.value="Key words";
	}
}
var hasRegEve=false;
function serach_focus(obj){
	if(obj.value=="Key words"){
		obj.value="";
	}else{
		obj.select();
	}
	if(!hasRegEve){
		if(document.addEventListener){
			document.addEventListener("click", hideMenu, false);
		}else if(document.attachEvent){
			document.attachEvent("onclick", hideMenu);
		}
	}
}
function hideMenu(){
	var obj=$('search_suggest');
	obj.innerHTML = '';
	obj.style.display="none";
}
function searchSuggest(){
	value=$('txtSearch').value;
	if(date_str==value)return;
	date_str=value;
	url='searchsuggest.php?search='+escape(value);
	ajaxGet(url,"HTML","","handleSearchSuggest(trim(s));");	
}
function searchKeyVal(v){
	if(v==13 && val_fix>=0 && val_fix<val_count){
		setSearch($("sugest_"+val_fix).innerHTML);
	}
	if(v!=38 && v!=40)return;
	if(v==38 && val_fix>0){
		val_fix--;
	}else if(v==40 && val_fix<val_count-1){
		val_fix++;
	}
	if(val_fix>=0 && val_fix<val_count)
	suggestOver($("sugest_"+val_fix));
}
function handleSearchSuggest(val){
	var obj = $('search_suggest');
	obj.innerHTML = '';
	var str = val.split("\n");
	val_count=str.length - 1;
	val_fix=-1;
	for(i=0; i < val_count; i++){
		var suggest = '<div id="sugest_'+i+'" tabindex="'+i+'" onmouseover="suggestOver(this)" onmouseout="suggestOut(this)" onclick="setSearch(this.innerHTML)" class="suggest_link">' + str[i] + '</div>';
		obj.innerHTML += suggest;
	}
	obj.style.display="";
}
function setSearch(value){
	hideMenu();
	value=value.replace('<b>','');
	value=value.replace('</b>','');
	value=value.replace('<B>','');
	value=value.replace('</B>','');
	$('txtSearch').value = trim(value);
	$('frmSearch').submit();
}
function suggestOver(o){
	for(i=0;i<val_count;i++){
		suggestOut($("sugest_"+i));
	}
	o.className = 'suggest_link_over';
	val_fix=o.getAttribute("tabindex");
}
function suggestOut(o){
	o.className = 'suggest_link';
}
function del_product(id){
	var obj=$("cart_quantity");
	obj.action += "&cart_delete[]="+id;
	obj.submit();
}
function html_out_login(obj,msg,boolvalue){
	if(boolvalue==0){
		$(obj+"_span").innerHTML=msg;
		$(obj).style.border="1px solid #FF0000";
	}else{
		$(obj+"_span").innerHTML=msg;
		$(obj).style.border="1px solid #A0A0A0";
	}
}
function my_input_check(obj){
	obj.value=trim(obj.value);
	switch(obj.id){
		case "firstname":
		case "lastname":
		case "fullname":
		case "s_firstname":
		case "s_lastname":
		case "b_firstname":
		case "b_lastname":
		case "name":
			check_name(obj);
			break;
		case "password":
		case "login_password":
		case "password_current":
		case "password_new":
			check_password(obj);
			break;
		case "confirmation":
		case "password_confirmation":
			check_confirmation(obj,$("password"));
			break;
		case "login_email_address":
		case "email_address_1":
		case "email_address_2":
		case "email":
			check_email(obj);
			break;
		case "street_address":
		case "s_street":
		case "b_street":
			check_street(obj);
			break;
		case "city":
		case "s_city":
		case "b_city":
			check_city(obj);
			break;
		case "country":
		case "s_country":
		case "b_country":
			check_country(obj);
			break;
		case "postcode":
		case "s_postcode":
		case "b_postcode":
			check_postcode(obj);
			break;
		case "telephone":
		case "phone":
		case "s_phone":
		case "b_phone":
			check_phone(obj);
			break;
		case "product_model":
			if(obj.value.length==0)
				html_out_login(obj.id,'<img src="images/error.gif" /><br />Product model cannot be empty',0);		
			else
				html_out_login(obj.id,'<img src="images/ok.gif" />',1);
			break;
		case "product_quantity":
			check_num(obj);
			break;
		case "enquiry":
			check_enquiry(obj);
			break;
		case "subject":
			check_subject(obj);
			break;
		case "antispam_contact":
			check_antispam_contact(obj);
			break;
	}
}
function check_antispam_contact(obj){
	value=trim(obj.value);
	if(value==""){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Antispam contact cannot be empty',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}
	return false;
}
function check_enquiry(obj){
	value=trim(obj.value);
	if(value==""){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Enquiry cannot be empty',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}
	return false;
}
function check_subject(obj){
	value=trim(obj.value);
	if(value==""){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Subject cannot be empty',0);
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}
	return false;
}
function check_postcode(obj){
	value=trim(obj.value);
	if(value==""){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Postcode cannot be empty',0);
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}
	return false;
}
function check_country(obj){
	value=trim(obj.value);
	if(value==""){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Please select the country',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;	
	}
	return false;
}
function check_city(obj){
	value=trim(obj.value);
	if(value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />City cannot be empty',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;	
	}
	return false;
}
function check_street(obj){
	value=trim(obj.value);
	if(value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Street cannot be empty',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;	
	}
	return false;
}
function check_name(obj){
	value=trim(obj.value);
	if(value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Please enter your name',0);							
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;	
	}
	return false;
}
function check_email(obj){
	value=trim(obj.value);
	if(value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Email address cannot be empty',0);
	}else{
		if(value.match(/^([a-zA-Z0-9_\-|\.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/i)!=null){
			html_out_login(obj.id,'<img src="images/ok.gif" />',1);
			return true;
		}else{
			html_out_login(obj.id,'<img src="images/error.gif" /><br />Invalid email address',0);	
		}
	}
	return false;
}
function is_tel_number(str){
	if(/(^[+\-0-9#,*()\s]+$)/.test(str))
		return true;
	return false;
}
function check_phone(obj){
	value=obj.value;
	if(value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Please enter your telephone number',0);		
	}else{
		if(is_tel_number(obj.value)){
			html_out_login(obj.id,'<img src="images/ok.gif" />',1);
			return true;
		}else{
			html_out_login(obj.id,'<img src="images/error.gif" /><br />Invalid telephone number',0);	
		}
	}
	return false;
}
function check_password(obj){
	if(obj.value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Please enter your password',0);		
	}else{
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}
	return false;
}
function check_confirmation(obj,p_obj){
	if(obj.value.length==0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Retyped password cannot be empty',0);		
	}else{
		if(obj.value==p_obj.value){
			html_out_login(obj.id,'<img src="images/ok.gif" />',1);
			return true;
		}else
			html_out_login(obj.id,'<img src="images/error.gif" /><br />Two entered passwords do not match',0);
	}
	return false;
}
function check_user_agreement(obj){
	if(obj.checked){
		html_out_login(obj.id,'<img src="images/ok.gif" />',1);
		return true;
	}else{
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Please agree to our Terms and Conditions',0);
	}
	return false;
}
function check_num(obj){
	value=trim(obj.value);
	if(value.length<=0){
		html_out_login(obj.id,'<img src="images/error.gif" /><br />Product quantity cannot be empty',0);
	}else{						
		if(value.match(/^[0-9]+/i)!=null){
			html_out_login(obj.id,'<img src="images/ok.gif" />',1); 
			return true;
		}else{
			html_out_login(obj.id,'<img src="images/error.gif" /><br />Product quantity must be number',0);
		}
	}
	return false;
}
function contact_us_check(){
	var result = check_name($("name"));
	result = check_email($("email")) && result;
	result = check_subject($("subject")) && result;
	result = check_enquiry($("enquiry")) && result;
	result = check_antispam_contact($("antispam_contact")) && result;
	return result;
}
function address_book_process_check(){
	var result = check_name($("firstname"));
	result = check_name($("lastname")) && result;
	result = check_street($("street_address")) && result;
	result = check_city($("city")) && result;
	result = check_country($("country")) && result;
	result = check_postcode($("postcode")) && result;
	result = check_phone($("phone")) && result;
	return result;
}
function onepage_submit_check(){
	var result = check_name($("s_firstname"));
	result = check_name($("s_lastname")) && result;
	result = check_street($("s_street")) && result;
	result = check_city($("s_city")) && result;
	result = check_country($("s_country")) && result;
	result = check_postcode($("s_postcode")) && result;
	result = check_phone($("s_phone")) && result;
	if(!$("same2sa").checked){
		result = check_name($("b_firstname")) && result;
		result = check_name($("b_lastname")) && result;
		result = check_street($("b_street")) && result;
		result = check_city($("b_city")) && result;
		result = check_country($("b_country")) && result;
		result = check_postcode($("b_postcode")) && result;
		result = check_phone($("b_phone")) && result;
	}
	obj=$1("shipping");v=false;
	for(i=0;i<obj.length;i++)if(obj[i].checked){v=true;break};
	if(!v){
		$("shipping_alert").style.display="";
		$("shipping_div").style.border="1px solid #ff0000";
	}
	result=result && v;
	obj=$1("payment");v=false;
	for(i=0;i<obj.length;i++)if(obj[i].checked){v=true;break};
	if(!v){
		$("payment_alert").style.display="";
		$("payment_div").style.border="1px solid #ff0000";
	}
	result=result && v;
	if(!result)window.scroll(0,-360);
	return result;
}
function login_submit_check(type){
	var result = check_email($("login_email_address"));
	result = check_password($("login_password")) && result;
	return result;
}
function create_account_check(){
	var result = check_email($("email_address_1"));
	result = check_password($("password")) && result;
	result = check_confirmation($("confirmation"), $("password")) && result;
	//result = check_user_agreement($("user_agreement")) && result;
	return result;
}
function checkout_guest_check(){
	return result = check_email($("email_address_2"));
}
function account_update_check(){
	var result = check_name($("firstname"));
	result = check_name($("lastname")) && result;
	result = check_email($("email_address")) && result;
	result = check_phone($("telephone")) && result;
	return result;
}
function account_passwd_check(){
	var result = check_password($("password_current"));
	result = check_password($("password_new")) && result;
	result = check_confirmation($("password_confirmation"),$("password_new")) && result;
	return result;
}
function mail_submit_check(type){
	var err=true;
	err=check_email($("email"));
	err=check_name($("fullname")) && err;
	err=check_enquiry($("enquiry")) && err;
	err=check_num($("product_quantity")) && err;
	
	if(err){
		postv="products_id="+$("products_id").value+"&fullname="+$("fullname").value+"&email="+$("email").value+"&phone="+$("phone").value+"&product_quantity="+$("product_quantity").value+"&enquiry="+$("enquiry").value;
		ajaxPost("ajax_page.php?action=postmail", "XML", "", postv, "postMailResult(trim(s));");
	}
}
function postMailResult(val){
	if(val=="OK"){
		$("dy_dialog_content").style.display = "none";
		$("dy_dialog_result").style.display = "block";
		setTimeout("closeLayer()",20000);
	}else{
		alert(val);
	}
}
function getPageScroll(){
	var yScroll;
	if(self.pageYOffset){
		yScroll = self.pageYOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	}else if(document.body){// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('', yScroll);
	return arrayPageScroll;
}
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY){
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}else if(document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}else{
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if(self.innerHeight){
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if(document.body){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	}else{
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	}else{
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}
showDialog=function(){
	var width=595;
	var height=474;
	showDialog = function(navi){
		var dy_dialog=$("dy_dialog");
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		dy_dialog.style.top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2) + 'px';
		dy_dialog.style.left = ((arrayPageSize[0] - width) / 2) + 'px';
		init();
		dy_dialog.style.display="block";
		$("dy_dialog_content").style.display = "block";
		$("dy_dialog_result").style.display = "none";
		$("bodybg").style.display = "block";
	};
	init = function(){
		fullname=$("fullname");
		email=$("email");
		phone=$("phone");
		product_quantity=$("product_quantity");
		enquiry=$("enquiry");
		fullname.value="";
		email.value="";
		phone.value="";
		product_quantity.value="";
		enquiry.value="";
		fullname.style.border="1px solid #cccccc";
		email.style.border="1px solid #cccccc";
		phone.style.border="1px solid #cccccc";
		product_quantity.style.border="1px solid #cccccc";
		enquiry.style.border="1px solid #cccccc";
		$("fullname_span").innerHTML="&nbsp;*&nbsp;";
		$("email_span").innerHTML="&nbsp;*&nbsp;";
		$("phone_span").innerHTML="&nbsp;";
		$("product_quantity_span").innerHTML="&nbsp;*&nbsp;";
		$("enquiry_span").innerHTML="&nbsp;*&nbsp;";
	}
	closeLayer = function(){
		$("dy_dialog").style.display="none";
		$("bodybg").style.display = "none";
	};
	drag=function(a){
		var o=$("dy_dialog");
		var d=document;
		if(!a)a=window.event;
		var X=a.pageX?a.pageX:a.clientX;
		var Y=a.pageY?a.pageY:a.clientY;
		if(o.setCapture)
			o.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		var backData = {x : (X-parseInt(o.style.left)), y : (Y-parseInt(o.style.top))};
		d.onmousemove=function(a){
			if(!a)a=window.event;
			X=a.pageX?a.pageX:a.clientX;
			Y=a.pageY?a.pageY:a.clientY;
			o.style.left=(X-backData.x)+"px";
			o.style.top=(Y-backData.y)+"px";
		};
		d.onmouseup=function(a){
			if(!a)a=window.event;
			if(o.releaseCapture)
				o.releaseCapture();
			else if(window.captureEvents)
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			d.onmousemove=null;
			d.onmouseup=null;
			X=a.pageX?a.pageX:a.clientX;
			Y=a.pageY?a.pageY:a.clientY;
			if(!document.body.pageWidth)document.body.pageWidth=document.body.clientWidth;
			if(!document.body.pageHeight)document.body.pageHeight=document.body.clientHeight;
			if(X < 1 || Y < 1 || X > document.body.pageWidth || Y > document.body.pageHeight){
				o.style.left=(X-backData.x)+"px";
				o.style.top=(Y-backData.y)+"px";
			}
		};
	};
	showDialog();
}
function popupWindow(url){
  window.open(url,'popupWindow','toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=700,height=850,screenX=150,screenY=100,left=100')
}
ImageLoader=function(id,url){
	//this.url=url;
	//this.id=id;
	addListener=function(element,type,expression,bubbling){
		bubbling=bubbling||false;
		if(window.addEventListener){
			element.addEventListener(type,expression,bubbling);
			return true;
		}else if(window.attachEvent){
			element.attachEvent('on'+type,expression);
			return true;
		}else{
			return false;
		}
	};
	ImageLoader=function(id,url){
		var image=document.createElement('img');
		//var url=this.url;
		//var id=this.id;
		addListener(image,'load',function(e){
			$(id).src = url;
		},false);
		image.src=url;
	};
	ImageLoader(id,url);
};
//ImageLoader("cell_img","http://travel.dahangzhou.com/fengjing/291/pics/20076191116512188.jpg");
