function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function $(id){
	return document.getElementById(id);
}

function checkElement(id){
	if(!$(id)) return true;
	if(trim($(id).value) == ''){
		$(id).parentNode.getElementsByTagName('label')[0].style.color = '#ce3319';
		$(id).style.borderColor = 'red';
		return false;
	}else{
		if(id == 'validation'){
			if(trim($(id).value).length != 4){
				$(id).parentNode.getElementsByTagName('label')[0].style.color = '#ce3319';
				$(id).style.borderColor = 'red';
				return false;
			}
		}
		$(id).parentNode.getElementsByTagName('label')[0].style.color = '';
		$(id).style.borderColor = '#7C7C7C #C3C3C3 #DDDDDD';
		return true;
	}
}

function init_form(){
	
	if($('author')){
		$('author').onblur = function(){
			checkElement('author');
		}
	}
	
	if($('email')){
		$('email').onblur = function(){
			checkElement('email');
		}
	}
	
	if($('content')){
		$('content').onblur = function(){
			checkElement('content');
		}
	}
	
	if($('validation')){
		$('validation').onblur = function(){
			checkElement('validation');
		}
	}
	
	if($('cmtform')){
		$('cmtform').onsubmit = function(){
			var res1 = checkElement('author');
			var res2 = checkElement('email');
			var res3 = checkElement('content');
			var res4 = checkElement('validation');
			return (res1&&res2&&res3&&res4);
		}
	}
}

function trim(str){
    var lr = /\s*((\S+\s*)*)/;
    var rr = /((\s*\S+)*)\s*/;
    str = str.replace(lr,"$1");
    str = str.replace(rr,"$1");
    return str;
}

function resizeImage(){
	var MaxWidth=695;
	var MaxHeight=99999;
	var imgs=document.getElementsByTagName('img');
	var length = imgs.length;
	for(var i=0; i<length; i++){
		var HeightWidth=imgs[i].offsetHeight/imgs[i].offsetWidth;//设置高宽比 
		var WidthHeight=imgs[i].offsetWidth/imgs[i].offsetHeight;//设置宽高比 
		if(imgs[i].offsetWidth>MaxWidth){ 
			imgs[i].width=MaxWidth; 
			imgs[i].height=MaxWidth*HeightWidth; 
		}
		if(imgs[i].offsetHeight>MaxHeight){ 
			imgs[i].height=MaxHeight; 
			imgs[i].width=MaxHeight*WidthHeight; 
		}
	}
}

init_form();

function afterOnload(){
	resizeImage();
}

window.onload = function(){
	resizeImage();
}