/* ================================================================ 
20090317 sky@Media Explorer Limited begin
	common function :
		browserChecker, addElementInnerHtml, gotoLink, openLink,
	
	form common function
		focusField,
		confirmRemove, confirmDelete,
		fckContent
	
	form checking function :
		formFieldAlert, elementObject,
		checkFileUpload, checkTextWordCount,
		checkTextField, checkSelectField, checkRadioField,
	
	css related : 
		itemSelectSwitched, objectCssClassChange,
	
	mail related :
		checkEmailInfo,
	
	ajax :
		ajaxPost, ajaxGet,
		http_request, makeRequest, makePOSTRequest,
	
	display:
		layerDisplayControl, layerDisplaySwitch,
		fullScreenLayer (concept only),
=================================================================== */

//--------------------------------------- common function -----------------------------------------
//20091028 2352
function browserChecker(langId){
	var isValid = false;
	var isIe = false;
	var isFf = false;
	try{
		var userAgent = navigator.userAgent.toLowerCase();
		isIe = (userAgent.indexOf('msie') > 0);
		isFf = (userAgent.indexOf('firefox') > 0);
		if(isIe){
			var index = userAgent.indexOf('msie');
			var subUserAgent = userAgent.substr(index);
			var userVersion = parseInt(subUserAgent.substring(5, 6));
			if(userVersion >= 7){
				isValid = true;
			}
		}
		else if(isFf){
			var index = userAgent.indexOf('firefox');
			var subUserAgent = userAgent.substr(index);
			var userVersion = parseInt(subUserAgent.substring(8, 9));
			if(userVersion >= 3){
				isValid = true;
			}
		}
		var log = "userAgent : " + userAgent;
		log += "\nisIe : " + isIe;
		log += "\nisFf : " + isFf;
		log += "\nisValid : " + isValid;
		//alert(log);
	}
	catch(err){}
	if(!isValid){
		layerDisplayControl('browserAlertTable', 1, 'block');
		var content1 = '<a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank">';
		var content2 = '<a href="http://www.firefox.com" target="_blank">';
		if(isIe){
			if(langId==1){
				content1 += 'Upgrade your Internet Explorer';
			}
			else if(langId==2){
				content1 += '\u66F4\u65B0\u4F60\u7684 Internet Explorer';
			}
		}
		else{
			if(langId==1){
				content1 += 'Switch to Internet Explorer';
			}
			else if(langId==2){
				content1 += '\u8F49\u63DB\u81F3 Internet Explorer';
			}
		}
		if(isFf){
			if(langId==1){
				content2 += 'Upgrade your Firefox';
			}
			else if(langId==2){
				content2 += '\u66F4\u65B0\u4F60\u7684 Firefox';
			}
		}
		else{
			if(langId==1){
				content2 += 'Switch to Firefox';
			}
			else if(langId==2){
				content2 += '\u8F49\u63DB\u81F3 Firefox';
			}
		}
		content1 += '</a>';
		content2 += '</a>';
		addElementInnerHtml('ieSpan', content1);
		addElementInnerHtml('fireFoxSpan', content2);
	}
}

//20091028 2352
function addElementInnerHtml($$object, content){
	var obj = elementObject($$object);
	obj.innerHTML = content;
}

//20091028 2352
function gotoLink(link){
	location.href = link;
}

//20091028 2352
function openLink(link){
	window.open(link);
}
//--------------------------------------- common function end -----------------------------------------

//--------------------------------------- form common function -----------------------------------------
//20091203 1155
function focusField($$object){
	var obj = elementObject($$object);
	obj.focus();
}
//20091028 2352
function confirmRemove(obj, id, confirmMsg){
	if(confirm(confirmMsg)){
		obj.cmd.value = "remove";
		obj.id.value = id;
		obj.submit();
	}
}

