// JavaScript Document
function getStats(fName){
				
	fullName = fName;
	shortName = fullName.match(/[^\/\\]+$/);
	splitName = fullName.split(".");
	fileType = splitName[1];
	fileType = fileType.toLowerCase();
	if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')
	{
		checkIt = fileSize(fullName);
		if (parseInt(checkIt) > 5*1024*1024){
			alert('Max File Size is 3MB');}
		} 
	else {
		alert("You must select an image file!");						
	}
}
function fileSize(fullName)
{
	var oas = new ActiveXObject("Scripting.FileSystemObject");
	var d = fullName;
	var e = oas.getFile(d);
	return e.size;
}

function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
	datefield.select();
	alert("That date is invalid.  Please try again.");
	datefield.focus();
	return false;
	}
	else {
	return true;
	   }
}
function chkdate(objName) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
	return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
	strDateArray = strDate.split(strSeparatorArray[intElementNr]);
	if (strDateArray.length != 3) {
	err = 1;
	return false;
	}
	else {
	strDay = strDateArray[0];
	strMonth = strDateArray[1];
	strYear = strDateArray[2];
	}
	booFound = true;
	   }
	}
	if (booFound == false) {
	if (strDate.length>5) {
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(2, 2);
	strYear = strDate.substr(4);
	   }
	}
	if (strYear.length == 2) {
	strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
	err = 2;
	return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
	for (i = 0;i<12;i++) {
	if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
	intMonth = i+1;
	strMonth = strMonthArray[i];
	i = 12;
	   }
	}
	if (isNaN(intMonth)) {
	err = 3;
	return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
	err = 4;
	return false;
	}
	if (intMonth>12 || intMonth<1) {
	err = 5;
	return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
	err = 6;
	return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
	err = 7;
	return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
	err = 8;
	return false;
	}
	if (LeapYear(intYear) == true) {
	if (intday > 29) {
	err = 9;
	return false;
	}
	}
	else {
	if (intday > 28) {
	err = 10;
	return false;
	}
	}
	}
	if (strDatestyle == "US") {
	datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
	datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
	}
	function LeapYear(intYear) {
	if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { return true; }
	}
	else {
	if ((intYear % 4) == 0) { return true; }
	}
	return false;
	}
	function doDateCheck(from, to) {
	if (Date.parse(from.value) <= Date.parse(to.value)) {
	alert("The dates are valid.");
	}
	else {
	if (from.value == "" || to.value == "") 
	alert("Both dates must be entered.");
	else 
	alert("To date must occur after the from date.");
	   }
}

function fieldSort(frmname,action,fldname){
	var f = document.forms[frmname];
	f.fsort.value=fldname;
	f.action=action;
	update_del_arr();
	f.submit();

}


function setRequire(frmname,fldname)
{
	if(document.forms[frmname].elements[fldname].value==''){
		alert('you have to enter ' + fldname);
		document.forms[frmname].elements[fldname].focus();
		return false;
	}
	else{
		return true;	
	}
	
}
function DateCombo(cboname,numopt,selected)
{
	var option_list = '<select name='+cboname+'>';
	
	for(i=1;i<=numopt;i++)
	{
		if(selected == i){
			option_list = option_list + "<option value='"+i+"' selected='selected'>"+i+"</option>";
		}
		else{
			option_list = option_list + "<option value='"+i+"'>"+i+"</option>";
		}
		
	}
	 option_list = option_list + '</select>';   
	return option_list;
}
//function to block input character only number
//attribute oncontextmenu='return false;' will block right mouse button
function blocktext(e) {
	var key;
	var keychar;
	var reg;
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 

	}
	else if(e.which) {
		// netscape
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}
	keychar = String.fromCharCode(key);
	//alert(keychar);
	reg = /[\d\b]/;
	return reg.test(keychar);
}
function check_all() {
	var f = document.frmList;
	var value=f.chkMaster.checked;
	var obj = f.elements["checked_delete[]"];
	if (obj) 
	{
		if (obj.length) 
			for (i=0;i<obj.length;i++)
				obj[i].checked = value;
		else
			obj.checked = value;
	}
}
function update_all()
{
	document.frmList.submit_type.value="update";
	document.frmList.submit();
}
function del_all()
{
	var f = document.frmList;
	var obj = f.elements["checked_delete[]"];
	if (obj) 
	{
		arr_id = new Array();
		if(f.del_arr.value!=''){
			str_id = f.del_arr.value;
			arr_id = str_id.split("~");//split string id to array
		}
if(!obj.length)
  {
   bool = arr_id.inArray(obj.value);
   if(!bool && obj.checked==true){
    arr_id.push(obj.value);
   }
   else if(bool && obj.checked==false){
    arr_id.remove(obj.value);
   }
   
  }		
		for (j=0;j<obj.length;j++)
		{ 
			//check if not exist in checked array => add element			
			bool = arr_id.inArray(obj[j].value);
			if(!bool && obj[j].checked==true){
				arr_id.push(obj[j].value);
			}
			if(bool && obj[j].checked==false){
				arr_id.remove(obj[j].value);
			}
		}
		str_temp = arr_id.join('~');
		f.del_arr.value = str_temp;
	}
	
//	if(del_string.length>0 )
	if(f.del_arr.value !='')
	{
		if(confirm('Are you sure?')){
			f.submit();
		}
	}
	else{
		alert('桼򤷤Ƥ');
	}
}


