//Utility functions

function warnInput( inputField ){
inputField.focus();
inputField.select();
return false;
}

function validString(str)
{
        //list of invalid strings
if     (str!='' &&
        str!=' ' &&
        str!='  ' &&
        str!='   ' &&
        str!='_')
        return true;
else
        return false;
}

function escapeSingleQuotes(textObj)
{     
	// Escape single quotes in string
	var newstr ="";
	for (i = 0; i < textObj.value.length; i++)
    {	newstr += textObj.value.charAt(i);
		// if character is a single quote
        if (textObj.value.charAt(i) == "'") 
			newstr += "'"; //append an extra quote after every occurrence
    }
	textObj.value = newstr; //reset value of string
}

function TextAreaSizeIsRight(textObj, maxs){
  if(textObj.value.length >= (maxs)){
    return false;
  }
  return true;
}

function checkTextArea(textObj){
  if(!TextAreaSizeIsRight(textObj, textAreaMaxSize)){
    textObj.value=textObj.value.substring(0, textAreaMaxSize-1);
    alert('Message must be under ' + textAreaMaxSize + ' characters!');
    textObj.focus();
    textObj.select();
  }
  return;
}


function isInteger(x){
for (i = 0; i < x.length; i++)
    {
        // Check that current character is a number.
        var c = x.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit
// (0 .. 9)
function isDigit (c){ return ((c >= "0") && (c <= "9")) }

function warnInput( inputField ){
inputField.focus();
inputField.select();
return false;
}

function TextAreaSizeIsRight(textObj, maxs){
  if(textObj.value.length >= (maxs)){
    return false;
  }
  return true;
}

function checkTextArea(textObj){
  if(!TextAreaSizeIsRight(textObj, textAreaMaxSize)){
    textObj.value=textObj.value.substring(0, textAreaMaxSize-1);
    alert('Message must be under ' + textAreaMaxSize + ' characters!');
    textObj.focus();
    textObj.select();
  }
  return;
}

function validPhoneNumber(inputObj){

	if (validString(inputObj.value)) {
        var phone=inputObj.value;
        var newphone='';
        isValid = false;
        	for (i = 0; i < phone.length; i++)
            {
         	     // Check that current character is a digit 0-9.
           	     var c = phone.charAt(i);
           	     if (isDigit(c)) {
			newphone= newphone + c;
		     }
            }
	    
            if (newphone.length >= 10){
          		//inputObj.value = newphone;
                return true;
            }
            else {
              alert(phone + ' is not a valid phone number.');
                              //+  'Please enter phone number as ' +
                             //'(xxx)xxx-xxxx or xxx-xxxx or xxx.xxx.xxxx');
                             //return warnInput(inputObj);
           }
	}

    else   // return true in the case
          // the phone number is skipped (blank)
          // as it is optional

     return true;
}

function validPhoneNumber(inputObj, international){

	if ((validString(inputObj.value)) && (international)) {
           var phone=inputObj.value;
           var newphone='';
           isValid = false;

      	   for (i = 0; i < phone.length; i++)
           {
      	     // Check that current character is a digit 0-9.
       	     var c = phone.charAt(i);
       	     if (isDigit(c)) {
		newphone= newphone + c;
	     }
	     else
	     {
                alert(phone + ' is not a valid phone number.  Please correct and try again.');
                return false;
	     }
           }
	    
//           if (newphone.length >= 10){
//              return true;
//           }
//           else {
//              alert(phone + ' is not a valid phone number.  Please correct and try again.');
//           }
	}
       else {
       	 alert("Enter a valid phone number.");
         return false;
      }
}

function isInteger(x){
for (i = 0; i < x.length; i++)
    {
        // Check that current character is a number.
        var c = x.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if argument string is valid date
// in the format mm/dd/yyyy or false otherwise
function isDate(s)
{
        d1    = s.charAt(2);
        d2    = s.charAt(5);
        month = s.substring(0, 2);
        day   = s.substring(3, 5);
        year  = s.substring(6, 10);

        if(s.length == 10 &&
                isInteger(month) && parseInt(month) < 13 && parseInt(month, 10) > 0 &&
                isInteger(day) &&  parseInt(day) < 32 && parseInt(day, 10) > 0 &&
                isInteger(year) &&
                d1 == '/' &&
                d2 == '/' ){
      
      if (Date.prototype.GetFullYear){ //Browser supports GetFullYear()
         if (isGoodDate(s)) return true;
             else 
                    alert('"' + s + '" is invalid.');
      }
      else return true;
          
        }
        else
        {
        alert('Date \"' + s +
        '\" is invalid, please enter a valid date in format' +
        ' \"mm\/dd\/yyyy\" with the leading zeros');
        return false
        }
}

function isGoodDate( dateIn ){
var d1 = new Date (dateIn);
var x  = d1.getMonth() + 1;
var xMonth = ((x <= 9 ) ? ('0' + x) : x);
var y  = d1.getDate();
var yDate  = ((y <= 9 ) ? ('0' + y) : y);
var z  = d1.getFullYear();
var zYear  = z;
var resultDate = xMonth + '/' + yDate + '/' + zYear;
if (resultDate == dateIn) return true;
else return false;
}


function mouseOver(imgName){
        if (document.images){document[imgName].src=eval(imgName + "_on.src")}
}

function mouseOut(imgName){
        if (document.images){document[imgName].src=eval(imgName + "_off.src")}
}

function hasSpecialCharacters (s)

{   var i;
    var returnString = "";
    var bag = ":;'\"()";
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    hasSC = false;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) {
        	hasSC = true;
        	break;
    	}
    }
    return hasSC;
}



//PHONE

// 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 isInteger2(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 stripCharsInBagLength(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.length;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger2(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }
 
 
function checkpass(x){
for (i = 0; i < x.length; i++)
    {
        // Check that current character is a number.
        var c = x.charAt(i);

        if (isDigit(c)) return true;
    }

    // All characters are characters.
    return false;
}