String.prototype.Trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
//取标签的绝对位置
function Offset(e){
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight - 2;
	while(e = e.offsetParent){
		t += e.offsetTop;
		l += e.offsetLeft;
	}
	return {
		top : t,
		left : l,
		width : w,
		height : h
	}
}

function getCheckBoxValue(strForm,strElement){
	var s = '';
	with (strForm){
		for (var i = 0; i < elements[strElement].length; i++){
			if (elements[strElement][i].checked){
				s += elements[strElement][i].value + ',';
			}
		}
	}
    return s;
}

function getInputNums(strForm,strElement){
	with (strForm){
		return elements[strElement].length;
	}
}

function CheckBoxSel(strForm,strElement,strValue){
    var strValue = strValue;
	with (strForm){
		for (var i = 0;i < elements[strElement].length ;i++){
			str_Value = strValue.split(",");
			for (var j = 0;j < str_Value.length ;j++ ){
				if(str_Value[j] == elements[strElement][i].value){
					elements[strElement][i].checked = true;
					break;
				}
			}
		}
	}
}


function ReLoadValid(id,url){
	document.getElementById(id).src = url+'?'+Math.random();
}


function AutoSizeDIV(objID,setHeight){
	var obj = document.getElementById(objID);
	var obj_div = Offset(obj);
	if (obj.scrollHeight < obj.offsetHeight){
		obj.style.height = obj.scrollHeight + 2;
	}
	if (obj_div.height > setHeight){
		obj.style.height = setHeight+'px';
		obj.style.overflowX = 'hidden';
		obj.style.overflowY = 'auto';
	}
}

function Comment(sAction,sType,sFormOrId) {
	this.PostForm = function(cType,cId) {
		$('commBox').innerHTML = '<img src="images/loading.gif" />';
		new Ajax.Request('comment.php?rnd='+Math.random()+'&x=postform&cType='+cType+'&cId='+cId, 
			{ 
				method:'get', 
				onSuccess: function(transport){
					$('commBox').innerHTML = transport.responseText;
				}, 
				onFailure: function(){$('commBox').innerHTML = 'Ajax Request Error...';}
			}
		);
	}

	this.SaveComment = function(cType,theForm) {
		if($('commContactType').value.Trim() != '' && $('commContact').value.Trim().match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == null) {
			alert('Please input your '+$('commContactType').value.Trim()+' address');
			$('commContact').focus();
			return false;
		}
		if($('commContent').value.Trim().length < 4) {
			alert('The Comment Content is too short');
			$('commContent').focus();
			return false;
		}
		if($('vcode').value.Trim().length != 4) {
			alert('Security Code is incorrect');
			$('vcode').focus();
			return false;
		}
		new Ajax.Request(theForm.action,{ 
			method:'post', 
			parameters:Form.serialize($(theForm)),
			onLoading: function() {
				theForm.disabled = true;
			},
			onSuccess: function(transport){
				theForm.disabled = false;
				var re = transport.responseText;
				res = re.split('|');
				alert(res[1]);
				//var cId = $('cId').value;
				if (res[0] == 1){
					theForm.reset();
					ReLoadValid('vimg','/inc/validcode.php');
				}
			}
		}
		);
	}

	this.ShowList = function(cType,cId) {
		$('commBox').innerHTML = '<img src="images/loading.gif" />';
		new Ajax.Request('comment.php?rnd='+Math.random()+'&x=showlist&cType='+cType+'&cId='+cId, 
			{ 
				method:'get', 
				onSuccess: function(transport){
					$('commBox').innerHTML = transport.responseText;
				}, 
				onFailure: function(){$('commBox').innerHTML = 'Ajax Request Error...';}
			}
		);
	}
	switch (sAction) {
	    case 'postform' :
	        PostForm(sType,sFormOrId);
	        break;
	    case 'showlist' :
	        ShowList(sType,sFormOrId);
	        break;
	    case 'savecomment' :
	        SaveComment(sType,sFormOrId);
	        break;
	    default :
	        alert('Errors');
	}
}

function artCategory(rootID) {
	document.getElementById('sublist_'+rootID).innerHTML = '<img src="images/loading.gif" />';
	if(document.getElementById('sublist_'+rootID).style.display == 'none') {
		document.getElementById('sublist_'+rootID).style.display = '';
		new Ajax.Request('category.php?rnd='+Math.random()+'&rootID='+rootID, 
			{ 
				method:'get', 
				onSuccess: function(transport){
					$('sublist_'+rootID).innerHTML = transport.responseText;
					document.getElementById('root_'+rootID).src = 'images/tree/folderOpen.gif';
				}, 
				onFailure: function(){$('sublist_'+rootID).innerHTML = 'Ajax Request Error';}
			}
		);
	}
	else {
		document.getElementById('sublist_'+rootID).innerHTML = '';
		document.getElementById('sublist_'+rootID).style.display = 'none';
		document.getElementById('root_'+rootID).src = 'images/tree/folderClosed.gif';
	}
}

