// JavaScript Document

function trim(myString){
	if(myString.length > 0){
		while(myString.charAt(0) == " "){
			myString = myString.substring(1,myString.length);
		}
		
		if(myString.length > 0){
			while(myString.charAt(myString.length - 1) == " "){
	  		myString = myString.substring(0,myString.length-2);
	    }
	  }
  }
  return myString;
}


// --- validasi alamat email ---
function isEMailValid(EMailAddress){
  var c;  // counter
  var ch;  // character at specific index
  var isValid;  // is e-mail address valid?
  var msg;  // alert message
  var titik=0;  //number of "."
  var ampcount=0;  // number of "@"'s
  var ampfound;  // is "@" found

  // scan for any invalid character
  for(c=0;c<EMailAddress.length;c++){
    ch=EMailAddress.charAt(c);
    isValid=((ch=="@")||(ch==".")||(ch=="-")||(ch=="_")||((ch>="A")&&(ch<="Z"))||((ch>="a")&&(ch<="z"))||((ch>="0")&&(ch<="9")));
    if(ch=="@")
    { ampcount++;ampfound=true;
      if((EMailAddress.charAt(c+1)==".")||  // there may not be a "." right after an "@"
    	   (c==0)||                            // or the first character is an "@"
    	   (EMailAddress.charAt(c+1)==''))   //there cannot be empty after "@"
       isValid=false;
    }
    if(ch==".")
   	{ titik++;
   		if(EMailAddress.charAt(c+1)=='')  // there cannot be empty after titik
   			isValid=false;
   	}
    // there may not be a "." succeeding another "." after an "@"
    if((ch==".")&&(EMailAddress.charAt(c+1)==".")&&(ampfound))
      isValid=false;
    if(!isValid)
      break;
  }
  if(ampcount!=1)
    isValid=false;  // there may not be more than one "@"
  // so, is it valid or not?
  if(titik==0)
  	isValid=false; // there must be at least 1 titik

  EMailAddress.toLowerCase();
  
  if(!isValid){
    alert("Invalid e-mail address");
  }
  return (isValid);
}


// --- validasi alamat website ---
function isWebsiteValid(WebsiteAddress){
  var c;  // counter
  var ch;  // character at specific index
  var isValid;  // is website address valid?
  var msg;  // alert message
  var doubleslashfound=false;  // is "//" found?
  var singleslashfound=false;  // is "/" found?

  if (WebsiteAddress.length == 0){
  	return true;
  }

  // scan for any invalid character
  for(c=0;c<WebsiteAddress.length;c++){
    ch=WebsiteAddress.charAt(c);
    isValid=((ch==":")||(ch=="/")||(ch==".")||(ch=="&")||(ch=="%")||(ch=="?")||(ch=="~")||(ch=="+")||(ch=="-")||(ch=="_")||(ch=="#")||(ch=="=")||((ch>="A")&&(ch<="Z"))||((ch>="a")&&(ch<="z"))||((ch>="0")&&(ch<="9")));

    // first character must be alphanumeric
    if((c==0)&& !(((ch>="A")&&(ch<="Z"))||((ch>="a")&&(ch<="z"))))
      isValid=false;

    // there may be only one occuring "//"
    if((ch=="/")&&(WebsiteAddress.charAt(c+1)=="/")){
      if(doubleslashfound)
        isValid=false;
      else
        doubleslashfound=true;
    // no "." after a "//"
    if(WebsiteAddress.charAt(c+2)==".")
      isValid=false;
  }
  if((ch=="/")&&(WebsiteAddress.charAt(c+1)!="/")&&(WebsiteAddress.charAt(c-1)!="/")&&(c>=1))
    singleslashfound=true;

  // no ":" after a "//"
  if((ch==":")&&(doubleslashfound))
    isValid=false;

  // no domain name containing special characters except "-" and "."
  if(!(singleslashfound)&&((ch=="&")||(ch=="%")||(ch=="?")||(ch=="=")||(ch=="~")||(ch=="+")||(ch=="#")||(ch=="_")))
    isValid=false;

  // there may not be a "/" succeeding a "."
  // there may not be a "." succeeding another "."
  if(((ch==".")&&(WebsiteAddress.charAt(c+1)=="/"))||((ch==".")&&(WebsiteAddress.charAt(c+1)==".")))
    isValid=false;
    if(!isValid)
      break;
  }

  // so, is it valid or not?
  if(!isValid){
    alert("Invalid website address");
  }
  return(isValid);
}


// --- validasi angka ---
// --- object = object fom yang akan divalidasi ---
// --- labelStr = nama yg kita berikan utk sebutan object tsb spy mudah dimengerti orang ---
function checkNumber(object, labelStr){
  var checkStr = object.value;
  var allValid = true;
  var decPoints = 0;
  var msg = "Invalid ";

  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
    allValid=((ch>="0")&&(ch<="9")||(ch=="-"));
    if(!allValid){
      msg = msg + labelStr + ".";
      alert(msg);
      object.focus();
      break;
    }
  }
  return(allValid);
}


// --- validasi kode pos ---
function kodePosValid(zip_code)  {
	var c;
	var ch;
	var isValid=true;
	
	for(c=0;c<zip_code.length;c++)
	{
		ch=zip_code.charAt(c);
		isValid=((ch>='0')&&(ch<='9'));
		if(!isValid)
			break;
	}
	return(isValid);
}


// --- object kosong, belum diisi ? ---
// --- object = object yang akan divalidasi ---
// --- labelStr = nama yg kita berikan utk sebutan object tsb spy mudah dimengerti orang ---
function kosong(object, labelStr){
  if(trim(object.value) == ""){
    var msg = labelStr + " harus diisi.";
    alert(msg);
    object.focus();
    return(true);
  }
  return(false);
}

function isNum(e){	//angka only
	var key = window.event ? e.keyCode : e.which;
	
	if (
		(key > 47 && key < 58) || key == 8
		) return true; 
		return false;
	}
	
function isNum_minus(e){	//titik dan angka and (-) only 
	var key = window.event ? e.keyCode : e.which;
	
		if (
		( key > 44 &&  key < 58) || key == 8 
		) return   true; 
		else return  false;
	}

function periksatanggal(tankgal, parammonth, paramyear) { // cek tanggal
	if((parammonth == 4)||(parammonth == 6)||(parammonth == 9)||(parammonth == 11)){
		if(tankgal >= 31){
			return false;
		}else return true;
	}else if((parammonth == 2)){
				if(tankgal == 29){
					if((paramyear%4) == 1){
						return false;
					}
					return true;
				}else if(tankgal >= 30){
							return false;
					  }else return true;
			}else return true;
} 


// --- cek apakah user sudah memilih nilai dari dropdown ? ---
// --- objeck = objeck dropdown yang akan divalidasi ---
// --- labelStr = nama yg kita berikan utk sebutan object tsb spy mudah dimengerti orang ---
function dropdownBelumDipilih(object, labelStr) {
	if (object.selectedIndex == 0) {	
		var msg = labelStr + " harus diisi.";
		alert(msg);
		object.focus();
    return(true);
  }
  return(false);
}
