/////////////////////////////////////////////////////////////////////
// 
// Function discriptions
// fieldname(name) - field name is function used for the name of the field which goes to 
// validation should be respectd name which is used in html page or these separte page  their used javascript
//
/////////////////////////////////////////////////////////////////////////

function findfield(field)
{
	switch(field)
	{
		//1
		case "txt_subject":
		return "Subject of New registration";
		break;
		//2
		case "txt_email_id":
		return "Email id";
		break;

		case "txt_subject1":
		return "Subject of Update registration";
		break;

		case "txt_subject2":
		return "Subject of Forgot Password";
		break;

		case "txt_smassage":
		return "Spacial message";
		break;

		case "txt_massage":
		return "Message";
		break;

		case "txt_message":
		return "Message";
		break;

		case "txt_fname":
		return "First name";
		break;
		case "txt_name":
				return "Your name";
				break;

		case "txt_cust_no":
				return "Customer Number";
				break;
		case "txt_lname":
		return "Last name";
		break;

		case "txt_company":
		return "Company name";
		break;

		case "txt_address":
		return "Address name";
		break;

		case "txt_uname":
		return "User name";
		break;

		case "txt_password":
		return "Password";
		break;

		case "txt_email":
		return "Email id";
		break;
		
		case "txt_phone":
		return "Phone Number";
		break;

		case "email_address":
		return "Email Address";
		break;
		case "txt_business":
		return "Business Id";
		break;

		case "txt_account":
		return "Account Id";
		break;

		default:
			return field;
		break;
	}
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
	
    return returnString;
}

function checkInternationalPhone(strPhone){

s= stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isLetter(c)
  {
	return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")));
  }

function ValidateEmail(val)
	{
    var id=val;
	var at=id.indexOf('@');
	var lastat=id.lastIndexOf('@');
	var dot=id.indexOf('.');
	lastdot=id.lastIndexOf('.')
	if ( !( (0 < at) && (at < (lastdot-1)) && (lastdot < (id.length-1)) && (at == lastat) ) )
	{
	  error = 1;
	  return (false);
	}
	var str_referral=id.length;
	var ch_referral=id;
	for(i=0;i<str_referral;i++)
	{
		ch_referral1=ch_referral.substring(i,i+1);

		if(ch_referral1==" ")
		{			
			return (false);
		}
	}
}
function isValidFile(file) 
{
	var extArray = new Array(".gif", ".jpg",".jpeg");
	var allowSubmit = false;
	var ext 
	while (file.indexOf("\\") != -1)
	{
		file = file.slice(file.indexOf("\\") + 1);
	}
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) 
		{ 
			allowSubmit = true; 
			break; 
		}
	}

	if (allowSubmit)
	{ 
		return true;
	}
	else
	{
		return false;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args = MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2];
  val=MM_findObj(args[i]); 
    if (val) { nm=findfield(val.name); if ((val=val.value)!="") {//email validation
      if (test.indexOf('isEmail')!=-1) {
	  if(ValidateEmail(val)==false) errors+='- '+nm+' must contain an e-mail address.\n';
      }
	  else if (test.indexOf('isImage')!=-1)//image validation
	  {if (!isValidFile(val)) errors+='- '+nm+' type must be gif, jpg or jpeg.\n';}

	   else if (test.indexOf('isAlpha')!=-1)//alphabate validation
	  { if (!isLetter(val)) errors+='- '+nm+' must contain a alphabate.\n';
		}

else if (test.indexOf('isPhone')!=-1)//alphabate validation
	  { if (checkInternationalPhone(val)==false) errors+='- '+nm+' must be Valid Number.\n';
		}

	  else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


//validation - Email - isEmail
//validation - Image - isImage
//validation - alphabate - isAlpha
//validation - Range - inRange
//validation - Numeric - RisNum
//validation - Required - R
//validation - Required Phone no- RisPhone

//validation - Not Required but Numeric - N

