var share = new Object();

share.subMenuStart = function (currObject, menuId, menuDiv, menuX, menuY, menuWidth) {
	if (this.timerId) clearTimeout(this.timerId);

	this.currObject = currObject;
	this.menuId = menuId;
	this.menuDiv = menuDiv;
	this.menuX = menuX;
	this.menuY = menuY;
	this.menuWidth = menuWidth;

	var pos = XMLHttp.getObjectPos(this.currObject);
	var menuObject = document.getElementById(this.menuDiv);
	this.menuObject = menuObject;
	menuObject.style.display = "block";
	menuObject.style.left = pos[0]+"px";
	if (this.menuX) menuObject.style.left = parseInt(pos[0]+this.menuX)+"px";
	if (this.menuY) menuObject.style.top = parseInt(pos[1]+this.menuY)+"px";

	menuHTML = "";
	if (this.menuAr[this.menuId]) {
		menuHTML += "<table bgcolor='#FFFFFF' width='"+menuWidth+"' onMouseOver=\"share.subMenuShow()\" onMouseOut=\"share.subMenuStop()\" border='0' cellpadding='5' cellspacing='1'>\n";
		for(menuKey in this.menuAr[this.menuId]) {
			menuHTML += "<tr>\n";
			menuHTML += "<td width='"+menuWidth+"' height='20' onClick=\"location.href='"+this.languagePath+"-"+menuKey+".html'\" style='cursor:pointer' class='submenu_bg' onMouseOver=\"this.className='submenu_bg_over'\" onMouseOut=\"this.className='submenu_bg'\">\n";
			menuHTML += this.menuAr[this.menuId][menuKey]+"\n";
			menuHTML += "</td>\n";
			menuHTML += "</tr>\n";
		}
		menuHTML += "</table>\n";
	}

	menuObject.innerHTML = menuHTML;
};

share.subMenuStop = function () {
	if (this.timerId) clearTimeout(this.timerId);
	this.timerId = setTimeout("share.subMenuHide()", 1000);
};

share.subMenuShow = function () {
	if (this.timerId) clearTimeout(this.timerId);
	if (this.menuObject) this.menuObject.style.display = "block";
};

share.subMenuHide = function () {
	if (this.menuObject) this.menuObject.style.display = "none";
};


share.newsDetails = function(school, pageName, id) {
	windowControl.windowOpen('divNews', 'index.php?igo=details&school='+school+'&page_name='+pageName+'&edid='+id+'&pmod=xml', '', [document.body.scrollLeft, document.body.scrollTop], '');
};

share.newsPhoto = function(school, pageName, id, photoI) {
	windowControl.windowOpen('divPhoto', 'index.php?igo=photo&school='+school+'&page_name='+pageName+'&data_id='+id+'&photo_i='+photoI+'&pmod=xml', '', [document.body.scrollLeft, document.body.scrollTop], '');
};

share.facilitiesDetails = function(school, pageName, id) {
	windowControl.windowOpen('divFacilities', 'index.php?igo=details&school='+school+'&page_name='+pageName+'&edid='+id+'&pmod=xml', '', [document.body.scrollLeft, document.body.scrollTop], '');
};

share.photoOpen = function (school, pageName, albumId, photoId) {
	if (share.timer) clearTimeout(share.timer);
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	windowControl.windowOpen('photo', 'index.php?igo=photo_info&school='+school+'&page_name='+pageName+'&pmod=xml&album_id='+albumId+'&edid='+photoId, '', [document.body.scrollLeft, document.body.scrollTop+topadd], '');
};

share.photoSlide = function (school, pageName, albumId, photoId, slideTimer) {
	if (share.timer) clearTimeout(share.timer);
	this.albumId = albumId;
	this.photoId = photoId;
	this.slideTimer = slideTimer;
	this.school = school;
	this.pageName = pageName;
	XMLHttp.sendReq('POST', 'index.php?igo=photo_slide&school='+school+'&page_name='+pageName+'&pmod=xml&album_id='+albumId+'&edid='+photoId, '', share.photoSlideReturn, 'showSlide', 'send');
};

share.photoSlideReturn = function (returnString) {
	eval(returnString);
	if (share.photoId) {
		for (i=0;i<share.slideAr.length;i++) {
			if (share.slideAr[i]['id'] == share.photoId) {
				share.slideNum = i;
				break;
			}
		}
	} else {
		share.slideNum = 0;
	}
	share.photoSlideInit();
};

