function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,aa,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { 
  	test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=args[i+1]; 
   	   if (test=='RO'){
	       aa = args[i];
	 		p = document.all[aa].options[document.all[aa].selectedIndex].value;
			if (p=="0"){errors+='- '+nm+' must select.\n';}
		}else{		
		if ((val=val.value)!="") {
     	 if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
       	 if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      	} else if (test!='R') { num = parseFloat(val);
        			if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
       				if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          					min=test.substring(8,p); max=test.substring(p+1);
         					 if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
   		 } 
		} 
	} else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} //
  }
  
   if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


function MM_validateFormNoPopup() { //v4.0
  var i,p,q,nm,test,num,min,max,aa,errors='',args=MM_validateFormNoPopup.arguments;
  var valBoolean;
  for (i=0; i<(args.length-2); i+=3) { 
  	test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=args[i+1]; 
   	   if (test=='RO'){
	       aa = args[i];
	 		p = document.all[aa].options[document.all[aa].selectedIndex].value;
			if ((p=="0") || (p=="") || (p=="-1")){errors+='- '+nm+' has to be selected.\n';}
		}else{		
		if ((val=val.value)!="") {
     	 if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
       	 if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      	} else if (test!='R') { num = parseFloat(val);
        			if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
       				if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          					min=test.substring(8,p); max=test.substring(p+1);
         					 if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
   		 } 
		} 
	} else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} //
  }
  
    valBoolean = true;
   if (errors) { 
	 	alert('The following error(s) occurred:\n'+errors);
		valBoolean = false;
	}
  document.MM_returnValue = valBoolean;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*
when click button for go to URL and send data
*/
function goURL(theform,URL){
	theform.action = URL;
	theform.submit();
	
}

/*
when click button for go to URL (not send data by method post)
*/
function goURLonly(URL){
	window.location = URL;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/*
confirm delete 
*/
function deleteItem(URL){
	if (confirm("Are you sure to delete?")){
		window.location = URL;
	}
}


/*
confirm cancel
*/
function cancelItem(URL){
	if (confirm("Are you sure to exit?")){
		window.location = URL;
	}
}

/*
go to URL 
*/
function ChangePage(URL)
{
window.location = URL;
}

/*
Show error
*/
function showerror(msg){
	alert(msg);
}
function makeSql(){// Send all fields name + values to make the SQL statement
		var tab = new Array();
		for (var i=0; i<(formSql.elements.length-2); i++){
			tab[i]=formSql.elements[i].name.substring(4)+'||'+formSql.elements[i].value+'||';
		}
		// test : alert(formSql.elements.length);
		// test : alert(tab[1]+tab[3]+tab[5]+tab[7]+tab[9]+tab[11]+tab[13]+tab[16]+tab[19]+tab[20]+tab[21]+tab[22]+tab[23]+tab[24]+tab[25]+tab[26]+tab[17]+tab[18]);
		formSql.ReqSql.value=tab[1]+tab[3]+tab[5]+tab[7]+tab[9]+tab[11]+tab[13]+tab[16]+tab[19]+tab[20]+tab[21]+tab[22]+tab[23]+tab[24]+tab[25]+tab[26]+tab[17]+tab[18];
		formSql.submit();
}

function txtBlock(nameCheckbox){ // Block text input if checkbox is unchecked
		if (nameCheckbox.checked==true){
			// Checkbox and textbox must have common name (eg. : chk_company & txt_company)
			eval('document.forms[0].txt_'+nameCheckbox.name.substring(4)+'.disabled=false');
			eval('document.forms[0].txt_'+nameCheckbox.name.substring(4)+'.value=\'\'');
			}
			else
			{
			eval('document.forms[0].txt_'+nameCheckbox.name.substring(4)+'.disabled=true');
			eval('document.forms[0].txt_'+nameCheckbox.name.substring(4)+'.value=\'DISABLED\'');
			}
}

/*
check checkbox=true if true then checked = true else false
*/
function chkValue(checkbox){
		if (checkbox.checked==true){
		checkbox.value='Yes'
		}
		else
		{
		checkbox.value='DISABLED'
		}
}

/*
when click button for sending data by post method
*/
function redemption(theform,id,url){
	theform.hdd_id.value = id;
	theform.action = url;
	theform.submit();
}

/*
show date and time in English version

*/
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var montharray=new Array("January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December");

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=12)
dn="PM"
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='000080' face='Verdana' font size='1'>"+dayarray[day]+", "+daym+" "+montharray[month]+", "+year+" "+hours+":"+minutes+":"+seconds+" "+dn+"</font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}

