
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 || 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.substring(ldot+1,ldot+2)==dot){
			alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lstr-1,lstr )==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 checkName(customerName)
{	
	
	 
   var iChars = "*|,\":<>[]{}`\';()@&$#%1234567890";
   for (var i = 0; i < customerName.length; i++) {
	     
      if (iChars.indexOf(customerName.charAt(i)) != -1)
	  {
	  	  return false;
	  }
   }

   return true;

}




function validateZIP(field) { 
var valid = "0123456789-";
for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") 
			{
				alert("Invalid characters in your zip code.  Please try again.");
				return false;
			}
	}
   return true
}


var dtCh= "/";
var minYear=05;
var maxYear=99;

function isInteger(){
	var i;	
	var s=document.wsdetails.fee.value;
    for (i = 0; i < s.length; i++){  
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
		{
			alert("Enter valide number for Fee");
			return false;
		}
    }

    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";


    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function parseIntMyway(str){
	if(str.indexOf("0")==0){
		str=str.substring(1,str.length-1);
	}
	return parseInt(str);
}
function isDate(dtStr){
	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
		if(pos1==0)
	{
		alert("Invalide '/' character in front of month");
		return false;
	}
	var strMonth=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos1+1)		
	
		var valid = "0123456789/";
	if (dtStr.length > 6 ) 
	{
		alert("Please enter your 2 digit month and 2 digit year");
		return false;
	}
	for (var i=0; i < dtStr.length; i++) 
	{
		temp = "" + dtStr.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") 
			{
				alert("Invalid characters in your date.  Please try again.");
				return false;
			}
	}
	

	month=parseIntMyway(strMonth);
	year=parseIntMyway(strYear);
		
	if (pos1==-1 ){
		alert("The date format should be : mm/yy")
		return false
	}

	//if (strMonth.length!=2 || month<1 || month>12){
	if (strMonth.length!=2 || strMonth<1 || strMonth>12){
		
		alert("Please enter a valid month")
		return false
	}
	
	if (strYear.length != 2 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 2 digit year between "+ "05" +" and "+maxYear)
		return false
	}	 	
	var currentDate=new Date()
	var curYear=currentDate.getFullYear(); 	 
	if(year>80)
	{
		year=year+1900;
		alert("invalid year");
		return false;
	}
	 strYear=1+strYear;
	 year=parseInt(strYear);
	 strYear=1900+year;	
	if(curYear>strYear){
			alert("Please enter a valid date");	
			return false;
	}
	else if (curYear==strYear)
		{	
			
			if(currentDate.getMonth()+1>strMonth)
				{
					alert("Please enter a valid date");
					return false;
				}
		}
return true
}


//////////////////Credit Card Validation Function///////////////

function isValidCreditCard(type, ccnum) {
	if(type==null || type=="")
		return false
    if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^\d{4}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Master") {
	   
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^\d{4}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover") {
      // Discover: length 16, prefix 6011, dashes optional.
      //var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	  var re = /^\d{4}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "American Express") {
      // American Express: length 15, prefix 34 or 37.
      //var re = /^3[4,7]\d{13}$/;
	  var re = /^\d{4}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)){ 
	   
	   return false};
   
   return true
}


 function isValidatePhone(field)
	{	  
var valid = "0123456789-";
var hyphencount = 0;
for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
			{
				alert("Invalid characters in your phone number.  Please try again.");
				return false;
			}
	}
var re = /^\d{3}-\d{3}-\d{4}$/; 
 if (!re.test(field))
	 { 
		return false
	 }

   return true
	
}



function isNoOfBooks(books)
	{
		
		 
		if(books=="")
			{ return false}
		var valid = "0123456789";
		
	    for (var i = 0; i < books.length; i++) 
			{
	     		temp = "" + books.substring(i, i+1);
				if (valid.indexOf(temp) == "-1") 
							{
								return false;
							}
			}
	   return true 
		
	}