share.photoSlideInit = function () {
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	this.slideTimer = 4;
	windowControl.windowOpen('photo', 'index.php?igo=photo_slide_start&school='+this.school+'&page_name='+this.pageName+'&pmod=xml', '', [document.body.scrollLeft, document.body.scrollTop+topadd], share.photoSlideInitReturn);
};

share.photoSlideInitReturn = function (returnString) {
	if (share.timer) clearTimeout(share.timer);
	share.timer = setTimeout("share.photoSlideStart()", 1000*this.slideTimer);
};

share.photoSlideStart = function (slideTimer) {
	if (share.timer) clearTimeout(share.timer);
	if (slideTimer) {
		this.slideTimer = slideTimer;
	}
	share.timer = setTimeout("share.photoSlideStart()", 1000*this.slideTimer);
	slideImage.innerHTML = "<img src='"+share.slideAr[share.slideNum]['path']+"' border='0' />";
	content = "";
	if (share.slideAr[share.slideNum]['name']) content += "<font class='album_info_font'>"+share.slideAr[share.slideNum]['name']+"</font><br>";
	if (share.slideAr[share.slideNum]['desc']) content += "<font class='album_name_font'>"+share.slideAr[share.slideNum]['desc']+"</font><br>";
	if (share.slideAr[share.slideNum]['date']) content += "<font class='album_info_font'>"+share.slideAr[share.slideNum]['date']+"</font><br>";
	slideContent.innerHTML = content;
	share.slideNum++;
	if (share.slideNum >= share.slideAr.length) share.slideNum = 0;
	if (document.images) {
		preloadImage = new Image(); 
		preloadImage.src=share.slideAr[share.slideNum]['path']; 
	}
	buttonHTML = "";
	if (this.slideTimer != 6) {
		buttonHTML += "<img src=\"image/layout1/button_slow_a.gif\" onmouseover=\"this.src='image/layout1/button_slow_b.gif'\" onmouseout=\"this.src='image/layout1/button_slow_a.gif'\" border=\"0\" style=\"cursor:pointer\" onClick=\"share.photoSlideStart(6);\" />&nbsp;";
	} else {
		buttonHTML += "<img src=\"image/layout1/button_slow_b.gif\" border=\"0\" />&nbsp;";
	}
	if (this.slideTimer != 4) {
		buttonHTML += "<img src=\"image/layout1/button_normal_a.gif\" onmouseover=\"this.src='image/layout1/button_normal_b.gif'\" onmouseout=\"this.src='image/layout1/button_normal_a.gif'\" border=\"0\" style=\"cursor:pointer\" onClick=\"share.photoSlideStart(4);\" />&nbsp;";
	} else {
		buttonHTML += "<img src=\"image/layout1/button_normal_b.gif\" border=\"0\" />&nbsp;";
	}
	if (this.slideTimer != 2) {
		buttonHTML += "<img src=\"image/layout1/button_fast_a.gif\" onmouseover=\"this.src='image/layout1/button_fast_b.gif'\" onmouseout=\"this.src='image/layout1/button_fast_a.gif'\" border=\"0\" style=\"cursor:pointer\" onClick=\"share.photoSlideStart(2);\" />&nbsp;";
	} else {
		buttonHTML += "<img src=\"image/layout1/button_fast_b.gif\" border=\"0\" />&nbsp;";
	}
	buttonHTML += "<img src=\"image/layout1/button_pause_play_a.gif\" onmouseover=\"this.src='image/layout1/button_pause_play_b.gif'\" onmouseout=\"this.src='image/layout1/button_pause_play_a.gif'\" border=\"0\" style=\"cursor:pointer\" onClick=\"share.photoSlideStop();\" />";
	//slideButton.innerHTML = buttonHTML;
};

share.photoSlideStop = function () {
	if (share.timer) clearTimeout(share.timer);
	slideButton.innerHTML = "<img src=\"image/layout1/button_pause_play_b.gif\" border=\"0\" style=\"cursor:pointer\" onClick=\"share.photoSlideStart();\" />";
};

share.photoSlideClose = function () {
	if (share.timer) clearTimeout(share.timer);
	windowControl.windowClose('photo');
};

share.hsOpen = function (school, pageName, id, showDiv) {
	XMLHttp.sendReq('POST', 'index.php?igo=open&school='+school+'&page_name='+pageName+'&pmod=xml&edid='+id, '', '', showDiv, 'send');
};