function set_boolean(id,type,field)
{
	var f = document.frmList;
	f.bool_id.value=id;
	f.bool_type.value=type;
	f.bool_field.value=field;
	f.submit();
}
function openwin(url,w_title,width,height)
{
	var maxw = window.screen.availWidth;
	var maxh = window.screen.availHeight;
	var win = window.open(url,w_title,'screenX=0,screenY=0,left='+((maxw-width)/2)+',top=0,width=' + width + ',height=' + height+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
		win.focus();
}
function delete_one(del_id)
{
	var f = document.frmList;
	f.action="confirm.php?del_id="+del_id;
	f.submit();
}
function edit_one(edit_id)
{
	var f = document.frmList;
	f.action="action.php?edit_id="+edit_id;
	f.submit();
}
function add_new()
{
	var f = document.frmList;
	f.action="action.php";
	f.submit();
}
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
Array.prototype.inArray = function (value){
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

Array.prototype.remove=function(s){
  var i;
  for(i=0;i<this .length;i++){
    if(s===this[i]) this.splice(i, 1);
  }
}
function update_del_arr()
{
	var f = document.frmList;
	var obj = f.elements["checked_delete[]"];
	if (obj) 
	{
		if(f.del_arr.value!=''){
			str_id = f.del_arr.value;
			arr_id = str_id.split("~");//split string id to array
		}
		else{
			arr_id = new Array();
		}
		
		if(!obj.length)
		{
			bool = arr_id.inArray(obj.value);
			if(!bool && obj.checked==true){
				arr_id.push(obj.value);
			}
			if(bool && obj.checked==false){
				arr_id.remove(obj.value);
			}
		}
		for (j=0;j<obj.length;j++)
		{
			//check if not exist in checked array => add element			
			bool = arr_id.inArray(obj[j].value);
			if(!bool && obj[j].checked==true){
				arr_id.push(obj[j].value);
			}
			if(bool && obj[j].checked==false){
				arr_id.remove(obj[j].value);
			}
		}
		str_temp = arr_id.join('~');
		f.del_arr.value = str_temp;

	}
}

function navigate(i)
{
	var f = document.frmList;
	//update_del_arr();
	f.page.value=i;
	//document.getElementById('page').value = i;
	f.submit();
}

function navigate_admin(i)
{
	clearCheckedDelete();

	navigate(i);
}

function dupplicate(dup_id)
{
	var f = document.frmList;
	f.action="action.php?dup_id="+dup_id;
	f.submit();
}

<!--Check Control-->
function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
   	return true;
 	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
     var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
     if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function V2validateData(strValidateStr,objValue,strError) 
{ 
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
           if(eval(objValue.value.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         }//case required 
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alpha-numeric characters allowed "; 
                }//if 
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
		   
		case "fullsize": 
        case "fullsizekana": 
           { //"^(\xA5[\xA1-\xF6])+$" for EUC-JP
/*              for(i=0; i<objValue.value.length; i++){
				if((objValue.value.charAt(i) != '') && (objValue.value.charAt(i) < '' || objValue.value.charAt(i) > '')){
				   if(!strError || strError.length ==0) 
					{ 
					  strError = objValue.name+": Only fullsize katakana allowed "; 
					}//if 
					alert(strError + "\n [Error character position " + eval(i+1)+"]"); 
					return false; 
				}
			  }              
*/              break; 
           }//case fullsize 
		   
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only digits allowed "; 
                }//if               
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break;               
           }//numeric 
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alpha 
  case "alnumhyphen":
   {
              var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if    
   			break;
   }
   case "numberdash":
   {
              var charpos = objValue.value.search("[^0-9\-]"); 
			  
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are 0-9,-"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if    
   break;
   }
        case "email": 
          { 
               if(!validateEmailv2(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Enter a valid Email address "; 
                 }//if                                               
                 alert(strError); 
                 return false; 
               }//if 
           break; 
          }//case email 
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }//if               
               alert(strError); 
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
    if(objValue.value.length > 0)
   {
             if(!objValue.value.match(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name+": Invalid characters found "; 
               }//if                                                               
               alert(strError); 
               return false;                   
             }//if 
   }
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Please Select one option "; 
              }//if                                                               
              alert(strError); 
              return false;                                   
             } 
             break; 
         }//case dontselect 
    }//switch 
    return true; 
}

