// JavaScript Document

/********** BASE FUNCTIONS ************/
function $(objid)
{
	var obj = document.getElementById(objid);
	return obj;
}
function $_$(objname)
{
	var obj = document.getElementsByTagName(objname);
	return obj;
}
function $_value(objid)
{
	var obj = $(objid);
	return obj.value;
}
/********** BASE FUNCTIONS ************/



/******* USED IN LEFT MENU ======= NOT USED ANY MORE *******/
//change left menu status
function OpenOrCloseUl(ObjValue)
{
	if(ObjValue == ""){
		return;
	}
	var obj=$_$("ul");
	for(var i=0;i<obj.length;i++)
	{
		if(obj[i].id == ObjValue){
			if(obj[i].style.display == "none"){
				obj[i].style.display = "";
			}else{
				obj[i].style.display = "none";
			}
		}
	}
}
/******* USED IN LEFT MENU ======= NOT USED ANY MORE *******/

/******* USED IN TOP MENU *******/
function OpenOrCloseSubNav(ObjValue)
{
	if(ObjValue == ""){
		return;
	}
	var obj=$_$("div");
	for(var i=0;i<obj.length;i++)
	{
		//do not alert top nav menu
		if(obj[i].className!="SubNavDiv"){
			continue;
		}
		
		//alert the sub nav
		obj[i].style.display = "none";
		if(obj[i].id == ObjValue){
			if(obj[i].style.display == "none"){
				obj[i].style.display = "";
			}else{
				obj[i].style.display = "none";
			}
		}
	}
}

function closeCurrentDiv(divID)
{
	var obj = $(divID);
	obj.style.display="none";
}
/******* USED IN TOP MENU *******/