share.hsClose = function (school, pageName, id, showDiv) {
	XMLHttp.sendReq('POST', 'index.php?igo=close&school='+school+'&page_name='+pageName+'&pmod=xml&edid='+id, '', '', showDiv, 'send');
};

share.showDonateForm = function (curr) {
	curr.style.display = "";
};

share.hideDonateForm = function (curr) {
	curr.style.display = "none";
};

share.changeStyle = function(curr) {
	if (curr) {
		if (share.focusField != curr) {
			curr.className = "reg_input_over";
			if (share.focusField) share.focusField.className = "reg_input";
		}
		share.focusField = curr;
	}
};

share.leftMenuOver = function(curr, objFont) {
	if (curr) {
		curr.background = "image/layout_hk/left_menu_r2_c2.jpg";
	}
	var leftFontItem = document.getElementById(objFont);
	if (leftFontItem) leftFontItem.className = "left_menu_over";
};

share.leftMenuOut = function(curr, objFont) {
	if (curr) {
		curr.background = "image/layout_hk/left_menu_r4_c2.jpg";
	}
	var leftFontItem = document.getElementById(objFont);
	if (leftFontItem) leftFontItem.className = "left_menu";
};

share.loginFocus = function(curr, defaultValue) {
	if (curr.value == defaultValue) {
		curr.value = "";
	}
};

share.openWindow = function (url, windowId) {
	if (!windowId) windowId = "popup";
	windowControl.windowOpen(windowId, url, '', [document.body.scrollLeft, document.body.scrollTop], '');
};

share.windowSendForm = function (url, formName) {
	XMLHttp.formValue(formName);
	XMLHttp.sendReq('POST', url, XMLHttp.postString, '', 'popup', 'popup');
	document.body.scrollTop = 200;
};

share.leftMenuOver = function (curr, target) {
	//var linkText = document.getElementById('left_menu_'+target);
	//linkText.className = "left_menu_over";
	curr.background = "image/layout_hk/page_menu_r2_c2.png";
};

share.leftMenuOut = function (curr, target) {
	//var linkText = document.getElementById('left_menu_'+target);
	//linkText.className = "left_menu";
	curr.background = "image/layout_hk/page_menu_r4_c2.png";
};

share.profilePhotoChange = function(act, languagePath, photoNumber) {
	if (act == "next") {
		if (share.profilePhotoCount >= share.profilePhotoList.length-3) {
			return;
		}
		share.profilePhotoCount = share.profilePhotoCount+photoNumber;
	}
	else if (act == "prev") {
		if (share.profilePhotoCount < 1) {
			return;
		}
		share.profilePhotoCount = share.profilePhotoCount-photoNumber;
	}
	photoCount = share.profilePhotoCount;
	
	for(i=0; i<photoNumber; i++) {
		var oldObject = document.getElementById('profilePhotoUrl'+i);
		if (oldObject) document.getElementById('profilePhoto'+i).removeChild(oldObject);
		if (!share.profilePhotoList[photoCount]) continue;
		
		var newUrl = document.createElement('a');
		newUrl.setAttribute('id', 'profilePhotoUrl'+i);
		newUrl.href = "javascript:share.openWindow('index.php?language_name="+languagePath+"&page_name=member&igo=view_photo&edid="+share.profilePhotoList[photoCount]['dataId']+"&photo_i="+photoCount+"&pmod=xml')";
		document.getElementById('profilePhoto'+i).appendChild(newUrl);
		var newImage = document.createElement('img');
		newImage.border = 0;
		newImage.src = share.profilePhotoList[photoCount]['filePath'];
		newUrl.appendChild(newImage);
		photoCount++;
	}
};

share.onlineDonate = function(formName) {
	XMLHttp.formValue(formName);
	XMLHttp.sendReq('POST', 'index.php?page_name=member&igo=donate_online_send&pmod=xml', XMLHttp.postString, share.onlineDonateReturn, 'donateMessage', 'donateMessage');
	document.body.scrollTop = 200;
};

share.onlineDonateReturn = function(returnString) {
	if (returnString) {
		if (returnString.indexOf('<!--') < 0) {
			return;
		}
		try {
			document.getElementById('donateForm').submit();
		} catch(error) {
		}
	}
};

share.donateSend = function (url, formName) {
	XMLHttp.formValue(formName);
	XMLHttp.sendReq('POST', url, XMLHttp.postString, share.onlineDonateReturn, 'popup', 'popup');
	document.body.scrollTop = 200;
};

