//bar--
var strNavAgent = navigator.userAgent.toLowerCase();
if(strNavAgent.indexOf("firefox") != -1)
{
  browsername='firefox';
}
else
{
  browsername='ie';
}


function include(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
}


function preefunction(object_id)
{
	var tagname;
	location.href="#a_function_list";
	old_ojbect_number=document.getElementById('function_number').value;

	if(old_ojbect_number!=object_id)
	{	
		//先將舊的還原	
		old_objrow=old_ojbect_number.split('_');
		if(!old_objrow[1])
		{
			old_objrow[1]='1';
			old_ojbect_number=old_objrow[0]+"_"+old_objrow[1];
		}	
		
		//把新的加上去
		object_number=object_id;		
		objrow=object_number.split('_');
		if(!objrow[1])
		{
			object_number=objrow[0]+"_1";
			objrow[1]='1';
		}
		
		document.getElementById("function_number").value=object_number;	
		
		if(old_objrow[0]!=objrow[0])
		{
			UnsetAll(old_ojbect_number);
			if(browsername=='ie')
			{
				tagname="className";
			}
			else if(browsername=='firefox')
			{
				tagname='class';
			}
			else
			{
				alert('31232');
			}

			document.getElementById(objrow[0]).setAttribute(tagname, 'activeTab');
			document.getElementById(objrow[0]+"_a").setAttribute(tagname, 'active1');
			document.getElementById(objrow[0]+"_b").setAttribute(tagname, 'active2');
			document.getElementById(objrow[0]+"_c").setAttribute(tagname, 'active3');
			document.getElementById(objrow[0]+"_d").setAttribute(tagname, 'activeTabText');
			document.getElementById("subfun").innerHTML=document.getElementById(objrow[0]+"_e").innerHTML;		
		}

		if((old_objrow[1]!=objrow[1])||(old_objrow[0]!=objrow[0]))
		{
			if(browsername=='ie')
			{
				tagname="className";
			}
			else if(browsername=='firefox')
			{
				tagname='class';
			}
			else
			{
				alert('31232');
			}

			document.getElementById(old_ojbect_number).setAttribute(tagname, 'inactiveText');
			document.getElementById(object_number).setAttribute(tagname, 'activeText');

		}
	}
	
	object_number=document.getElementById("function_number").value;	
	CallXajaxByName(object_number,'mng');		
	
}


function UnsetAll(object_name)
{
	old_objrow=old_ojbect_number.split('_');
	document.getElementById(old_objrow[0]).className='inactiveTab';
	document.getElementById(old_objrow[0]+"_a").className='inactive1';
	document.getElementById(old_objrow[0]+"_b").className='inactive2';
	document.getElementById(old_objrow[0]+"_c").className='inactive3';
	document.getElementById(old_objrow[0]+"_d").className='inactiveTabText';
}

function CallXajax(act,obj)
{	
	document.getElementsByName("action")[0].value=act;	
	xajax_processForm(xajax.getFormValues(obj.form.name));	
}

function CallXajaxByName(act,objname)
{	
	document.getElementsByName("action")[0].value=act;	
	xajax_processForm(xajax.getFormValues(objname));	
}

//主要用於分頁時的頁碼展現功能
function CallXajaxWithPageByName(act,page,objname)
{	
	document.getElementsByName("action")[0].value=act;	
	document.getElementsByName("gotopage")[0].value=page;	
	xajax_processForm(xajax.getFormValues(objname));	
}

function CallXajaxWithParameterByName(act,objname,parameter,type)
{	

	document.getElementsByName("action")[0].value=act;	

	if(type=="c")
	{
		document.getElementsByName("ctempval")[0].value=parameter;	
	}
	else if(type=="f")
	{	////用於指定某值或filler用的值
		document.getElementsByName("fixedtempval")[0].value=parameter;	
	}
	else if(type=="s")
	{	//用於暫時給予某值
		document.getElementsByName("selectedtempval")[0].value=parameter;	
	}
	else
	{
		document.getElementsByName("tempval")[0].value=parameter;		
	}
	
	xajax_processForm(xajax.getFormValues(objname));	
}