function ContactDo(theForm) {
	if($('mail_from').value.Trim().match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == null) {
		alert('invalid email address');
		$('mail_from').focus();
		return false;
		
	}
	if($('mail_subject').value.Trim() == '') {
		alert('message title is required');
		$('mail_subject').focus();
		return false;
		
	}
	if($('mail_body').value.Trim() == '') {
		alert('message content is required');
		$('mail_body').focus();
		return false;
		
	}
	if($('vcode').value.Trim().length != 4 || $('vcode').value.Trim().match(/^[a-zA-Z]/) == null) {
		alert('Security Code is invalid');
		$('vcode').focus();
		return false;
	}

	new Ajax.Request(theForm.action,{ 
		method:'post', 
		parameters:Form.serialize($(theForm)),
		onLoading: function() {
			theForm.disabled = true;
			$('button').disabled = true;
			$('button').value = 'PROCESSING...';
		},
		onSuccess: function(transport){
			var re = transport.responseText;
			res = re.split('|');
			alert(res[1]);
			theForm.disabled = false;
			$('button').disabled = false;
			$('button').value = 'SEND MESSAGE';
			if (res[0] == 1){
				theForm.reset();
				ReLoadValid('vimg','/inc/validcode.php');
			}
		},
		onFailure: function(transport) {
			alert('Error:' + transport.responseText.stripTags());
		}
	}
	);
}

//editor
function eRunCode(codeid) {
	var codebox = document.getElementById(codeid);
	var win = window.open('about:blank');
	win.document.open('text/html', 'replace');
	win.document.writeln(codebox.innerText);
}
function eCopyCode(obj) {
	var getObj = document.getElementById(obj);
	var rng = document.body.createTextRange();
	rng.moveToElementText(getObj);
	rng.scrollIntoView();
	rng.select();
	rng.execCommand("Copy");
	rng.collapse(false);
}

function HostingDo(sAction,sSecond,sThird) {
	this.Rating = function(theForm) {
		var Rating_checked = false;
		for (var i = 0; i < getInputNums(theForm,'rateVal'); i++) {
			if (theForm.elements['rateVal'][i].checked == true){
				Rating_checked = true;
				break;
			}
		}

		if(Rating_checked == false) {
			alert('Please select your rating value');
			theForm.elements['rateVal'][0].focus();
			return false;
		}

		new Ajax.Request(theForm.action,{ 
			method:'post', 
			parameters:Form.serialize($(theForm)),
			onLoading: function() {
				theForm.disabled = true;
				$('button').disabled = true;
				$('button').value = 'Rating...';
			},
			onSuccess: function(transport){
				var re = transport.responseText;
				res = re.split('|');
				alert(res[1]);
				theForm.disabled = false;
				$('button').disabled = false;
				$('button').value = 'Rating';
				if (res[0] == 1){
					theForm.reset();
					$('rateValue').innerHTML = res[2];
					$('ratedN').innerHTML = parseInt($('ratedN').innerHTML) + 1;
					$('ratedN1').innerHTML = parseInt($('ratedN1').innerHTML) + 1;
				}
			},
			onFailure: function(transport) {
				alert('Error:' + transport.responseText.stripTags());
			}
		}
		);
	}
	switch (sAction) {
	    case 'rating' :
	        Rating(sThird);
	        break;
	    case 'showlist' :
	        ShowList(sSecond,sThird);
	        break;
	    case 'savecomment' :
	        SaveComment(sSecond,sThird);
	        break;
	    default :
	        alert('Errors');
	}
}

