// add by zheng on 20091221
document.write('<script src="js/lang.js"></'+'script>'); 

function setonlineSubject(id){
xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	   
		// org code before 20091221
        //alert ("Your browser does not support AJAX!");
        
        alert (langObj['Your browser does not support AJAX!']);// add by zheng on 20091221;        

		return;
	}
	var url="subjectoperation.php";
	var param = "";
	param += "id="+id;
	param += "&op=on";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", param.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(param);

}

function setofflineSubject(id){
xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	   
		// org code before 20091221
        //alert ("Your browser does not support AJAX!");
        
        alert (langObj['Your browser does not support AJAX!']);// add by zheng on 20091221;        

		return;
	}
	var url="subjectoperation.php";
	var param = "";
	param += "id="+id;
	param += "&op=off";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", param.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(param);

}
function delSubject(id){
var conf=confirm("您確定要刪除這則專題嗎？");
if(conf==1){
xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	   
		// org code before 20091221
        //alert ("Your browser does not support AJAX!");
        
        alert (langObj['Your browser does not support AJAX!']);// add by zheng on 20091221;        

		return;
	}
	var url="subjectoperation.php";
	var param = "";
	param += "id="+id;
	param += "&op=del";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", param.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(param);

}
}

function nnewSubject()
{
	var flagnewstitle=1;
	if(document.getElementById("subjectTitle").value.length > 40)
	flagnewstitle=0;
	else if(document.getElementById("subjectTitle").value.length == 0)
	flagnewstitle=2;
	
	var flagnewstitlecontent=1;
	if(document.getElementById("subjecttitleContent").value.length > 120)
	flagnewstitlecontent=0;
	else if(document.getElementById("subjecttitleContent").value.length == 0)
	flagnewstitlecontent=2;

	var yr=document.getElementById("subjectyr").value;
	var mn=document.getElementById("subjectmn").value;
	var dd=document.getElementById("subjectdd").value;
	var flagnewsdate=datecheck(yr,mn,dd);
	
	if(flagnewstitle == 1 && flagnewstitlecontent == 1 && flagnewsdate==1)
	{
		document.getElementById("nw").value="1";
		document.getElementById("edit").value="0";
		document.subjectedit.submit();
	}
	else
	{
		if(flagnewstitle == 0){
		document.getElementById("spantitle").innerHTML="<label class=\"stylelbl\">字數過長</label>";
		}
		else if(flagnewstitle == 2){
		document.getElementById("spantitle").innerHTML="<label class=\"stylelbl\">請輸入專題名稱</label>";
		}
		else{
		document.getElementById("spantitle").innerHTML="";
		}

		if(flagnewstitlecontent == 0){
		document.getElementById("spantitlecontent").innerHTML="<label class=\"stylelbl\">字數過長</label>";
		}
		else if(flagnewstitlecontent == 2){
		document.getElementById("spantitlecontent").innerHTML="<label class=\"stylelbl\">請輸入簡述</label>";
		}
		else{
		document.getElementById("spantitlecontent").innerHTML="";
		}

		if(flagnewsdate == 0){
		document.getElementById("spantitledate").innerHTML="<label class=\"stylelbl\">請輸入正確日期</label>";
		}
		else{
		document.getElementById("spantitledate").innerHTML="";
		}
	}
}



function editSubject(id){
window.location='subjectedit.php?op=edita&id='+id;
}

function createSubject(){
window.location='subjectedit.php?op=newa&id=';
}

function editSubjectPriority(){
window.location='subjectpriority.php';
}

function editintroductioncontent()
{
window.location='editintrocontent.php';
}

function updateSubject(){

	document.getElementById("edit").value="1";
	document.getElementById("nw").value="0";

	var flagnewstitle=1;
	if(document.getElementById("subjectTitle").value.length > 80)
	flagnewstitle=0;
	else if(document.getElementById("subjectTitle").value.length == 0)
	flagnewstitle=2;
	
	var flagnewstitlecontent=1;
	if(document.getElementById("subjecttitleContent").value.length > 220)
	flagnewstitlecontent=0;
	else if(document.getElementById("subjecttitleContent").value.length == 0)
	flagnewstitlecontent=2;

	var yr=document.getElementById("subjectyr").value;
	var mn=document.getElementById("subjectmn").value;
	var dd=document.getElementById("subjectdd").value;
	var flagnewsdate=datecheck(yr,mn,dd);
	
	if(flagnewstitle == 1 && flagnewstitlecontent == 1 && flagnewsdate==1){
	document.subjectedit.submit();
	}else{
		if(flagnewstitle == 0){
		document.getElementById("spantitle").innerHTML="<label class=\"stylelbl\">專題名稱字數過長</label>";
		}
		else if(flagnewstitle == 2){
		document.getElementById("spantitle").innerHTML="<label class=\"stylelbl\">專題名稱不得空白</label>";
		}
		else
		document.getElementById("spantitle").innerHTML="";

		if(flagnewstitlecontent == 0){
		document.getElementById("spantitlecontent").innerHTML="<label class=\"stylelbl\">專題簡述字數過長</label>";
		}
		else if(flagnewstitlecontent == 2){
		document.getElementById("spantitlecontent").innerHTML="<label class=\"stylelbl\">專題簡述不得空白</label>";
		}
		else
		document.getElementById("spantitlecontent").innerHTML="";

		if(flagnewsdate == 0){
		document.getElementById("spantitledate").innerHTML="<label class=\"stylelbl\">請輸入正確日期</label>";
		}
		else
		document.getElementById("spantitledate").innerHTML="";
	
	}
	
	
}


function updateintrocontent()
{
document.getElementById("edit").value="1";
document.introcontent.submit();
}
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState==200){
    var ss=xmlHttp.responseText;
    
   //alert(ss);  // org code before 20091222

    alert (langObj[ss]); // add by zheng on 20091222
    
    window.location="subject.php";
    }
}
function cancelintro()
{
window.location="introduction.php";
}
function cancelSubject()
{
window.location="subject.php";
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
    xmlHttp=new XMLHttpRequest();
    }
catch (e){
    try{
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
    catch (e){
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
    }

return xmlHttp;
}

function datecheck(yr,mn,dd){
var  dateStr=dd+"/"+mn+"/"+yr;

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
//alert("Please enter your birth date as dd/mm/yyyy. Your  selection reads:"+dateStr );
return 0;
}

day = matchArray[1]; // p@rse date into variables
month = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
//alert("Month must be between 1 and 12.");
return 0;
}

if (day < 1 || day > 31) {
//alert("Day must be between 1 and 31.");
return 0;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("Month "+month+" doesn`t have 31 days!");
return 0;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
//alert("February " + year + " doesn`t have " + day +  "days!");
return 0;
}
}
return 1; // date is valid


}

function checkSubjectTitle()
{
	if(document.getElementById("subjectTitle").value.length > 40)
	alert("專題名稱字數過長");
}

function checkSubjectTitleContent()
{
	if(document.getElementById("subjecttitleContent").value.length > 120)
	alert("專題簡述字數過長");
}