//20091028 2352
function confirmDelete(link, cmd, id, isAllow){
	var confirmMsg = "Please confirm to delete selected item.";
	if(isAllow){
		if(confirm(confirmMsg+"\nID : "+id+"\n")){
			location.href = link+"?cmd="+cmd+"&id="+id;
		}
	}
	else{
		if(alertMsg!=null && alertMsg!=""){
			alert(alertMsg);
		}
	}
}

//20091208 1553
function fckContent($$object){
	var obj = FCKeditorAPI.GetInstance($$object).GetXHTML(true);
	return obj;
}
//--------------------------------------- form common function end -----------------------------------------

//--------------------------------------- form checking function -----------------------------------------
//20091208 1555
function checkFckField($$object){
	var value = fckContent($$object);
	var isValid = !(value==null || value=='');
	return isValid;
}

//20091028 2352
function formFieldAlert($$object, isValid){
	var obj = elementObject($$object);
	if(isValid){
		obj.style.border = '';
		obj.style.borderColor = '';
		obj.style.borderWidth = '';
	}
	else{
		obj.style.border = 'solid';
		obj.style.borderColor = 'red';
		obj.style.borderWidth = '2';
	}
}

//20091028 2352
function elementObject($$object){
	var obj = null;
	if(typeof($$object)=='string'){
		obj = document.getElementById($$object);
	}
	else{
		obj = $$object;
	}
	return obj;
}

//20091028 2352
function checkFileUpload($$object){
	var obj = elementObject($$object);
	var isValid = !(obj==null || obj.value=='');
	if(isValid){
		isValid = (obj.value.lastIndexOf(".jpg")!=-1) || (obj.value.lastIndexOf(".gif")!=-1) || (obj.value.lastIndexOf(".png")!=-1);
	}
	formFieldAlert(obj, isValid);
	return isValid;
}

//20091028 2352
function checkTextWordCount($$object, $$wordCount){
	var isValid = false;
	var obj = elementObject($$object);
	try{
		isValid = (obj.value.length<=$$wordCount);
	}
	catch(err){}
	formFieldAlert(obj, isValid);
	return isValid;
}

//20091028 2352
function checkTextField($$object, $$isNaN){
	var obj = elementObject($$object);
	var isValid = !(obj==null || obj.value=='');
	if(isValid && $$isNaN){
		isValid = !isNaN(obj.value);
	}
	formFieldAlert(obj, isValid);
	return isValid;
}

//20091112 1718
function checkSelectField($$object, $$defaultIndex){
	var obj = elementObject($$object);
	var isValid = !(obj.selectedIndex == $$defaultIndex);
	formFieldAlert(obj, isValid);
	return isValid;
}

/** Must be form object
*	eg. var frm = document.form;
*		$$object = frm.radio;
*/
//20091028 2352
function checkRadioField(obj){
	var isValid = false;
	if(obj!=null){
		if(obj.length==null){
			isValid = obj.checked;
		}
		else{
			for(var i=0 ; i<obj.length ; i++){
				if(obj[i].checked){
					isValid = true;
					break;
				}
			}
		}
	}
	else{
		isValid = true;
	}
	if(!isValid){
		
	}
	return isValid;
}
//--------------------------------------- form checking function end -----------------------------------------

//--------------------------------------- css related -----------------------------------------
//20091028 2352
function itemSelectSwitched(count, index, tagPre, selectedStyle, unSelectedStyle){
	for(var i=0; i<count ; i++){
		if(i == index){
			var tagId = tagPre + i;
			objectCssClassChange(tagId, selectedStyle);
		}
		else{
			var tagId = tagPre + i;
			objectCssClassChange(tagId, unSelectedStyle);
		}
	}
}

//20091028 2352
function objectCssClassChange(tagId, cssClassName){
	var tagObj = document.getElementById(tagId);
	if(tagObj!=null && tagObj.getAttribute('class')==null){
		tagObj.setAttribute('className', cssClassName);
	}
	else if(tagObj!=null){
		tagObj.setAttribute('class', cssClassName);
	}
}
//--------------------------------------- css related end -----------------------------------------