function CallXajaxWithParameter(act,obj,parameter,type)
{	
	CallXajaxWithParameterByName(act,obj.form.name,parameter,type);
}



//全選
function chkallbutton(obj)
{	
	var objForm =document.getElementById(obj.id).form;
	var objLen = objForm.length;
		   
   for (var iCount = 0; iCount < objLen; iCount++)
   {
      if (obj.checked)
      {
         if (objForm.elements[iCount].type == "checkbox")
         {
             objForm.elements[iCount].checked = true;
         }
      }
      else
      {
         if (objForm.elements[iCount].type == "checkbox")
         {
             objForm.elements[iCount].checked = false;
         }
      }
    }
	

}


/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/


/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */
function fileQueued(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("Pending...");
		progress.toggleCancel(true, this);

	} catch (ex) {
		this.debug(ex);
	}

}

function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
			return;
		}

		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			progress.setStatus("File is too big.");
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			progress.setStatus("Cannot upload Zero Byte files.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			progress.setStatus("Invalid File Type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				progress.setStatus("Unhandled Error");
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesSelected > 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = false;
		}
		
		/* I want auto start the upload and I can do that here */
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		/* I don't want to do any file validation or anything,  I'll just update the UI and
		return true to indicate that the upload should start.
		It's important to update the UI here because in Linux no uploadProgress events are called. The best
		we can do is say we are uploading.
		 */
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("Uploading...");
		progress.toggleCancel(true, this);
		
		//
		updateDisplay.call(this, file);
	}
	catch (ex) {}
	

		

	
	return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setProgress(percent);		
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {		
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setComplete();
		progress.setStatus("Complete.");
		progress.toggleCancel(false);

	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			progress.setStatus("Upload Error: " + message);
			this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus("Upload Failed.");
			this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			progress.setStatus("Server (IO) Error");
			this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			progress.setStatus("Upload limit exceeded.");
			this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			progress.setStatus("Failed Validation.  Upload skipped.");
			this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			// If there aren't any files left (they were all cancelled) disable the cancel button
			if (this.getStats().files_queued === 0) {
				document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			}
			progress.setStatus("Cancelled");
			progress.setCancelled();
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			progress.setStatus("Stopped");
			break;
		default:
			progress.setStatus("Unhandled Error: " + errorCode);
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		document.getElementById(this.customSettings.cancelButtonId).disabled = true;
	}
}







//Validate



//檢查email是否合法
function check_Email(field_value)
{
	with (field_value)
	{

		apos=field_value.indexOf("@")
		dotpos=field_value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
	  	{
	  		return false
	  	}
		else 
		{
			return true
		}
	}
}


function check_CheckBox(fieldname)
{
	var ccount=0; 
	var obj=document.getElementsByName(fieldname);
	
	for (var i=0;i<obj.length;i++) 
	{ 
		if (obj[i].checked) 
			{ 		
			ccount=ccount+1;
			} 
	}
	if (ccount<1) 
	{ 
		var text=document.getElementsByName("S_"+fieldname)[0].innerHTML;
		return text+"\r\n";			
	}	
	return "";

}

function check_TextField(fieldname)
{

	if(is_element_exist(fieldname))
	{
	
		var value=document.getElementsByName(fieldname)[0].value;
	
		if(value=="")
		{
			if(is_element_exist("S_"+fieldname))
			{
				text=document.getElementsByName("S_"+fieldname)[0].innerHTML;
				return text+"\r\n";
			}			
		}
	}	
	
	return "";	
}

function is_element_exist(ElementName)
{
	var obj=document.getElementById(ElementName);
	if(obj==null)
		return false;
	else
		return true;
	
}


//**************************
// Detects if the current device is an iPhone.

function DetectIphone()
{
   var uagent = navigator.userAgent.toLowerCase();
   if (uagent.search("iphone") > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPod Touch.

function DetectIpod()
{
   var uagent = navigator.userAgent.toLowerCase();
   if (uagent.search("ipod") > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPhone or iPod Touch.
function DetectIphoneOrIpod()
{
    if (DetectIphone())
       return true;
    else if (DetectIpod())
       return true;
    else
       return fals
}