function ValidateBookForm() 
{									
	var mand=new Array()

	
	var customerFirstName=document.BookForm.firstname
	var customerLastName=document.BookForm.lastname
	if (((customerFirstName.value==null)||(customerFirstName.value=="")) && 	
		((customerLastName.value==null)||(customerLastName.value=="")))
	{
		 mand.push("Please Enter your Name")
	}else{		 
		if ((customerFirstName.value != null)||(customerFirstName.value != ""))
			{
				if(checkName(customerFirstName.value)==false)
					{
						mand.push("Please Enter your Name")
					}
			}
		if ((customerLastName.value != null)||(customerLastName.value != ""))
			{
		
				if(checkName(customerLastName.value)==false)
					{
						mand.push("Please Enter your Name")
					}
			}

	}
	
		
	if (((document.BookForm.address1.value==null) ||(document.BookForm.address1.value=="")) && 
		((document.BookForm.address2.value=="") ||
		(document.BookForm.address2.value==null)))
		{	
			mand.push("Please Enter Your Shipping Address")
		}
	if (((document.BookForm.billingaddress1.value==null) ||(document.BookForm.billingaddress1.value=="")) && 
		((document.BookForm.billingaddress2.value=="") ||
		(document.BookForm.billingaddress2.value==null)))
		{	
			mand.push("Please Enter Your Billing Address")
		}
	if (document.BookForm.city.value==null ||document.BookForm.city.value=="")
		{
			mand.push("Please Enter The Shipping City")			

		} 
	if (document.BookForm.billingcity.value==null ||document.BookForm.billingcity.value=="")
		{
			mand.push("Please Enter The Billing City")			

		} 	
	if (document.BookForm.state.selectedIndex==0)
//		if (document.BookForm.state.value==null ||document.BookForm.state.value=="")
		{
						mand.push("Please Enter The Shipping State")			
		}
	if (document.BookForm.billingstate.selectedIndex==0)
//		if (document.BookForm.state.value==null ||document.BookForm.state.value=="")
		{
						mand.push("Please Enter The Billing State")			
		}
	if (document.BookForm.noofbooks.value==null ||document.BookForm.noofbooks.value=="")
		{
			mand.push("Please Enter The Number of Books")			

		} 
	if (document.BookForm.zip.value==null ||document.BookForm.zip.value=="")
		{
			mand.push("Please Enter The Shipping Zip Code")			
		}
	else{	 
		if(validateZIP(document.BookForm.zip.value)==false)
			{
				//document.BookForm.zip.value=""
				document.BookForm.zip.focus()
				return false
			}
	}
	if (document.BookForm.billingzip.value==null ||document.BookForm.billingzip.value=="")
	{
		mand.push("Please Enter The Billing Zip Code")			
	}
	else{
	 
		if(validateZIP(document.BookForm.billingzip.value)==false)
			{
				//document.BookForm.zip.value=""
				document.BookForm.billingzip.focus()
				return false
			}
	}	
		var emailID=document.BookForm.email
		if ((emailID.value==null)||(emailID.value==""))
			{
				mand.push("Please Enter Your Email Address")			
			}
		else
			{ 
				if (echeck(emailID.value)==false)
					{
						emailID.value=""
						emailID.focus()
						return false
					}
			} 
	if(isValidatePhone(document.BookForm.phone.value)==false)
	{	
		mand.push("Please Enter Your Phone Number format xxx-xxx-xxxx")			
	}  					 


	if(document.BookForm.elements[13].name=="noofbooks")
	{	
		if(isNoOfBooks(document.BookForm.noofbooks.value)==false)
		{
			mand.push("Please Enter No. of Books")			
		} 								 
	}

	if(document.BookForm.name.value=="" || document.BookForm.name.value==null)
	{	
		mand.push("Please Enter Your Name as it appears on your credit card.")			
	}else{
		if(checkName(document.BookForm.name.value)==false)
		{
			mand.push("Please Enter Your Name")			
		}
	}
	if(document.BookForm.cardtype.value != "Visa" && document.BookForm.cardtype.value != "Master" &&document.BookForm.cardtype.value != "Discover" && document.BookForm.cardtype.value != "American Express" )
	{
		mand.push("Please Select Card Type")			
	}
	var dt=document.BookForm.expdate
	if(dt.value==null || dt.value=="")
	{
		mand.push("Please Enter Card Expiry Date")			
	}else{
		if (isDate(dt.value)==false)
		{
			dt.focus()
			return false
		}					
	}
	if(document.BookForm.cardnumber.value==null || document.BookForm.cardnumber.value=="")
	{
		mand.push("Please Enter 16 digit Card Number")			
	}else{
		if(isValidCreditCard(document.BookForm.cardtype.value,document.BookForm.cardnumber.value)==false)
		{
			mand.push("Invalid Card Number")			
		}	
	}
	
    if(mand.length>0)
	{
		var string =""
		for(var i=0;i<mand.length;i++)
		{
			string= string + mand[i] + "\n"
		}
		alert(string) 
		return false
	}
	return true
}


