//this page is used for various type of validations required by the forms of FreshVegies.net
//Created by
//	KK	Kundan Kumar


function isLength(Item,name)
{	
	var sItemValue = Item.value;
	if(sItemValue.length  > 0)
	{
		return true;
	}
	else
	{
		alert(name+" field cannot be left empty.");
		Item.focus();
		Item.select();
		return false;
	}
}

function isNumber(Item,name)
{
	var sItemValue = Item.value;
	if(!isNaN(sItemValue))
	{
		return true;
	}
	else
	{
		alert(name+" field must contain only numeric data.");
		Item.focus();
		Item.select();
		return false;
	}
}

function isProperDate(Item,name)
{
	if(Item.value != "")
   {
   	    var sItemValue = Item.value;
	    var date2 = new Date();
	    date2.setTime(Date.parse(sItemValue));
	    
	    if(isNaN(date2))
            {
                alert("Please enter a valid date in field "+name);
                Item.focus();
		Item.select();
                return false;
            }
        else
        	  {
        	 	  return true;
        	  }
    }
    else
    {
    	alert("Please enter a valid date in field "+name);
       Item.focus();
       Item.select();
       return false;
	}
}

function isAlpha(Item,name)
{
	if(isLength(Item,name))
	{
		var isNot = "`~!@#$%^&*()-+={}][|:;'\?><,. /";
		var invalid = false;
		var sItemValue = Item.value;
		
		outer:for(var i = 0; i<sItemValue.length;i++)
		{
			for(var j=0;j<isNot.length;j++)
				{
					if(sItemValue.charAt(i) != isNot.charAt(j))
					{
						continue;
					}
					else
					{
						alert("Please use only ALpha Numeric Characters in the field "+name);
						break outer; 						
					}
				}
		 }
	}
}
			 				
function isEmail(Item,name)
{
	if(isLength(Item,name))
	{
		var e = new String(Item.value);
		if((e.indexOf("@")>0) && (e.indexOf(".")>0))  				
		{
			if(e.length>5)
			{
				return true;
			}
		}
		else
		{
			alert("Please enter a proper email address in field "+name);
			Item.focus();
			Item.select();
			return false;
		}
	}

}


function isZip(Item,name)
{
      if(isLength(Item,name))
      {
			var tVal = Item.value;
			if(tVal.length == 5)
			{
				if(!isNaN(tVal))
				{
					return true;
				}
				else
				{
					alert("Please enter a valid Zip Code in the field "+name);
					Item.focus();
					Item.select();
				    return false;
				}
					
			}
			else if(tVal.length == 10)
			{
				var m1 = tVal.substring(5,6);
				alert(m1);
				var l5 = tVal.substring(0,5);
				alert(l5);
				var r4 = tVal.substring(tVal.length,(tVal.length - 4));
				alert(r4);
						
				if((!isNaN(l5) && !isNaN(r4)) && (m1=="-"))
		  		{
					return true;
		  		}
				else
		  		{
					alert("Please enter a valid Zip Code in the field "+name);
					Item.focus();
					Item.select();
					return false;
		  		}
			}
			else
			{
		       alert("Please enter a valid Zip Code in the field "+name);
				Item.focus();
				Item.select();
				return false;
       	}
      }
 }
 
 function isInteger(Item,name)
 {
	var sItemValue = Item.value;
 	if(!isNaN(sItemValue))
 	{
 		if(sItemValue.indexOf(".")>0)
 		{
 			alert("Please enter a valid integer value in the field "+name);
 			Item.focus();
			Item.select();
 			return false;
 		}
 		else
 		{
 			return true;
 		}
	}
	else
	{
		alert("Please enter a valid integer value in the field "+name);
 		Item.focus();
		Item.select();
 		return false;
 	}
}

function isFloat(Item,name)
{
 	var sItemValue = Item.value;
 	
 	if(!isNaN(sItemValue))
 	{
 		if(sItemValue.indexOf(".")>0)
 		{
 			return true;
 		}
 		else
 		{
 			alert("Please enter a valid float value in the field "+name);
 			Item.focus();
			Item.select();
 			return false;
  		}
	}
	else
	{
		   alert("Please enter a valid float value in the field "+name);
 			Item.focus();
			Item.select();
 			return false;
	}
}

