//this function check validity of email with and error message
function checkemail(emailId,errorbgColor,rightbgcolor,divcolor,fontColor,offset,warning){
	var emailele=document.getElementById(emailId);
	var x=0,y=0;
	var emailwidth=emailele.offsetWidth;
	var parentele=emailele;
	while(parentele){
		x+=parentele.offsetLeft;
		y+=parentele.offsetTop;
		parentele=parentele.offsetParent;
	}
	var x=emailele.offsetLeft;
	var y=emailele.offsetTop;
	var email=emailele.value.match(/^[a-zA-Z0-9_.-]+[@]{1}[a-zA-Z0-9_-]+[.]{1}\w+/);
	if(email==null){
	var div=document.createElement("div");
	div.setAttribute("id","emailwarning");
	div.style.position="absolute";
	div.style.top=y+"px";
	div.style.left=x+emailwidth+offset+"px";
	div.style.color=fontColor;
	div.style.backgroundColor=divcolor;
	div.innerHTML=warning;
	document.body.appendChild(div);
	emailele.style.backgroundColor=errorbgColor;
	}else{
	var div=document.getElementById("emailwarning");
	emailele.style.backgroundColor=rightbgcolor;
	if(div!=null){
		document.body.removeChild(div);
		//emailele.style.backgroundColor=rightbgcolor;
		}
	}
}
//check email validity and change color
function checkemailvcolor(emailId,errorbgColor,rightbgcolor){
	var emailele=document.getElementById(emailId);
	var x=0,y=0;
	var emailwidth=emailele.offsetWidth;
	var parentele=emailele;
	while(parentele){
		x+=parentele.offsetLeft;
		y+=parentele.offsetTop;
		parentele=parentele.offsetParent;
	}
	var x=emailele.offsetLeft;
	var y=emailele.offsetTop;
	var email=emailele.value.match(/^[a-zA-Z0-9_.-]+[@]{1}[a-zA-Z0-9_-]+[.]{1}\w+/);
	if(email==null && emailele.value.length!=0){
	emailele.style.backgroundColor=errorbgColor;
	}else if(emailele.value.length==0 || email!=null){
	emailele.style.backgroundColor=rightbgcolor;
	}
}
//check whether 2 times input passwords are match
function pwdmatch(pwd1,pwd2){
	var pwd1=document.getElementById(pwd1);
	var pwd2=document.getElementById(pwd2);
}

//add another file-upload type <input> tag
//this function has 2 parameters,-replaceid mean element of need to be moved/replaced,-formnumber mean which form need use this function
function addafile(replaceid,formnumber,size){
var submit=document.getElementById(replaceid);
document.forms[0].removeChild(submit);
var nl=document.createElement("br");
var addfile=document.createElement("input");
addfile.setAttribute("type","file");
addfile.setAttribute("name","images[]");
addfile.setAttribute("size",size);
document.forms[formnumber].appendChild(nl);
document.forms[formnumber].appendChild(addfile);
document.forms[formnumber].appendChild(submit);
}

function checkhac(hac,eid,phpmd5code,jmd5code){
if(phpmd5code==jmd5code){
	var element=document.getElementById(eid);
	element.removeAttribute("disabled");
	}
}
//detect what kind of browser(IE or FIREFOX) and disable CSS accordingly.
function pickCSS(ff,ie6,ie7,ie8){
var ffcss=document.getElementById(ff);
var ie6css=document.getElementById(ie6);
var ie7css=document.getElementById(ie7);
var ie8css=document.getElementById(ie8);

var browser=navigator.appName;
var version=navigator.appVersion;
var matchrs=version.match(/MSIE 7\.0/);

if(browser=="Microsoft Internet Explorer"){
	if(version.match(/MSIE 6\.0/)){
		ffcss.disabled="true";
		ie7css.disabled="true";
		ie8css.disabled="true";
	}else if (version.match(/MSIE 7\.0/)){
		ffcss.disabled="true";
		ie6css.disabled="true";
		ie8css.disabled="true";
	}else if (version.match(/MSIE 8\.0/)){
		ffcss.disabled="true";
		ie6css.disabled="true";
		ie7css.disabled="true";
	}
	}else{
	ie6css.disabled="true";
	ie7css.disabled="true";
	ie8css.disabled="true";
}
}

//switch assigned 2 images
function switchimg(imgid,img1,img2){
var img=document.getElementById(imgid);
var imgname=img.getAttribute("src");
var regexpress=new RegExp(img1);
if(imgname.search(regexpress)==-1)	img.setAttribute("src",img1);
else	img.setAttribute("src",img2);
}