function doSelect(obj)
{
  var f=document.frmList;
  var strIdCheckbox="";
  strIdCheckbox=f.txtArrId.value;
  if(obj.checked)
  {
	if(strIdCheckbox=="")
		strIdCheckbox+=obj.value;
	else
		strIdCheckbox+="-"+obj.value;
  }else{
	strIdCheckbox=strIdCheckbox.replace("-"+obj.value,"");
	strIdCheckbox=strIdCheckbox.replace(obj.value+"-","");
	strIdCheckbox=strIdCheckbox.replace(obj.value,"");
  } 
  f.txtArrId.value=strIdCheckbox;
}

function doCheckAll(checked) {
  if (!document.frmList["checked_delete[]"]) {
    return;
  }
  if (document.frmList["checked_delete[]"].length) {
    for(i=0; i<document.frmList["checked_delete[]"].length; i++){
      document.frmList["checked_delete[]"][i].checked = checked;
      doSelect(document.frmList["checked_delete[]"][i]);
    }
  }
  else {
    if (document.frmList["checked_delete[]"]) {
      document.frmList["checked_delete[]"].checked = checked;
      doSelect(document.frmList["checked_delete[]"]);
    }
  }
}

function doDetailSearchCheckAll(checked) {

	document.frmList["conditionRecommended"].checked = checked;
	
	document.frmList["conditionAirConditioner"].checked = checked;
	document.frmList["conditionElevator"].checked = checked;
	document.frmList["conditionParking"].checked = checked;
	document.frmList["conditionSlope"].checked = checked;
	document.frmList["conditionExhaustFan"].checked = checked;

	document.frmList["conditionLeisure"].checked = checked;
	document.frmList["conditionMusicalInstruments"].checked = checked;
	document.frmList["conditionFurniture"].checked = checked;
	document.frmList["conditionBook"].checked = checked;
	document.frmList["conditionWine"].checked = checked;
	document.frmList["conditionValuables"].checked = checked;
	document.frmList["conditionMotorcycle"].checked = checked;
	
	document.frmList["conditionTransportation"].checked = checked;
	document.frmList["condition24hours"].checked = checked;
	document.frmList["conditionCampaign"].checked = checked;
	document.frmList["conditionSecurity"].checked = checked;
	document.frmList["conditionAirConditionService"].checked = checked;
}

function clearCheckedDelete()
{
	var f = document.frmList;
	if (!f) {
		return;
	}

  if (!document.frmList["checked_delete[]"]) {
    return;
  }
  if (document.frmList["checked_delete[]"].length) {
    for(i=0; i<document.frmList["checked_delete[]"].length; i++){
      document.frmList["checked_delete[]"][i].checked = false;
    }
  }

	if (f) {
		if (f.txtArrId) {
			f.txtArrId.value = '';
		}
	}
}