/*
Make groupmail stream and send 
*/
function goGroup(){
	var list_g = "";
	for (var i=0; i<document.MAILFORM.groupmail.length; i++){
		if (document.MAILFORM.groupmail.options[i].selected){
			if (list_g==""){
				list_g= document.MAILFORM.groupmail.options[i].value;
			}else{
				list_g= list_g + "," + document.MAILFORM.groupmail.options[i].value;
			}
		} // end 
	}
	document.pagetrack.hdd_group.value = list_g;
	//document.pagetrack.pagenumber.value=pagenumber;
	//document.pagetrack.orderby.value=order_by;
	document.pagetrack.submit();
}
/*
send idgroup to popup_group.asp by post method
*/
function changeGroup(theform,id){
	theform.hdd_group.value = id;
	theform.action = "popup_group.asp";
	theform.submit();
}
/*
make idcustomer stream and send to popup_group.asp by post method
*/
function changePageGroup(theform,idgroup,pageN){
	var member_id = document.all.hdd_mem.value;
	var member_temp=theform.mem_temp.value;
	var looprow = theform.numberRows.value;
	var temp_id = "";
	var temp_id_no = "";
	for (var i=1; i<=looprow; i++){
	// Make object
		boxC = eval("document.all.chk_" + i);
		// checked and make stream for chosen  boxes and not chosen boxes
		if (boxC.checked == true){
			if (temp_id==""){
				temp_id = boxC.value;
			}else{
				temp_id =temp_id + "," + boxC.value;
			} // end if
		}else{
			if (temp_id_no==""){
				temp_id_no = boxC.value;
			}else{
				temp_id_no =temp_id_no + "," + boxC.value;
			}
		
		} // end if
	}// end for
			// mix old stream and new stream
			if (member_id==""){
				member_id  = temp_id;
			}else{
				member_id = member_id + "," + temp_id;
			}

	// check indicate
	var list_temp = new Array();
	var check_same = true;
	var list_1 = member_id.split(",");
	var count_temp = 0;
	
	
	if (list_1.length>1){ 
		for(var i=0; i<list_1.length-1; i++){
			check_same = true;
			for (var j=i+1; j<list_1.length; j++){
					if (list_1[j] == list_1[i]){
						check_same = false;
						j = list_1.length;
					}
			}
			
			if (check_same==true){
				list_temp[count_temp] = list_1[i];
				count_temp++;
			}
		}
		
			list_temp[count_temp] = list_1[i];
		
	}else{
		list_temp = list_1;
	}

	
	var member_true = ""
	// check items not want
	var list_t = new Array();
	if (temp_id_no!=""){
	// itemp not empty
		var	list_no_want = temp_id_no.split(",");
		count_temp = 0;
		for (var aj=0; aj<list_temp.length; aj++){
			check_same = true;
			for (var bj=0; bj<list_no_want.length; bj++){
					if (list_no_want[bj] == list_temp[aj]){
						check_same = false;
						bj = list_no_want.length;
					}
			}
			
			if (check_same==true){
				list_t[count_temp] = list_temp[aj];
				count_temp++;
			}
		}
	}else{
		list_t = list_temp;
	}
//==== end item not empty=======
	
	for (var i=0; i<list_t.length; i++){
		if (member_true==""){
			if (list_t[i] != ""){member_true = list_t[i];}
		}else{
			if (list_t[i] != ""){member_true = member_true +","+list_t[i];}
		}
	}
	theform.hdd_mem.value = member_true;	
	theform.mem_temp.value = temp_id;
	theform.gPage.value = pageN;
	theform.hdd_group.value = idgroup;
	theform.action = "popup_group.asp";
	theform.submit();
}

/*
Make group
*/
function makegroup(theform){
	var member_id = document.all.hdd_mem.value;
	var member_temp=theform.mem_temp.value;
	var looprow = theform.numberRows.value;
	var temp_id = "";
	var temp_id_no = "";
	for (var i=1; i<=looprow; i++){
		boxC = eval("document.all.chk_" + i);
		if (boxC.checked == true){
			if (temp_id==""){
				temp_id = boxC.value;
			}else{
				temp_id =temp_id + "," + boxC.value;
			} // end if
		}else{
			if (temp_id_no==""){
				temp_id_no = boxC.value;
			}else{
				temp_id_no =temp_id_no + "," + boxC.value;
			}
		
		} // end if
	}// end for
	
			if (member_id==""){
				member_id  = temp_id;
			}else{
				member_id = member_id + "," + temp_id;
			}
	//alert(member_id + "= member_id");
	//alert(temp_id + "= temp_id");
	// check indicate
	var list_temp = new Array();
	var check_same = true;
	var list_1 = member_id.split(",");
	var count_temp = 0;
	
	
	if (list_1.length>1){ 
		for(var i=0; i<list_1.length-1; i++){
			check_same = true;
			for (var j=i+1; j<list_1.length; j++){
					if (list_1[j] == list_1[i]){
						check_same = false;
						j = list_1.length;
					}
			}
			
			if (check_same==true){
				list_temp[count_temp] = list_1[i];
				count_temp++;
			}
		}
		
			list_temp[count_temp] = list_1[i];
		
	}else{
		list_temp = list_1;
	}

	
	var member_true = ""
	// check items not want
	var list_t = new Array();
	if (temp_id_no!=""){
	// itemp not empty
		var	list_no_want = temp_id_no.split(",");
		count_temp = 0;
		for (var aj=0; aj<list_temp.length; aj++){
			check_same = true;
			for (var bj=0; bj<list_no_want.length; bj++){
					if (list_no_want[bj] == list_temp[aj]){
						check_same = false;
						bj = list_no_want.length;
					}
			}
			
			if (check_same==true){
				list_t[count_temp] = list_temp[aj];
				count_temp++;
			}
		}
	}else{
		list_t = list_temp;
	}
//==== end item not empty=======
	
		for (var i=0; i<list_t.length; i++){
			if (member_true==""){
				if (list_t[i] != ""){member_true = list_t[i];}
			}else{
				if (list_t[i] != ""){member_true = member_true +","+list_t[i];}
			}
		}
		theform.hdd_mem.value = member_true;
	//}
	var msgError = "";
		if (theform.txt_group.value == ""){
		 msgError = "Group name is required.\n";
		}
		//if (theform.hdd_mem.value == ""){
		//msgError = msgError+"Selected less one member.";
		//}
	
		if (msgError==""){
			theform.action = "popup_group_edit.asp";
			theform.submit();
		}else{ 
		msgError = "Error :\n" + msgError;
		alert(msgError);
		}
}