function isPhone(Item,name)
{
	if(isLength(Item,name))
	{
		var isNot = "abcdefghijklmnopqrstuvwxyz`~!@#$%^&*+={}][|:;'\?><./";
		var invalid = false;
		var sItemValue = Item.value;
		
		var isValid = "() ,-";
			
		outer:for(var i = 0; i<sItemValue.length;i++)
		{
			inner:for(var j=0;j<isValid.length;j++)
				{
				  if (isNaN(sItemValue.charAt(i)))
				  {
					if(sItemValue.charAt(i) != isValid.charAt(j))
					{
						invalid = true;
						//alert(sItemValue.charAt(i) + " = " + isValid.charAt(j));
						continue;
					}
					else
					{
						invalid = false;
						break inner;
					}
					
				  }
				}
		}
		
		//alert(invalid);
		if (invalid)
		{
			alert("Please Enter a valid phone number in field " +name+ ". You can only use numbers and characters such as (),- and space between them");
			Item.focus();
			Item.select();
			return false;
		}
		else
		{
			return true;	 						
		}
		
		
	}
}

function isCCNumber(Item)
{
	var ccnumber = "false";
	var ccnum = "";
    if(!isNaN(Item))
    {
	    ccnum = Item;
    }
    total = 0
    i = ccnum.length;
    for(i;i >= 2;i=i-2)
    {
	    total = total + parseInt(ccnum.substring(i-1,i));
	    tmp  = ((ccnum.substring(i - 2,i-1) * 2));
	    tmp = "" + tmp;
	    total = total + parseInt(tmp.charAt(0));
	    if (tmp.length > 1)
	    {
		    tmp = "" + tmp;
		    total = total + parseInt(tmp.charAt(tmp.length - 1));
	    }
     }
     if ((total.length % 2) == 1)
     {
	     total = total + parseInt(ccnum.charAt(0));
     }
     if((total % 10) == 0)
     {
	   ccnumber = "true";
     }
		
    if (ccnumber == "true")
    {
       return true;
    }
    else
    {
   	   alert("Please use a valid Credit Card Number");
	   return false;
    }
}

function isCCDate(Item1,Item2)
{
	var ccdate = "false";
	if(Item1.value == "01")
		stempMonth = "0";
	else if(Item1.value == "1")
		stempMonth = "0";
	else if(Item1.value == "02")
		stempMonth = "1";
	else if(Item1.value == "2")
		stempMonth = "1";
	else if(Item1.value == "03")
		stempMonth = "2";
	else if(Item1.value == "3")
		stempMonth = "2";
	else if(Item1.value == "04")
		stempMonth = "3";
	else if(Item1.value == "4")
		stempMonth = "3";
	else if(Item1.value == "05")
		stempMonth = "4";
	else if(Item1.value == "5")
		stempMonth = "4";
	else if(Item1.value == "06")
		stempMonth = "5";
	else if(Item1.value == "6")
		stempMonth = "5";
	else if(Item1.value == "07")
		stempMonth = "6";
	else if(Item1.value == "7")
		stempMonth = "6";
	else if(Item1.value == "08")
		stempMonth = "7";
	else if(Item1.value == "8")
		stempMonth = "7";
	else if(Item1.value == "09")
		stempMonth = "8";
	else if(Item1.value == "9")
		stempMonth = "8";
	else if(Item1.value == "10")
		stempMonth = "9";
	else if(Item1.value == "11")
		stempMonth = "10";
	else if(Item1.value == "12")
		stempMonth = "11";
	else
		{
		}
	sMonth = parseInt(stempMonth);
	stmpYear = "20" + Item2.value
	sYear = parseInt(stmpYear);
	var today = new Date();

	var sMonthNow = today.getMonth();
	var sYearNow = today.getYear();

	if(sYear == parseInt(sYearNow))
	{
		if(sMonth >= parseInt(sMonthNow)) 
		{
			alert(" MONTH TRUE ");
			ccdate = "true";
		}
	}
	else
	{
		if(sYear < (parseInt(sYearNow) + 4))
		{
				ccdate = "true";
		}
	}
	
	if(ccdate == "true")
	{
		isCCDate = true;
		return true;
	}
	else
	{
		alert(" Please enter a valid expiry date. ");
		return false;
	}
}