share.profileDeletePhoto = function (formName, fieldName) {
	document.getElementById(formName+fieldName+"_div").innerHTML = "<input type='hidden' name='"+formName+fieldName+"_delete' value='1' />";
};

share.headerInit = function (photoI, timeout) {
	if (share.timerId) clearTimeout(share.timerId);
	if (photoI >= share.headerPhoto.length) photoI = 0;
	var oldImage = document.getElementById('showHeaderImage');
	if (oldImage) document.getElementById('showHeader').removeChild(oldImage);
	
	if (share.headerPhoto[photoI]) {
		var headerImage = "";
		if (share.headerPhoto[photoI]['url']) {
			headerImage += "<a href='"+share.headerPhoto[photoI]['url']+"' target='"+share.headerPhoto[photoI]['target']+"'>";
		}
		headerImage += "<img src='"+share.headerPhoto[photoI]['photo']+"' border='0' />";
		document.getElementById('showHeader').innerHTML = headerImage;
	}
	photoI++;
	if (share.headerPhoto.length > 1) share.timerId = setTimeout("share.headerInit("+photoI+", "+timeout+")", timeout * 1000);
};

share.voteOpen = function (voteId, dataId, email, auth) {
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	windowControl.windowOpen('photo', 'index.php?igo=vote&page_name=vote&pmod=xml&vote_id='+voteId+'&data_id='+dataId+'&email='+email+'&auth='+auth, '', [document.body.scrollLeft, document.body.scrollTop+topadd], '');
};

share.voteSend = function (voteId, dataId, email, auth) {
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	windowControl.windowOpen('photo', 'index.php?igo=voteSend&page_name=vote&pmod=xml&vote_id='+voteId+'&data_id='+dataId+'&email='+email+'&auth='+auth, '', [document.body.scrollLeft, document.body.scrollTop+topadd], '');
};

share.itemOldHTML = new Array();
share.itemPickupOldHTML = new Array();
share.itemList = new Array();
share.addCart = function(itemId) {
	if (!itemId) return;
	share.itemId = itemId;
	if (!share.itemOldHTML[itemId]) share.itemOldHTML[itemId] = document.getElementById("shop_item_value_"+share.itemId).innerHTML;
	if (!share.itemPickupOldHTML[itemId]) share.itemPickupOldHTML[itemId] = document.getElementById("shop_pickup_"+share.itemId).innerHTML;
	var itemQn = document.getElementById("cart_qn_"+itemId).value;
	if ((!itemQn) || (itemQn == 0)) itemQn = 1;
	itemPickup = 0;
	for(i=1; i<=4; i++) {
		if (document.getElementById("cart_pickup_"+itemId+"_"+i)) {
			if (document.getElementById("cart_pickup_"+itemId+"_"+i).checked) itemPickup = i;
		}
	}
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=cart_add&pmod=xml&itemId='+itemId+'&itemQn='+itemQn+'&itemPickup='+itemPickup, '', share.addCartReturn, 'shop_item_'+itemId, 'message', 'noReplace');
};

share.addCartReturn = function(returnString) {
	xmlDoc = share.convertStringToXml(returnString);
	var xmlSuccess = xmlDoc.getElementsByTagName('success');
	var xmlError = xmlDoc.getElementsByTagName('error');
	if (xmlSuccess.length) {
		itemId = xmlSuccess[0].getElementsByTagName('itemId')[0].childNodes[0].nodeValue;
		document.getElementById("shop_item_"+itemId).innerHTML = xmlSuccess[0].getElementsByTagName('message')[0].childNodes[0].nodeValue;
		document.getElementById("shop_pickup_"+itemId).innerHTML = xmlSuccess[0].getElementsByTagName('pickupMessage')[0].childNodes[0].nodeValue;
		document.getElementById("shop_item_"+share.itemId).style.display = "";
		document.getElementById("shop_item_value_"+share.itemId).style.display = "none";
		document.getElementById("shop_pickup_"+itemId).style.display = "";
		document.getElementById("shop_pickup_value_"+itemId).style.display = "none";
		share.cartCountRenew();
	}
	if (xmlError.length) {
		itemId = xmlError[0].getElementsByTagName('itemId')[0].childNodes[0].nodeValue;
		document.getElementById("shop_item_value_"+itemId).innerHTML = share.itemOldHTML[itemId]+"<br /><span class='err_font'>"+xmlError[0].getElementsByTagName('message')[0].childNodes[0].nodeValue+"</span>";
	}
};

