/*
<-----------=[ F.I.L.E  I.N.F.O ]=----------->

-Language: PHP 5.2.5
-Database: None
-Web Server: Apache 2.2.4
-IDE: Zend PHP editor
-OS: Windows 2000 SP4

-Author: Bijin Babu
-Filename: form_validation.js
-Description: Provides the form validation functionality to the register module
-Created On: 30-Sep-2008
-Modified by : Anez.A
-Last Modified On: 17-April-2009

<-----------------=[ E.N.D ]=----------------->
 */

// flags for the mandatory entries...

var isValid = new Array();

isValid[1] = false;
isValid[2] = false;
isValid[3] = false;
isValid[4] = false;
isValid[5] = false;

var isWarr = new Array();
isWarr[1] = false;
isWarr[2] = false;
isWarr[3] = false;
isWarr[4] = false;
isWarr[5] = false;
isWarr[6] = false;
isWarr[7] = false;




function validate(e)
{
    var val = e.value;
    switch (e.id)
    {
        case "firstname":
            result = isEmpty(val);
            document.getElementById(1).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(1).src="images/validation/false.png"',100);
                isValid[1] = false;
            }
            else
            {
                setTimeout('document.getElementById(1).src="images/validation/true.png"',100);
                isValid[1] = true;
            }
			
            break;

		case "lastname":
            result = isEmpty(val);
            document.getElementById(2).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(2).src="images/validation/false.png"',100);
                isValid[2] = false;
            }
            else
            {
                setTimeout('document.getElementById(2).src="images/validation/true.png"',100);
                isValid[2] = true;
            }
			
            break;

       
            
        case "email":
            result = isEmail(val);
            document.getElementById(3).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(3).src="images/validation/true.png"',100);
                isValid[3] = true;
            }
            else
            {
           		setTimeout('document.getElementById(3).src="images/validation/false.png"',100);
                isValid[3] = false;
            }
			
            break;  
		case "telephone":
			result = isEmpty(val);
			document.getElementById(4).src="images/validation/loading.gif";
			if(result=="1")
			{
				setTimeout('document.getElementById(4).src="images/validation/false.png"',100);
				isValid[4] = false;
			}
			else
			{
				setTimeout('document.getElementById(4).src="images/validation/true.png"',100);
				isValid[4] = true;
			}
		
		break;  
            
              
        case "comments":
            result = isEmpty(val);
            document.getElementById(5).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(5).src="images/validation/false.png"',100);
                isValid[5] = false;
            }
            else
            {
                setTimeout('document.getElementById(5).src="images/validation/true.png"',100);
                isValid[5] = true;
            }
			
            break;          
         
    }	
}

// checking wheather all the mandatory fields are filled before form submission...
function doSubmit()
{
    var i=0;
    var frm = document.frmMain;
    for(i=1;i<=isValid.length;i++)
    {	
        if(isValid[i]==false)
        {
            alert("Please fill the mandatory field(s) before submitting");
            return false;
        }
    }
	return true;
    
}

function validateWarr(e)
{
    var val = e.value;
    switch (e.id)
    {
        case "product_name":
            result = isEmpty(val);
            document.getElementById(1).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(1).src="images/validation/false.png"',100);
                isWarr[1] = false;
            }
            else
            {
                setTimeout('document.getElementById(1).src="images/validation/true.png"',100);
                isWarr[1] = true;
            }
			
            break;

		case "product_no":
            result = isEmpty(val);
            document.getElementById(2).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(2).src="images/validation/false.png"',100);
                isWarr[2] = false;
            }
            else
            {
                setTimeout('document.getElementById(2).src="images/validation/true.png"',100);
                isWarr[2] = true;
            }
			
            break;

       case "purc_date":
            result = isEmpty(val);
            document.getElementById(3).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(3).src="images/validation/false.png"',100);
                isWarr[3] = false;
            }
            else
            {
                setTimeout('document.getElementById(3).src="images/validation/true.png"',100);
                isWarr[3] = true;
            }
			
            break;
      case "trans_id":
            result = isEmpty(val);
            document.getElementById(4).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(4).src="images/validation/false.png"',100);
                isWarr[4] = false;
            }
            else
            {
                setTimeout('document.getElementById(4).src="images/validation/true.png"',100);
                isWarr[4] = true;
            }
			
            break;
		case "vin_no":
            result = isEmpty(val);
            document.getElementById(5).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(5).src="images/validation/false.png"',100);
                isWarr[5] = false;
            }
            else
            {
                setTimeout('document.getElementById(5).src="images/validation/true.png"',100);
                isWarr[5] = true;
            }
			
            break;
        case "email":
            result = isEmail(val);
            document.getElementById(6).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(6).src="images/validation/true.png"',100);
                isWarr[6] = true;
            }
            else
            {
           		setTimeout('document.getElementById(6).src="images/validation/false.png"',100);
                isWarr[6] = false;
            }
			
            break;  
		           
              
        case "comments":
            result = isEmpty(val);
            document.getElementById(7).src="images/validation/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(7).src="images/validation/false.png"',100);
                isWarr[7] = false;
            }
            else
            {
                setTimeout('document.getElementById(7).src="images/validation/true.png"',100);
                isWarr[7] = true;
            }			
            break;          
         
    }	
}


// checking wheather all the mandatory fields are filled before form submission...
function doSend()
{
    var i=0;
    var frm = document.frmMain;
    for(i=1;i<=isWarr.length;i++)
    {	
        if(isWarr[i]==false)
        {
            alert("Please fill the mandatory field(s) before submitting");
            return false;
        }
    }
	return true;
    
}