function HostingPostSave(theForm) {
	if($F('hostType').Trim() == '') {
		alert('Hosting type error, free or paid?');
		$('hostType').focus();
		return false;
	}
	var hostType = $F('hostType').Trim();
	if(hostType == 'paid') {
		if($F('hostPrice').Trim() != '' && $F('hostPrice').Trim().match(/^\d+(\.\d+)?$/) == null) {
			alert('Hosting price is not correct');
			$('hostPrice').focus();
			return false;
		}
	}
	if($F('hostURL').Trim().match(/^(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$/) == null) {
		alert('Provier website URL is not correct');
		$('hostURL').focus();
		return false;
	}
	if($F('hostWebspace').Trim() != '' && $F('hostWebspace').Trim().match(/^\d+(\.\d+)?|(-1)$/) == null) {
		alert('Hosting webspace size is not correct');
		$('hostWebspace').focus();
		return false;
	}
	if($F('hostBandwidth').Trim() != '' && $F('hostBandwidth').Trim().match(/^\d+(\.\d+)?|(-1)$/) == null) {
		alert('Bandwidth limitation is not correct');
		$('hostBandwidth').focus();
		return false;
	}

	var hostControlPanels_check = false;
	for (var i = 0; i < getInputNums(theForm,'hostControlPanels'); i++) {
		if (theForm.elements['hostControlPanels'][i].checked == true){
			hostControlPanels_check = true;
			break;
		}
	}
	if(hostControlPanels_check == false) {
		alert('Please tell us what is the hosting control panel');
		theForm.elements['hostControlPanels'][0].focus();
		return false;
	}

	var hostOS_check = false;
	for (var i = 0; i < getInputNums(theForm,'hostOS'); i++) {
		if (theForm.elements['hostOS'][i].checked == true){
			hostOS_check = true;
			break;
		}
	}

	if(hostOS_check == false) {
		alert('Please tell us what is the server OS');
		theForm.elements['hostOS'][0].focus();
		return false;
	}

	if($('hostCountry').value == '') {
		alert('Please tell us where is the server located');
		$('hostCountry').focus();
		return false;
	}

	if($F('hostParkedDomains').Trim() != '' && $F('hostParkedDomains').Trim().match(/^(\d+$)|(-1)/) == null) {
		alert('The ammount of parked domain is not correct');
		$('hostParkedDomains').focus();
		return false;
	}
	if($F('hostSubDomains').Trim() != '' && $F('hostSubDomains').Trim().match(/^(\d+$)|(-1)/) == null) {
		alert('The ammount of subdomain is not correct');
		$('hostSubDomains').focus();
		return false;
	}
	if($F('hostAddonDomains').Trim() != '' && $F('hostAddonDomains').Trim().match(/^(\d+$)|(-1)/) == null) {
		alert('The ammount of addon domain is not correct');
		$('hostAddonDomains').focus();
		return false;
	}
	if(hostType == 'paid') {
		if($F('hostApplyURL').Trim() != '' && $F('hostApplyURL').Trim().match(/^[a-zA-Z]+:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/) == null) {
			alert('Hosting plan order URL is not correct');
			$('hostApplyURL').focus();
			return false;
		}
	}
	else {
		if($F('hostApplyURL').Trim().match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == null && $F('hostApplyURL').Trim().match(/^[a-zA-Z]+:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/) == null) {
			alert('Hosting requests URL is not correct');
			$('hostApplyURL').focus();
			return false;
		}
	}

	if($F('hostDemoURL').Trim() != '' && $F('hostDemoURL').Trim().match(/^[a-zA-Z]+:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/) == null) {
		alert('Hosting demo URL is not correct');
		$('hostDemoURL').focus();
		return false;
	}
	if($F('vcode').Trim().length != 4) {
		alert('Security code is not correct');
		$('vcode').focus();
		return false;
	}

	new Ajax.Request(theForm.action,{ 
		method:'post', 
		parameters:Form.serialize($(theForm)),
		onLoading: function() {
			theForm.disabled = true;
		},
		onSuccess: function(transport){
			theForm.disabled = false;
			var re = transport.responseText;
			res = re.split('|');
			alert(res[1]);
			if (res[0] == 1){
				theForm.reset();
			}
		},
		onFailure: function(transport) {
			alert('Error:' + transport.responseText.stripTags());
		}
	}
	);
}

function FriendLink(theForm) {
	if($F('linkName').Trim() == '') {
		alert('Your website name is required');
		$('linkName').focus();
		return false;
	}
	if($F('linkURL').Trim().match(/^[a-zA-Z]+:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/) == null) {
		alert('Your website domain is not correct');
		$('linkURL').focus();
		return false;
	}
	if($F('vcode').Trim().length != 4) {
		alert('Security code is not correct');
		$('vcode').focus();
		return false;
	}
	new Ajax.Request(theForm.action,{ 
		method:'post', 
		parameters:Form.serialize($(theForm)),
		onLoading: function() {
			theForm.disabled = true;
		},
		onSuccess: function(transport){
			theForm.disabled = false;
			var re = transport.responseText;
			res = re.split('|');
			alert(res[1]);
			if (res[0] == 1){
				theForm.reset();
				ReLoadValid('vimg','/inc/validcode.php');
			}
		},
		onFailure: function(transport) {
			alert('Error:' + transport.responseText.stripTags());
		}
	}
	);
}