// JavaScript Document
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 

//date format = dd/mm/yyyy
function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date of birth (dd/mm/yyyy)");
return false;
}

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

//PPage
function submitPForm(doc)
{	doc.submit()
}

//Confirmation before delete
function confirmDelete()
{	msg = "Are you sure you want to delete?"
	rex = confirm(msg);
	if(rex == true){
		return true;
	}else{
		return false;
	}
}



//Confirmation delete by id
function deleteById(doc, field, id){
	rs = confirmDelete()
	if(rs == true)
	{	submitForm(doc, field, id)
	}
}

// Digunakan untuk edit
function submitForm(doc, field, id)
{	field.value = id
	 doc.submit()
}

function checkValue(value,field)
    {
    var tmpVar = value;
    
    var isError = false;
	
    if (isEmpty(tmpVar)){
	    alert("Type the amount.");
            isError=true;
		}
   	else if (isNaN(tmpVar)){
		alert("You have typed an invalid amount.");
		isError=true;
		}
    else if((tmpVar.indexOf('.') != -1 ) && ((tmpVar.indexOf('.')+3) != tmpVar.length) && ((tmpVar.indexOf('.')+2) != tmpVar.length)){
	     alert("You have typed an invalid amount.");
	     isError=true;
	}

    tmpVar = parseFloat(tmpVar);

    if (tmpVar <= 199.99 || tmpVar > 1000000.01){
		alert("Amount must be greater than 200 and less than 1,000,000.00"); 
		isError=true;
		return false;
	}

    if(isError){
    	field.select();
		field.focus();
		return false;
		}

    return true;
}


function checkValueWith(value,field)
    {
    var tmpVar = value;
    
    var isError = false;
	
    if (isEmpty(tmpVar)){
	    alert("Type the amount.");
            isError=true;
		}
   	else if (isNaN(tmpVar)){
		alert("You have typed an invalid amount.");
		isError=true;
		}
    else if((tmpVar.indexOf('.') != -1 ) && ((tmpVar.indexOf('.')+3) != tmpVar.length) && ((tmpVar.indexOf('.')+2) != tmpVar.length)){
	     alert("You have typed an invalid amount.");
	     isError=true;
	}

    tmpVar = parseFloat(tmpVar);

    if (tmpVar <= 199.99){
		alert("Amount must be greater than 200");
		isError=true;
		return false;
		}

    if(isError){
    	field.select();
		field.focus();
		return false;
		}

    return true;
}

function isEmpty(str) {
		if( str == null || str == "")
		    return true;
		var returnValue = true;
		var len = str.length;
		for( i=0; i<len; i++ ) {
		    if( str.charAt(i) != " ")
				returnValue = false;
		}
		return returnValue;
	}