share.cartRemove = function(itemId) {
	share.itemId = itemId;
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=cart_remove&pmod=xml&itemId='+itemId, '', share.cartRemoveReturn, 'cart', 'message');
};

share.cartRemoveReturn = function(returnString) {
	try {
		document.getElementById("shop_item_"+share.itemId).style.display = "none";
		document.getElementById("shop_item_value_"+share.itemId).style.display = "";
		document.getElementById("shop_pickup_"+share.itemId).style.display = "none";
		document.getElementById("shop_pickup_value_"+share.itemId).style.display = "";
	} catch(error) {
	}
	share.cartCountRenew();
};

share.cartUpdate = function (formName) {
	XMLHttp.formValue(formName);
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=cart_update&pmod=xml', XMLHttp.postString, share.cartUpdateReturn, 'cart', 'message');
};

share.cartUpdateReturn = function(returnString) {
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=cart_check_qn&pmod=xml', XMLHttp.postString, share.cartUpdateQn, 'cart', 'message', 1);
	share.cartCountRenew();
};

share.cartUpdateQn = function(returnString) {
	xmlDoc = share.convertStringToXml(returnString);
	var xmlItem = xmlDoc.getElementsByTagName('item');
	for(i=0; i<xmlItem.length; i++) {
		itemId = xmlItem[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;
		message = xmlItem[i].getElementsByTagName('message')[0].childNodes[0].nodeValue;
		pickupMessage = xmlItem[i].getElementsByTagName('pickupMessage')[0].childNodes[0].nodeValue;
		document.getElementById("shop_item_"+itemId).innerHTML = message;
		document.getElementById("shop_pickup_"+itemId).innerHTML = pickupMessage;
	}
};

share.cartCountRenew = function() {
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=cart_count&pmod=xml', '', '', 'shopCart', 'message');
};

share.cartCheckout = function() {
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	windowControl.windowOpen('cart', 'index.php?igo=checkout&page_name=shop&pmod=xml', '', [document.body.scrollLeft, document.body.scrollTop+topadd], '');
};

share.cartCheckoutRenew = function() {
	XMLHttp.sendReq('POST', 'index.php?igo=checkout&page_name=shop&pmod=xml', '', '', 'cart', 'message');
}

share.inputInfo = function() {
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=info&pmod=xml', '', '', 'cart', 'message');
};

share.inputInfoSend = function(formName) {
	XMLHttp.formValue(formName);
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=info_send&pmod=xml', XMLHttp.postString, '', 'cart', 'message');
};

share.orderSend = function() {
	XMLHttp.sendReq('POST', 'index.php?page_name=shop&igo=order_send&pmod=xml', '', share.orderSendReturn, 'cart', 'message');
};

share.orderSendReturn = function(returnString) {
	share.cartReset();
	if (returnString.indexOf('<!-- -->') >= 0) {
		setTimeout("paymentAuto()", 1000);
	}
};

share.cartReset = function() {
	share.cartCountRenew();
	for(cartKey in share.itemList) {
		try {
			document.getElementById("shop_item_"+cartKey).style.display = "none";
			document.getElementById("shop_item_value_"+cartKey).style.display = "";
			document.getElementById("shop_pickup_"+cartKey).style.display = "none";
			document.getElementById("shop_pickup_value_"+cartKey).style.display = "";
		} catch(error) {
		}
	}
};

share.convertStringToXml = function(string) {
	string2 = share.trim(string);
	if (window.ActiveXObject) {  
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  
		xmlDoc.async="false";  
		xmlDoc.loadXML(string2);
		return xmlDoc;
	} else {  
		parser=new DOMParser();  
		xmlDoc=parser.parseFromString(string2,"text/xml");
		return xmlDoc;  
	}  
};

share.shopRecordInfo = function(dataId) {
	if (document.body.scrollTop < windowControl.windowTopCus) topadd = windowControl.windowTopCus - document.body.scrollTop;
	else topadd = 0;
	windowControl.windowOpen('record', 'index.php?igo=shop_record_info&page_name=member&pmod=xml&dataId='+dataId, '', [document.body.scrollLeft, document.body.scrollTop+topadd], '');
};

share.ltrim = function (instr){
	return instr.replace(/^[\s]*/gi,"");
}

share.rtrim = function (instr){
	return instr.replace(/[\s]*$/gi,"");
}

share.trim = function (instr){
	instr = share.ltrim(instr);
	instr = share.rtrim(instr);
	return instr;
}