//--------------------------------------- mail related -----------------------------------------
//20091028 2352
function checkEmailInfo(nameObj, mailObj){
	var result = "";
	if(nameObj.value == ""){
		result = "name";
		
		if(mailObj.value != "" && !checkEMail(mailObj.value)){
			result = "mailInvalid";
		}
		else if(mailObj.value != "" && checkEMail(mailObj.value)){
			result = "mailExist";
		}
	}
	else{
		if(mailObj.value == ""){
			result = "mailEmpty";
		}
		else if(!checkEMail(mailObj.value)){
			result = "mailInvalid";
		}
	}
	return result;
}
//------------------------------------ mail related end -------------------------------------

//--------------------------------------- ajax related -----------------------------------------
var http_request = false;

//20091028 2352
function ajaxPost(link, param) {
	makePOSTRequest(link, encodeURI(param));
}

//20091028 2352
function ajaxGet(url) {
	makeRequest(url);
}

//20091215 1726
function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var msg = http_request.responseText;
			if(msg!=null && msg!=''){
				if(msg.indexOf('unique_')==-1){
					ajaxResponse(http_request.responseText);
				}
				else{
					msg = msg.split('unique_')[1];
					ajaxResponse2(msg);
				}
			}
		} else {
			alert("Connection Fail.");
		}
	}
}

//20091028 2352
function makeRequest(url) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url, true);
	http_request.send(null);
}

//20091028 2352
function makePOSTRequest(url, param) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", param.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(param);
}
//------------------------------------ ajax related end -------------------------------------

//------------------------------------ display related -------------------------------------
/*
	$$object - related object ( e.g : img01 or document.getElementById('img01') )
	$$zIndex - target z-index
	$$display - display controller ('none'/'block'/null)
*/
//20091217 1207
function layerDisplayControl($$object, $$zIndex, $$display){
	var obj = elementObject($$object);
	if(obj!=null){
		if($$display==null){
			layerDisplaySwitch(obj, $$zIndex);
		}
		else if(obj.style.display != $$display){
			if($$zIndex!=null){
				obj.style.zIndex = $$zIndex;
			}
			obj.style.display = $$display;
		}
	}
}

//20091028 2352
function layerDisplaySwitch($$object, $$zIndex){
	if($$object.style.display == 'none'){
		if($$zIndex!=null){
			$$object.style.zIndex = $$zIndex;
		}
		$$object.style.display = 'block';
	}
	else{
		if($$zIndex!=null){
			$$object.style.zIndex = -$$zIndex;
		}
		$$object.style.display = 'none';
	}
}

/*
	$$object - related object ( e.g : document.getElementById('img01') )
	$$recordedScrollTop - scroll's top position before function call
	$$recordedScrollLeft - scroll's left position before function call
	$$width -  content exact width
*/
//20091028 2352
function fullScreenLayer($$object, $$recordedScrollTop, $$recordedScrollLeft, $$width){
	if( ($$recordedScrollTop > document.body.scrollTop) || (parseInt($$object.style.top.replace("px",""))+document.body.offsetHeight) < document.body.scrollHeight){
		$$object.style.top = document.body.scrollTop+0 ;
	}
	$$recordedScrollTop = document.body.scrollTop;
	var imgLeft = 0;
	if(document.body.offsetWidth>$$width){
		imgLeft = 0 - (( (document.body.offsetWidth-$$width)/2 ));
		$$object.style.left = imgLeft ;
	}
	else{
		if( ($$recordedScrollLeft > document.body.scrollLeft) || (parseInt($$object.style.left.replace("px",""))+document.body.offsetWidth) < document.body.scrollWidth){
			$$object.style.left = document.body.scrollLeft+0 ;
		}
	}
	
	$$recordedScrollLeft = document.body.scrollLeft;
	$$object.style.width = document.body.offsetWidth-0;
	$$object.style.height = document.body.offsetHeight-0;
}
//------------------------------------ display related end -------------------------------------