/////////For integer value
 function isNumberKey(evt)
  {
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	 else
		return true;
  }                            

/* returns true if the string is empty */
function isEmpty(str){
    return (str == null) || (str.length == 0);
}

/* returns true if the string is a valid email */
function isEmail(str){
    if(isEmpty(str)) return false;
    var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
    return re.test(str);
}

/*  returns true if the string only contains characters A-Z or a-z */
function isAlpha(str){
    var re = /[^a-zA-Z]/g
    if (re.test(str)) return false;
    return true;
}

/* returns true if the string only contains characters 0-9 */
function isNumeric(str){
    var re = /[\D]/g
    if (re.test(str)) return false;
    return true;
}

/* returns true if the string only contains characters A-Z, a-z or 0-9 */
function isAlphaNumeric(str){
    var re = /[^a-zA-Z0-9]/g
    if (re.test(str)) return false;
    return true;
}

/* returns true if the string's length equals "len" */
function isLength(str, len){
    return str.length == len;
}

/* returns true if the string's length is between "min" and "max" */
function isLengthBetween(str, min, max){
    return (str.length >= min)&&(str.length <= max);
}

/* returns true if the string is a US phone number formatted as...
 (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000 */
function isPhoneNumber(str){
    var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
    return re.test(str);
}

function isMobileNumber(str){
    var re = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/
    return re.test(str);
}

/* Most Concise RegExp for matching Decimal IPs. If nothing else, it'll make your code easier to read. (And I know that \d?\d is \d{1,2} but that's 2 extra characters.) --Update: darkone noticed 8 characters could be shaved down. I've edited it to reflect this */
function isIPAddress(str){
    var re = /\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b/
    return re.test(str);
}

/*Description  	
This will grep for a valid MAC address , with colons seperating octets. It will ignore strings too short or long, or with invalid characters. It will accept mixed case hexadecimal. Use extended grep.
Matches 	
01:23:45:67:89:ab | 01:23:45:67:89:AB | fE:dC:bA:98:76:54
Non-Matches 	
01:23:45:67:89:ab:cd | 01:23:45:67:89:Az | 01:23:45:56:*/
function isMacAddress(str){
    var re = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
    return re.test(str);
}

/* Checks for a valid windows file name (Must be used with the case-insensitive option Checks that the file has at lease one char, does not contain any invalid characters and does not have a reserved word as a file name. &quot;If you see a &amp;quot; in the regex replace it with a &quot; character&quot;
Matches: test.txt | test.jpg.txt | a&b c.bmp Non-Matches: CON | .pdf | test:2.pdf */



/* returns true if the string is a valid date formatted as...
 mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy */
function isDate(str){
    var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
    if (!re.test(str)) return false;
    var result = str.match(re);
    var m = parseInt(result[1]);
    var d = parseInt(result[2]);
    var y = parseInt(result[3]);
    if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
    if(m == 2){
        var days = ((y % 4) == 0) ? 29 : 28;
    }else if(m == 4 || m == 6 || m == 9 || m == 11){
        var days = 30;
    }else{
        var days = 31;
    }
    return (d >= 1 && d <= days);
}
/* Extra Added for validation */
function limitText(field,maxlimit,remark) 
{
	if (field.value.length > maxlimit) 
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{
		if(remark) 
		{
			remark.value = maxlimit - field.value.length;
		}
	}	
}
/* Extra Added for validation */
function Trim(TXT)
{
	if(TXT=="")
	return ;
	
	return TXT.replace(/(^\s+)|(\s+$)/g,""," ");
}
function sendMail()
{
	var frm = document.frmMain;
	
	var fflag = doSubmit();
	
	
	if(!fflag)		
	return;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		alert ("Your browser does not support AJAX!");
		return;
	  } 
	
	var url="ajax/getPageContents.php";
	url=url+"?form_action="+"MAILLIST";
	url=url+"&first="+encodeURI(frm.firstname.value);
	url=url+"&last="+encodeURI(frm.lastname.value);
	url=url+"&email="+encodeURI(frm.email.value);
	url=url+"&telephone="+encodeURI(frm.telephone.value);
	url=url+"&comments="+encodeURI(frm.comments.value);
	url=url+"&sid="+Math.random();
	
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
}

function sendWarr()
{
	var frm = document.frmMain;
	
	var fflag = doSend();
	
	
	if(!fflag)		
	return;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
		alert ("Your browser does not support AJAX!");
		return;
	  } 
	
	var url="ajax/getPageContents.php";
	      
	url=url+"?form_action="+"SENDWARR";
	url=url+"&product="+encodeURI(frm.product_name.value);
	url=url+"&pro_no="+encodeURI(frm.product_no.value);
	url=url+"&purc_date="+encodeURI(frm.purc_date.value);
	url=url+"&trans_id="+encodeURI(frm.trans_id.value);
	url=url+"&vin_no="+encodeURI(frm.vin_no.value);
	url=url+"&email="+encodeURI(frm.email.value);
	url=url+"&comments="+encodeURI(frm.comments.value);
	url=url+"&sid="+Math.random();
	
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
}


function GetXmlHttpObject()
{

	var xmlHttp=null;
try
{
	// Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	 catch (e)
	 {
	  // Internet Explorer
	 try
	 {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	 }
	  catch (e)
	 {
		 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
 }
		  return xmlHttp;
}

function stateChanged() 
{
if (xmlHttp.readyState==4)
{
	var val=xmlHttp.responseText;
	document.getElementById('contnt_div').innerHTML = val;
	
	
}
}