//change font's color
function changefontcolor(ele,color){
if(typeof(ele)=="object"){
	ele.style.color=color;
}else{
	ele=document.getElementById(ele);
	ele.style.color=color;
}
}


//change opacity
function changeopacity(ele,opa){
ele=document.getElementById(ele);
ele.style.opacity=opa+"";
ele.style.filter="alpha(opacity="+opa*100+")";
}

//change border color
function changebordercolor(ele,color){
if(typeof(ele)=="object"){
	ele.style.borderColor=color;
}else{
	ele=document.getElementById(ele);
	ele.style.borderColor=color;
}
}

//put image in <div> tag's center and middle and cut overflowed border
function adjustimg(divclassname,imgclassname,targetimgwidth,divwidth,divheight){
	try{
		var imgdiv=[],imgs=[],links=[];
		var divs=document.getElementsByTagName("div");
		var allimg=document.getElementsByTagName("img");
		for(var i=0;i<divs.length;i++){
			if(divs[i].className.toString()==divclassname) imgdiv.push(divs[i]);
		}
		for(var i=0;i<allimg.length;i++){
			if(allimg[i].className.toString()==imgclassname) imgs.push(allimg[i]);
		}
		//alert("div:"+imgdiv.length+"\r\nimgs:"+imgdiv.length);
		for(var i=0;i<imgdiv.length;i++){
			imgdiv[i].style.overflow="hidden";
			imgs[i].style.width=targetimgwidth+"px";		//note here,here set image's size.either can set in img tag with width or height property
			imgs[i].style.position="relative";
			imgs[i].style.left="0px";
			imgs[i].style.top="0px";
			imgw=parseInt(imgs[i].width);
			imgh=parseInt(imgs[i].height);
			divw=divwidth;
			divh=divheight;
			hoffset=parseInt((imgw-divw)/2);
			voffset=parseInt((imgh-divh)/2);
			imgs[i].style.left=-hoffset+"px";
			imgs[i].style.top=-voffset+"px";
			//alert("div:"+hoffset+"\r\nimgs:"+voffset);
		}
	}catch(ex){
		alert(ex);
	}
}
//fade
function fade(ele,from,to){
	var opa=from,target=to;
	ele=document.getElementById(ele);
	function changing(){
		if(opa>target){
			opa-=0.1;
			ele.style.opacity=opa+"";
			ele.style.filter="alpha(opacity="+opa*100+")";
		}
	}
	veilintid=setInterval(changing,100);
	if(opa<=target) clearInterval(veilintid);
}

//switch opacity
function switchopacity(ele,opa1,opa2){
var ele=document.getElementById(ele);
var copc=ele.style.opacity;
if(copc==opa1){
ele.style.opacity=opa2+"";
ele.style.filter="alpha(opacity="+opa2+")";
}
else{
ele.style.opacity=opa1+"";
ele.style.filter="alpha(opacity="+opa1+")";
}
}

//change background color
function changebgcolor(ele,color){
if(typeof(ele)=="object"){
ele.style.backgroundColor=color;
}else{
ele=document.getElementById(ele);
ele.style.backgroundColor=color;
}
}
//show an element
function show(eid){
var element=document.getElementById(eid);
element.style.visibility="visible";
element.style.display="block";
}
//hide an element
function hide(eid){
var element=document.getElementById(eid);
element.style.visibility="hidden";
element.style.display="none";
}


//show or hidde an element
function showhide(eid){
var element=document.getElementById(eid);
if(element.style.visibility!=''){
	if(element.style.visibility=="visible")	element.style.visibility="hidden";
	else	element.style.visibility="visible";
}else if(element.style.display!=''){
	if(element.style.display=="block")	element.style.display="none";
	else	element.style.display="block";
}
}
//insert an element into another element
function replacetimg(parentele,newimgfullname,oldimgid,height,width){
var parentele=document.getElementById(parentele);
var oldimg=document.getElementById(oldimgid);
var newimg=document.createElement("img");
newimg.setAttribute("src",newimgfullname);
newimg.setAttribute("id",oldimgid);
newimg.style.width=width+"px";
newimg.style.height=height+"px";
parentele.replaceChild(newimg,oldimg);
}

function checkradio(eleid,attname){
	eleid='#'+eleid;
	var selectid='#'+$(eleid).attr(attname);
	$(selectid).attr('checked','checked');
}