//Main Validation Function


function Validate() {
	
	validated = false;
        if (!(validate_required("fullname","Your  name is required.") )){
         	return false;
	}else if (!(validate_email("email", "Your valid email is required.") )){
		return false;
	}
	
	validated = true;
	return true;
		
	
     	
}





function validate_date(dayId,monthId,yearId,message){
		if (isDate(dayId,monthId,yearId,message)==false){
			return false;
		}
	    return true;
 }

function validate_select(id,message) {
	if(!document.getElementById(id)){
		alert("id="+id+ " and does not exist");
		return false;
	}
        if(document.getElementById(id).options[document.getElementById(id).selectedIndex].value == ""){
                if(message != ''){
               		alert(message);
                }
                document.getElementById(id).focus();
                return false;
         }
         else{
                return true;
        }
}

function validate_number(id,message){
	if(isInteger(document.getElementById(id).value)){
		if(message != ''){
			alert(message);
		}
		document.getElementById(id).focus();
		return false;
	}
	else{
		return true;
        }


}
//Text exists validation
function validate_required(id,message) {
	if(!document.getElementById(id)){
		alert("id="+id+ " and does not exist");
		return false;
	}
	document.getElementById(id).value = Trim(document.getElementById(id).value);
        if(Trim(document.getElementById(id).value) == ""){
                if(message != ''){
			alert(message);
                }
                document.getElementById(id).focus();
                return false;
         }
         else{
                return true;
        }
}

function validate_required_number(id,message) {
	if(!document.getElementById(id)){
		alert("id="+id+ " and does not exist");
		return false;
	}
	document.getElementById(id).value = Trim(document.getElementById(id).value);
        if(isNaN(parseFloat(Trim(document.getElementById(id).value)))){
                if(message != ''){
			alert(message);
                }
                document.getElementById(id).focus();
                return false;
         }
         else{
                return true;
        }
}

function validate_checkbox(id,message) {
        if(!(document.getElementById(id).checked) ){
                if(message != ''){
			alert(message);
                }
                document.getElementById(id).focus();
                return false;
         }
         else{
                return true;
        }
}

function validate_radio(radio_obj,message){
	
	if(!radio_obj){
		alert("name="+radio_obj+ " and does not exist");
		return false;
	}

	myOption = -1;
	for (i=radio_obj.length - 1; i > -1; i--) {
	if (radio_obj[i].checked) {
	myOption = i;
	}
	}
	if (myOption == -1) {
		alert(message);
		radio_obj[0].focus();
		return false;
	}
	return true;

}

function validate_email(id,message){
  document.getElementById(id).value = Trim(document.getElementById(id).value);
  if(!echeck(document.getElementById(id).value)){
     if(message != ''){
     	alert(message);
     }
     document.getElementById(id).focus();
     return false;
   }
   else{
      return true;
   }
}

//String Trim Function
//removes spaces from the start and the end of the string
function Trim(TRIM_VALUE){
        if(TRIM_VALUE.length < 1){
                return"";
        }
        TRIM_VALUE = RTrim(TRIM_VALUE);
        TRIM_VALUE = LTrim(TRIM_VALUE);
        if(TRIM_VALUE==""){
                return "";
        } else{
                return TRIM_VALUE;
        }
} 

function RTrim(VALUE){
        var w_space = String.fromCharCode(32);
        var v_length = VALUE.length;
        var strTemp = "";
                if(v_length < 0){
                        return"";
        }
        var iTemp = v_length -1;

        while(iTemp > -1){
                if(VALUE.charAt(iTemp) == w_space){
                }
                else{
                        strTemp = VALUE.substring(0,iTemp +1);
                        break;
                }
                iTemp = iTemp-1;
        } 
        return strTemp;

}


function LTrim(VALUE){
        var w_space = String.fromCharCode(32);
        if(v_length < 1){
                return"";
        }
        var v_length = VALUE.length;
        var strTemp = "";

        var iTemp = 0;

        while(iTemp < v_length){
                if(VALUE.charAt(iTemp) == w_space){
                }
                else{
                        strTemp = VALUE.substring(iTemp,v_length);
                        break;
                }
                iTemp = iTemp + 1;
        } 
        return strTemp;
}





//Email Syntax Checking Function
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){
     return false
   }

   if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
   }

   if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
     return false
   }

   if (str.indexOf(at,(lat+1))!=-1){
     return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
     return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
     return false
   }

   if (str.indexOf(" ")!=-1){
      return false
   }

   return true
}