/********** Validate Login Details ************/
//Main Validate Class ( PROCESS FORM SUBMIT )
function ObjAttribute()
{
	this.format_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;	//Format of Email
	
	this.format_phone = /^\d{3}-\d{3}-\d{4}(|-\d{1,6})$/;	//Format of USA Tel
	
	this.format_zip = /^\d{5}$/;	//zip format
	
	this.format_year = /^\d{4}$/;	//year format
	
	this.username = "";
	
	this.password = "";
	
	this.errmsg = 'Sorry, we cannot complete your request.<br />Kindly provide us the missing or incorrect information enclosed below.<br /><br />';	//default error message
	
	this.errorcolor = "#BB0000";
	this.font_color = "#FFFFFF";
	
	/***** main function validate start *****/
	this.validate = function(Objfrm,Objerr){
		if(Objfrm==""){
			return false;
		}
		
		var len=Objfrm.elements.length;
		for(var i=0;i<len;i++){ //cycle for all the elements
			
			with(Objfrm.elements[i]){
				
				var _require = getAttribute("require");
				var _datatype = getAttribute("datatype");
				var _disabled = getAttribute("disabled");
				
				if(_disabled!=null && _disabled!=false){	//check if this item needs validation
					continue;
				}
				if(_datatype!=null && _datatype!=false){ //check if this item needs validation
					if(_require!=null && _require!=false){ //don't need validating
						this.CheckNull(Objfrm,i,value,_datatype);						
					}else{	//continue when the obj is not required, and the value is null
						if( value == "" ){
							continue;
						}
					}
					switch(_datatype){
						case "Email":
						case "CC":
						case "BCC":
						case "Email From":
							this.CheckEmail(Objfrm,i,value,_datatype);
							continue;
						case "Phone":
						case "Telephone":
						case "Billing Phone":
						case "Recipient Phone":
							this.CheckPhone(Objfrm,i,value,_datatype);
							continue;
						case "Zip":
						case "Billing Zip":
						case "Recipient Zip":
							this.CheckZip(Objfrm,i,value,_datatype);
							continue;
						case "Year of Birth":
							this.CheckYear(Objfrm,i,value,_datatype);
							continue;
						case "AD Image":
						case "Company AD":
						case "Company Logo":
							this.CheckEx(Objfrm,i,value,_datatype);
							continue;
						case "AD URL":	
						case "Company URL":
						case "Link URL":
							this.CheckURL(Objfrm,i,value,_datatype);
							continue;
						case "Price":
							this.CheckPrice(Objfrm,i,value,_datatype);
							continue;
						case "Quantity":
							this.CheckNumeric(Objfrm,i,value,_datatype);						
							continue;
						case "Thumbnail Picture":
							this.CheckEx(Objfrm,i,value,_datatype);						
							continue;
						default:
							continue;
					}
					
				}else{
					continue;
				}
				
			}
			
		}
		if(this.errmsg!='Sorry, we cannot complete your request.<br />Kindly provide us the missing or incorrect information enclosed below.<br /><br />'){
			Objerr.style.display="";
			//var CellPosition = Objerr.cells[0];
			Objerr.innerHTML=this.errmsg;
			return false;
			
		}else{
			Objerr.style.display="none";
			return true;
		}
		
	}
	/***** main function end *****/
	
	
	
	/***** validate if required start *****/
	this.CheckNull = function (Objfrm,ObjOrder,ObjValue,DataType){
		
		if(ObjValue==''){
			//Objfrm.elements[ObjOrder].style.backgroundColor=this.errorcolor;
			//Objfrm.elements[ObjOrder].style.color=this.font_color;
			//this.errmsg += "- Please Input <strong>"+DataType+"</strong> <br>";
			this.errmsg += "- <strong>"+DataType+"</strong> is Required <br>";
			return false;
		}else{
			return true;
		}
		
	}
	/***** validate if required start *****/
	
	
	/***** validate email start *****/
	this.CheckEmail = function (Objfrm,ObjOrder,ObjValue,DataType){
		
		if(!ObjValue.match(this.format_email)){
			//Objfrm.elements[ObjOrder].style.backgroundColor=this.errorcolor;
			//Objfrm.elements[ObjOrder].style.color=this.font_color;			
			//var DataType = Objfrm.elements[ObjOrder].name;			
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
		
	}
	/***** validate email end *****/
	
	
	/***** validate phone start *****/
	this.CheckPhone = function (Objfrm,ObjOrder,ObjValue,DataType){
		
		if(!ObjValue.match(this.format_phone)){
			//Objfrm.elements[ObjOrder].style.backgroundColor=this.errorcolor;
			//Objfrm.elements[ObjOrder].style.color=this.font_color;
			//var DataType = Objfrm.elements[ObjOrder].name;
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
		
	}
	/***** validate phone end *****/
	
	/***** validate zip start *****/
	this.CheckZip = function (Objfrm,ObjOrder,ObjValue,DataType){
		
		if(!ObjValue.match(this.format_zip)){
			//Objfrm.elements[ObjOrder].style.backgroundColor=this.errorcolor;
			//var DataType = Objfrm.elements[ObjOrder].name;
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
		
	}
	/***** validate zip end *****/
	
	/***** validate zip start *****/
	this.CheckYear = function (Objfrm,ObjOrder,ObjValue,DataType){
		
		if(!ObjValue.match(this.format_year)){
			//Objfrm.elements[ObjOrder].style.backgroundColor=this.errorcolor;
			//var DataType = Objfrm.elements[ObjOrder].name;
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}else if( parseInt(ObjValue) <1900 ){
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
		
	}
	/***** validate zip end *****/
	
	/***** validate img extension ******/
	this.CheckEx = function (Objfrm,ObjOrder,ObjValue,DataType){
		var imgArr = new Array( 'jpg', 'jpeg', 'gif', 'png' );
		var cmpD = imgArr.join(",");
		var cmpS = ObjValue.split(".");
		var extensionN = cmpS[cmpS.length-1];
		if( cmpD.match(extensionN) == null ){
			this.errmsg += "- Upload <strong>"+ DataType +"</strong> Failed, Please Select .jpg .jpeg .gif .png File Only <br>";
		}
	}
	/***** validate img extension ******/
	
	/***** validate URL ******/
	this.CheckURL = function (Objfrm,ObjOrder,ObjValue,DataType){
		var strUrl = /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i;
		if( !ObjValue.match(strUrl) ){
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
	}
	/***** validate img extension ******/
	
	/***** validate Price ******/
	this.CheckPrice = function (Objfrm,ObjOrder,ObjValue,DataType){
		var strPrice = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
		if( !ObjValue.match(strPrice) ){
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
	}
	/***** validate img extension ******/
	
	/***** validate Price ******/
	this.CheckNumeric = function (Objfrm,ObjOrder,ObjValue,DataType){
		var strNumeric = /^[1-9]{1}[0-9]*$/;
		if( !ObjValue.match(strNumeric) ){
			this.errmsg += "- Please Input Correct Format of <strong>"+ DataType +"</strong> <br>";
		}
	}
	/***** validate img extension ******/
}


function ValidateForm(objfrm)
{
	var obj=new ObjAttribute();
	var objErr=$("ValidateArea");
	if(!obj.validate(objfrm,objErr)){
		return false;
	}else{
		return true;
	}
}

function ValidateForm2(objfrm)
{
	var obj=new ObjAttribute();
	var objErr=$("ValidateArea");
	if(!obj.validate(objfrm,objErr)){
		return false;
	}else{
		
		if( $("password").value != $("password2").value ){
			objErr.style.display="";
			objErr.innerHTML = "Sorry, we cannot complete your request.<br />Kindly provide us the missing or incorrect information enclosed below.<br /><br /><strong>- Confirm Password should be the same with Password <br />";
			return false;
		}else{
			return true;
		}
	}
}
/********** Validate Login Details ************/






/************ Other Functions ************/


//check all
function checkall(objForm)
{
	var len = objForm.elements.length;
	for(var i=0;i<len;i++){
		if (objForm.elements[i].type=='checkbox'){
			objForm.elements[i].checked=objForm.check_all.checked;
		}
	}
}

//clear all the object in the form
function searchFormClear(objForm, selectStr)
{
	var len = objForm.elements.length;
	
	if( selectStr!="" ){
		selectArr = selectStr.split(",");
	}
	
	for(var i=0;i<len;i++){
		if(objForm.elements[i].type=='checkbox'){
			objForm.elements[i].checked = false;
		}
		if(objForm.elements[i].type=='text'){
			objForm.elements[i].value = "";
		}
		if(objForm.elements[i].type=='radio'){
			objForm.elements[i].checked = false;
		}
		if( selectStr!="" ){
			for(var q=0; q< selectArr.length; q++){
				if(objForm.elements[i].id == selectArr[q]){
					objForm.elements[i].value = "";
				}
			}
		}
	}
}

//if it is checked
function ifChecked(objForm)
{
	var len = objForm.elements.length;
	var checked_target=0;
	for(var i=0;i<len;i++){
		if((objForm.elements[i].type=='checkbox')&&(objForm.elements[i].disabled==false)&&(objForm.elements[i].checked==true)&&(objForm.elements[i].name!='check_all')){
			checked_target++;
		}
	}
	if(checked_target==0){
		return false;
	}else{
		return true;
	}
}

//delete form submit
function deleteFormSubmit(objfrm)
{
	var Objerr = $("jsAlert");	
	if(ifChecked(objfrm)){
		Objerr.style.display = "none";
		objfrm.submit();
		return true;
	}else{
		Objerr.style.display = "";
		Objerr.innerHTML = "Please Select the Checkbox";
		return false;
	}
}

//submit into popupwindow
function popFormSubmit(objfrm, submitPage, popup)
{
	var Objerr = $("jsAlert");
	if(ifChecked(objfrm)){
		Objerr.style.display = "none";
		objfrm.action = submitPage;
		if( popup == 'yes' ){
			objfrm.target="popup";
			objfrm.method="post";
			var height = 480;
			var width = 600;
			var left = (document.body.clientWidth - width)/2;
			var top = (document.body.clientHeight - height)/2;
			var win=window.open("","popup","height="+height+",width="+width+",top="+top+",left="+left);
			win.focus();
		}else{
			objfrm.target="_self";
			objfrm.method="post";
		}
		objfrm.submit();
		return true;
	}else{
		Objerr.style.display = "";
		Objerr.innerHTML = "Please Select the Checkbox";
		return false;
	}
}

//display special div ( used when change the dropdown list "Group Type" )
function alertOptionDiv(trID, objvalue)
{
	if( objvalue == "" ){
		return;
	}else{
		var Objerr = $("errorPosition")
		Objerr.style.display="none";
				
		var objTr = $_$("tr");
		var tableOrder = 1;	//defaul table order;
		for(var i=0;i<objTr.length;i++)
		{
			if( objTr[i].id == trID ){
				objTr[i].style.display="";
				var re = /\[\:number\:\]/g;		//replace current operating table
				var newTemplateValue = $_value(objvalue+"_odiv").replace(re, tableOrder);
				$("tableOrder").value = tableOrder;		//define the table orders.
				objTr[i].cells[0].innerHTML = newTemplateValue;
				tableOrder++;
			}
		}		
	}
}

//display special div ( used when change the dropdown list "Group Type" )
function alertOptionDiv2(trID, objvalue)
{
	
	var old_value = $_value("old_gtype");
	if( objvalue == "" ){
		return;
	}else{
		if( old_value == objvalue ){
			$("alertConfirmPosition").style.display="none";
		}else{
			$("alertConfirmPosition").style.display="";
		}
		var Objerr = $("errorPosition")
		Objerr.style.display="none";
				
		var objTr = $_$("tr");
		var tableOrder = 1;	//defaul table order;
		for(var i=0;i<objTr.length;i++)
		{
			if( objTr[i].id == trID ){
				var re = /\[\:number\:\]/g;		//replace current operating table
				var newTemplateValue = $_value(objvalue+"_odiv").replace(re, tableOrder);
				$("tableOrder").value = tableOrder;		//define the table orders.
				objTr[i].cells[0].innerHTML = newTemplateValue;
				tableOrder++;
			}
		}		
	}
}

//add special div ( used when clicking "Add an Option" )
function addOptionDiv(tableDIV, optionType)
{
	if( $_value(optionType) == ""){
		var Objerr = $("errorPosition")
		Objerr.style.display="";
		var CellPosition = Objerr.cells[0];
		CellPosition.innerHTML="Please Select an Option Group";
	}else{
		var tableOBJ = $(tableDIV);
		var old_template = $_value($_value(optionType)+"_odiv");	//fetch current template
		var current_table_order = parseInt($_value("tableOrder"))+1;		//fetch the current # order of group table
		var re = /\[\:number\:\]/g;
		var new_template = old_template.replace(re, current_table_order);	//replace tag with current # order of group table
		$("tableOrder").value = current_table_order;
		var rows_length = tableOBJ.rows.length;
		var newOTr = tableOBJ.insertRow(rows_length-1);
		newOTr.align = 'center';
		newOTr.id = 'optionTrDiv';
		var newOTd = newOTr.insertCell(0);
		newOTd.colSpan = "2";
		newOTd.bgColor = "#FFFFFF";
		newOTd.innerHTML = new_template;	//insert the table template into new TD.
	}
}

//open new window ( Used when clicking the button "Add a condition logic" )
function openNewWindow(openWindowParameter, tableOrderNumber)
{
	$("currentTableClick").value = tableOrderNumber;
	var winOBJ = "conditionLogicEdit.php?"+openWindowParameter;
	var win = window.open(winOBJ,"popup","height=601,width=1010,top=210,left=110,scrollbars=1");
	win.focus();
}

//validate the form ( Used in openWindow )
function validateFormJavascript(objfrm)
{
	var obj = $_$("input");
	var checked_count = 0;
	var parentDivID = $_value("tableOrderNumber");
	
	for(i=0;i<obj.length;i++)
	{
		if( obj[i].type == "checkbox" ){
			if( obj[i].checked == true ){
				checked_count++;
			}
		}
	}
	
	if( checked_count == 0 && checked_number_count == 0 ){
		var errmsg="Please Select the Checkbox<br>";
		var Objerr = $("errorPosition");
		Objerr.style.display="";
		var CellPosition = Objerr.cells[0];
		CellPosition.innerHTML=errmsg;
		return false;
	}else{
		var Objerr = $("errorPosition");
		Objerr.style.display="none";
		showLogicDiv(parentDivID);
		window.close();
		return false;
	}
}

//add the current logic into DIV ( Used when click the button "Save Condition logic(s)" in the PopUpWindow )
function showLogicDiv(tableID)
{
	window.opener.document.getElementById("conditionlogic_"+tableID).style.display="";
	var objTable = window.opener.document.getElementById("optionT_"+tableID);
	var tableRows = objTable.rows.length;
	var tmp_array = new Array();	//just used for deleting the old rows
	
	//search the new insert Row
	for(var i=0;i<tableRows;i++)
	{
		if( objTable.rows[i].id == "NewTrID" ){
			tmp_array.push(i);
		}
	}
	//delete the old Row
	for(var i=0,j=0;i<tmp_array.length;i++)
	{
		objTable.deleteRow(tmp_array[i]-j);
		j++;
	}
	
	
	var post_hidden_logic = "";
	var tmp_hidden_logic = "";
	
	var inputsArr = $_$("input");
	for(var i=0,k=0;i<inputsArr.length;i++)
	{
		if( inputsArr[i].checked == true ){
			k++;
			var logic_checkbox_arr = inputsArr[i].value;
			
			if( inputsArr[i].name == "category[]" ){
				TD1_ID = logic_checkbox_arr.split("[:innerTAG:]")[0];
				TD02_NAME = logic_checkbox_arr.split("[:innerTAG:]")[1];
				TD1 = "<strong>Category:</strong>";
				TD2 = TD02_NAME;
				
				if( post_hidden_logic == "" ){	//it is used when save the logic into the database
					post_hidden_logic = TD1_ID;
				}else{
					post_hidden_logic += "[:CategoryTAG:]"+TD1_ID;
				}
			}
			
			if( inputsArr[i].name == "question[]" ){
				TD1_ID = logic_checkbox_arr.split("[:externalTAG:]")[0].split("[:innerTAG:]")[0];
				TD1_NAME = logic_checkbox_arr.split("[:externalTAG:]")[0].split("[:innerTAG:]")[1];
				TD2_ID = logic_checkbox_arr.split("[:externalTAG:]")[1].split("[:innerTAG:]")[0];
				TD2_NAME = logic_checkbox_arr.split("[:externalTAG:]")[1].split("[:innerTAG:]")[1];
				TD1 = "<strong>Question:</strong>";
				TD2 = "<strong>"+TD1_NAME+" &raquo; </strong>"+TD2_NAME;
				if( post_hidden_logic == "" ){	//it is used when save the logic into the database
					post_hidden_logic = TD1_ID+"_"+TD2_ID;
				}else{
					post_hidden_logic += "[:CategoryTAG:]"+TD1_ID+"_"+TD2_ID;
				}
			}
			if( tmp_hidden_logic == "" ){	//it is used when save the logic into the database
				tmp_hidden_logic = logic_checkbox_arr;
			}else{
				tmp_hidden_logic += "[:CategoryTAG:]"+logic_checkbox_arr;
			}
			
			window.opener.document.getElementById("PostHiddenLogic_"+tableID).value = post_hidden_logic;
			window.opener.document.getElementById("PostHiddenLogicTrigger_"+tableID).value = $_value("function_when");
			window.opener.document.getElementById("HiddenLogicValue_"+tableID).value = tmp_hidden_logic;
			
			insertLogicTable(objTable, TD1, TD2);
		}
	}
	if( k == 0 ){
		window.opener.document.getElementById("PostHiddenLogic_"+tableID).value = "";
		window.opener.document.getElementById("PostHiddenLogicTrigger_"+tableID).value = "";
		window.opener.document.getElementById("HiddenLogicValue_"+tableID).value = "";
	}
	
}

//insert into a new table
function insertLogicTable(objTable, TD1, TD2)
{
	var tableRows_new = objTable.rows.length;
	var newTr = objTable.insertRow(tableRows_new-1);
	newTr.align = 'center';
	newTr.bgColor = '#FFFFFF';
	newTr.id = 'NewTrID';	//get ready for modifying for the next time
	
	var newTd0 = newTr.insertCell(0);
	newTd0.align = 'right';
	newTd0.innerHTML = TD1;
	var newTd1 = newTr.insertCell(1);
	newTd1.align = 'left';
	newTd1.innerHTML = TD2;
}

//disabled the sub checkbox of category ( used when click the category checkbox in popupwindow )
function disableOpenClose(categoryOBJ)
{
	var categoryValue = categoryOBJ.value;
	var inputARR = $_$("input");
	for( var i=0; i<inputARR.length; i++ )
	{
		if( inputARR[i].type == "checkbox" && inputARR[i].name == "question[]" && inputARR[i].value.split("[:externalTAG:]")[0] == categoryValue ){
			if( categoryOBJ.checked == true ){
				inputARR[i].disabled = true;
				inputARR[i].checked = false;
			}else{
				inputARR[i].disabled = false;
			}
		}
	}
}

//it used in the popupWindow
function initPagePrint()
{
	var tableOrderNumber = window.opener.document.getElementById("currentTableClick").value;	//get the table order
	$("tableOrderNumber").value = tableOrderNumber;
	var binding_logic = window.opener.document.getElementById("HiddenLogicValue_"+tableOrderNumber).value;
	var PostHiddenLogicTrigger = window.opener.document.getElementById("PostHiddenLogicTrigger_"+tableOrderNumber).value;
	
	//make the default checkbox checked
	var binding_logic_arr = binding_logic.split("[:CategoryTAG:]");
	var inputs_arr = $_$("input");
	for(var i=0; i<inputs_arr.length; i++)
	{
		if( inputs_arr[i].type == "checkbox" ){
			for(var j=0; j<binding_logic_arr.length; j++)
			{
				if( inputs_arr[i].value == binding_logic_arr[j] ){
					inputs_arr[i].checked = true;
					checked_number_count++;	//the numbers of the checked checkbox; It is a golbal variable
				}
			}
		}
	}
	
	//disable the checkbox if the category has been checked
	for(var i=0; i<inputs_arr.length; i++)
	{
		if( inputs_arr[i].type == "checkbox" && inputs_arr[i].name == "category[]" && inputs_arr[i].checked == true ){
			var category_value_id = inputs_arr[i].value;
			for(var j=0; j<inputs_arr.length; j++)
			{
				if( inputs_arr[j].type == "checkbox" && inputs_arr[j].name == "question[]" ){
					if( inputs_arr[j].value.split("[:externalTAG:]")[0] == category_value_id )
					{
						inputs_arr[j].checked = false;
						inputs_arr[j].disabled = true;
						checked_number_count--;	//the numbers of the checked checkbox; It is a golbal variable
					}
				}
			}
			checked_number_count++;
		}
	}
		
	//make the div display	
	PostHiddenLogicTrigger = (PostHiddenLogicTrigger==""?1:PostHiddenLogicTrigger);	//default is "checked"
	
	
	
	//make the dropdown checked
	$("function_when").value = PostHiddenLogicTrigger;
	
}

//fck
function addAFck(objID)
{
	var objName = $(objID).name;
		
	var FCK = new FCKeditor(objName);
	FCK.BasePath = "../fckeditor/";
	FCK.Height = 350;
	FCK.ToolbarSet = "Default"; 
	FCK.ReplaceTextarea();
}

function fillSPI(objChecked)
{
	var array_fields = new Array( "firstname", "lastname", "address", "address2", "city", "state", "zip", "phone");//bill fields	
	var stateOBJ = $("data[state]");	//original object of state
	var bill_stateOBJ = $("data[bill_state]");
	
	for(var i=0;i<array_fields.length;i++){
		var objValue = $_value("data["+array_fields[i]+"]");
		var objBillValue = $_value("data[bill_"+array_fields[i]+"]");
		if( objChecked == true ){
			$("data[bill_"+array_fields[i]+"]").value = objValue;
			$("data[bill_"+array_fields[i]+"]").readOnly = true;
			
			if( array_fields[i] == "state" ){
				bill_stateOBJ.length = 0;
				if( objValue == "" ){
					addOption(bill_stateOBJ, "-------- Select --------", objValue);	//set the objValue to be the ONLY value of state
				}else{
					addOption(bill_stateOBJ, objValue, objValue);	//set the objValue to be the ONLY value of state
				}
			}
			
		}else{
			$("data[bill_"+array_fields[i]+"]").readOnly = false;
			if( array_fields[i] == "state" ){				
				bill_stateOBJ.length = 0;
				addOption(bill_stateOBJ, "-------- Select --------", "");
				for( var k=1;k<stateOBJ.options.length;k++ ){
					addOption(bill_stateOBJ, stateOBJ.options[k].value, stateOBJ.options[k].value);	//reset the original value of state
				}
				$("data[bill_"+array_fields[i]+"]").value = objValue;
			}
			
		}
	}
}

function fillREC(objChecked)
{
	var array_fields = new Array( "firstname", "lastname", "address", "address2", "city", "state", "zip", "phone");//bill fields	
	var stateOBJ = $("data[state]");	//original object of state
	var recipients_stateOBJ = $("data[recipients_state]");
	
	for(var i=0;i<array_fields.length;i++){
		var objValue = $_value("data["+array_fields[i]+"]");
		var objBillValue = $_value("data[recipients_"+array_fields[i]+"]");
		if( objChecked == true ){
			$("data[recipients_"+array_fields[i]+"]").value = objValue;
			$("data[recipients_"+array_fields[i]+"]").readOnly = true;
			
			if( array_fields[i] == "state" ){
				recipients_stateOBJ.length = 0;
				if( objValue == "" ){
					addOption(recipients_stateOBJ, "-------- Select --------", objValue);	//set the objValue to be the ONLY value of state
				}else{
					addOption(recipients_stateOBJ, objValue, objValue);	//set the objValue to be the ONLY value of state
				}
			}
			
		}else{
			$("data[recipients_"+array_fields[i]+"]").readOnly = false;
			if( array_fields[i] == "state" ){				
				recipients_stateOBJ.length = 0;
				addOption(recipients_stateOBJ, "-------- Select --------", "");
				for( var k=1;k<stateOBJ.options.length;k++ ){
					addOption(recipients_stateOBJ, stateOBJ.options[k].value, stateOBJ.options[k].value);	//reset the original value of state
				}
				$("data[recipients_"+array_fields[i]+"]").value = objValue;
			}
			
		}
	}
}

//add options
function addOption(selectObj, optionText, optionValue)
{
	var moPtion=document.createElement("OPTION");
	moPtion.innerText=optionText;
	moPtion.text=optionText;
	moPtion.value=optionValue;
	selectObj.appendChild(moPtion);
}

//change line's color automaticlly
/*
colorOne: the first color which is ready to change
colorTwo: the second color which is ready to change
tableID: the ID of table
startLine: the real line count in table where we start change color
*/
function changeTrColor(colorOne, colorTwo, tableID, startLine)
{
	var tableOBJ = $(tableID);
	var trArr = tableOBJ.getElementsByTagName("tr");
	for( var i = (parseInt(startLine)-1); i<trArr.length; i ++ )
	{
		var trOBJ = trArr[i];
		var tdArr = trOBJ.getElementsByTagName("td");
		if(trOBJ.style.display == 'none')
		{
			continue;
		}
		var bgColorDefault = (bgColorDefault == colorOne?colorTwo:colorOne);
		for( var j=0; j<tdArr.length; j++ )
		{
			tdArr[j].bgColor = bgColorDefault;
		}
	}
}

/*
Print Function
*/
function PrintResult(newWindow)
{
	var height = 480;
	var width = 600;
	var left = (document.body.clientWidth - width)/2;
	var top = (document.body.clientHeight - height)/2;
	var win=window.open(newWindow,"popup","height="+height+",width="+width+",top="+top+",left="+left+", toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=yes, status=no");
	win.focus();
}


/*
Print PDF
*/
function PrintPDF(newWindow)
{
	window.location.href=newWindow;
	location.target='_blank';
}

/////////////// FOLLOWING FUNCTIONS ARE NOT USED NOW => START /////////////////

//check if the value is null
function checkUnNull(ObjValue)
{
	if( ObjValue == "" ){
		return false;
	}else{
		return true;
	}
}

//validate Email/Telephone/Zip
function checkValues(ObjValue, status)
{
	var format_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;	//Format of Email
	var format_phone = /^\d{3}-\d{3}-\d{4}-(|\d{1,6})$/;	//Format of USA Tel
	var format_zip = /^\d{5}$/;	//zip format
	
	var chkOriginal = "";
	
	if( status == "email" ){
		chkOriginal = format_email;
	}
	
	if( status == "telephone" ){
		chkOriginal = format_phone;
	}
	
	if( status == "zip" ){
		chkOriginal = format_zip;
	}
	
	//Start checking
	if( chkOriginal == "" || ObjValue == "" ){
		return false;
	}else{
		if(!ObjValue.match(chkOriginal)){
			return false;
		}else{
			return true;	
		}
	}
}

//validate Forms
function validateLogin(formTag)
{
	
	var originalErrmsg = 'Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n';	//default error message
	
	if( formTag == "backendLogin" ){
		var username = $_value("username");
		var passwd = $_value("passwd");
		if( !checkUnNull(username) || !checkUnNull(passwd) ){
			originalErrmsg += "- Please Enter Login Details.\n";
			alert(originalErrmsg);
			return false;
		}
	}
}

//alert logic div
function alertLogicDiv(logicMethod)
{
	var method_string = logicMethod;
	$("method_"+method_string).style.display = "";
	if(method_string == "category" ){
		$("method_question").style.display = "none";
	}else if(method_string == "question" ){
		$("method_category").style.display = "none";
	}
}

///////////////  FOLLOWING FUNCTIONS ARE NOT USED NOW <= END /////////////////

/************ Other Functions ************/