function ValidateWorkshopForm() 
{									
	var mand=new Array()

	
	var customerFirstName=document.BookForm.firstname
	var customerLastName=document.BookForm.lastname
	if (((customerFirstName.value==null)||(customerFirstName.value=="")) && 	
		((customerLastName.value==null)||(customerLastName.value=="")))
	{
		 mand.push("Please Enter your Name")
	}else{		 
		if ((customerFirstName.value != null)||(customerFirstName.value != ""))
			{
				if(checkName(customerFirstName.value)==false)
					{
						mand.push("Please Enter your Name")
					}
			}
		if ((customerLastName.value != null)||(customerLastName.value != ""))
			{
		
				if(checkName(customerLastName.value)==false)
					{
						mand.push("Please Enter your Name")
					}
			}

	}
	
		
	if (((document.BookForm.address1.value==null) ||(document.BookForm.address1.value=="")) && 
		((document.BookForm.address2.value=="") ||
		(document.BookForm.address2.value==null)))
		{	
			mand.push("Please Enter Your Billing Address")
		}
	if (document.BookForm.city.value==null ||document.BookForm.city.value=="")
		{
			mand.push("Please Enter The Billing City")			

		} 	
	if (document.BookForm.state.value=="select")
//		if (document.BookForm.state.value==null ||document.BookForm.state.value=="")
		{
						mand.push("Please Enter The Billing State")			
		}
	if (document.BookForm.zip.value==null ||document.BookForm.zip.value=="")
		{
			mand.push("Please Enter The Billing Zip Code")			
		}
	else{	 
		if(validateZIP(document.BookForm.zip.value)==false)
			{
				//document.BookForm.zip.value=""
				document.BookForm.zip.focus()
				return false
			}
	}	
	var emailID=document.BookForm.email
	if ((emailID.value==null)||(emailID.value==""))
	{
		mand.push("Please Enter Your Email Address")			
	}else{ 
		if (echeck(emailID.value)==false)
		{
			emailID.value=""
			emailID.focus()
			return false
		}
	} 
	if(isValidatePhone(document.BookForm.phone.value)==false)
	{	
		mand.push("Please Enter Your Phone Number format xxx-xxx-xxxx")			
	}  
	if(document.BookForm.name.value=="" || document.BookForm.name.value==null)
	{	
		mand.push("Please Enter Your Name as it appears on your credit card.")			
	}else{
		if(checkName(document.BookForm.name.value)==false)
		{
			mand.push("Please Enter Your Name")			
		}
	}
	if(document.BookForm.cardtype.value != "Visa" && document.BookForm.cardtype.value != "Master" &&document.BookForm.cardtype.value != "Discover" && document.BookForm.cardtype.value != "American Express" )
	{
		mand.push("Please Select Card Type")			
	}
	var dt=document.BookForm.expdate
	if(dt.value==null || dt.value=="")
	{
		mand.push("Please Enter Card Expiry Date")			
	}else{
		if (isDate(dt.value)==false)
		{
			dt.focus()
			return false
		}					
	}
	if(document.BookForm.cardnumber.value==null || document.BookForm.cardnumber.value=="")
	{
		mand.push("Please Enter 16 digit Card Number")			
	}else{
		if(isValidCreditCard(document.BookForm.cardtype.value,document.BookForm.cardnumber.value)==false)
		{
			mand.push("Invalid Card Number")			
		}	
	}
	
    if(mand.length>0)
	{
		var string =""
		for(var i=0;i<mand.length;i++)
		{
			string= string + mand[i] + "\n"
		}
		alert(string) 
		return false
	}
	return true
}


function  Hide()
{
	   	 
		document.BookForm.name.disabled=true
		document.BookForm.cardtype.disabled=true
		document.BookForm.expdate.disabled=true
		document.BookForm.cardnumber.disabled=true
	
}

function Show()
{
	    document.BookForm.name.disabled=false
		document.BookForm.cardtype.disabled=false
		document.BookForm.expdate.disabled=false
		document.BookForm.cardnumber.disabled=false
		
}
 