// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

function checkEmail (strng) {
strngVal = strng.value
var error="";
if (strngVal == "") {
   error = "Please fill the email address\n";
}

    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strngVal))) {
       error = "Please enter a valid email address\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strngVal.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }

        if ("" != error) {
                alert(error);
                strng.focus()
                return false;
        }
//return error;
   return true;
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng,fieldname) {
var error = "";
strngVal = strng.value
if (strngVal == "") {

}
else
{
var stripped = strngVal.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The "+fieldname+" phone number contains illegal characters.\n";

    }
}
        if ("" != error) {
                alert(error);
                strng.focus()
                return false;
        }
//return error;
return true;
}

// number validation added by tejswita - start
function checkNumber(strng,fieldname) {
var ValidChars = "0123456789";
var IsNumber=true;
var Char;
sText = strng.value

for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   if(IsNumber == false)
   {
     alert("The "+fieldname+" contains illegal characters.\n");
   }
   return IsNumber;

}

function checkLegalStr(strng,fieldname) {
var ValidChars = "abcdegefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@._123456789- ";
var IsLegalStr=true;
var Char;
sText = strng.value

for (i = 0; i < sText.length && IsLegalStr == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsLegalStr = false;
         }
      }

   if(IsLegalStr == false)
   {
     alert("The "+fieldname+" phone number contains illegal characters.\n");
   }
   return IsLegalStr;

}


// number validation added by tejswita - End


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
strngVal = strng.value
if (strngVal == "") {
   error = "Please fill the password\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers

    if ((strngVal.length < 6) || (strngVal.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strngVal)) {
      error = "The password contains illegal characters.\n";
    }
    else if (!((strngVal.search(/(a-z)+/)) && (strngVal.search(/(A-Z)+/)) && (strngVal.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }

        if ("" != error) {
                alert(error);
                strng.focus()
                return false;
        }
//return error;
return true;
}


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (fld) {
var error = "";
strng = fld.value
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    }

        if ("" != error) {
                alert(error);
                fld.focus()
                return false;
        }
//return error;
return true;
}


// non-empty textbox

function isEmpty(strng,fieldname) {
var error = "";
var vval = strng.value;
  if (vval.length == 0) {
     error = "Please fill the "+fieldname+"\n"
  }
  if ("" != error) {
       alert(error);
           strng.focus()
           return false;
    }
//return error;
   return true;
}

// was textbox altered

function isDifferent(fld) {
var error = "";
strng = fld.value
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }

        if ("" != error) {
                alert(error);
                fld.focus()
                return false;
        }
//return error;
return true;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice,fieldname) {
var error = "";
var choiceInd = choice.selectedIndex
    if (choiceInd == 0) {
    //error = "You didn't choose an option from the drop-down list '"+fieldname+"'.\n";
        error = "Please select an option from the '"+fieldname+"' drop-down list.\n";
    }
  if ("" != error) {
       alert(error);
           choice.focus()
           return false;
    }
//return error;
   return true;
}


function checkCheckbox(choice,fieldname) {
var error = "";
var choiceInd = choice.checked
    if (!(choiceInd)) {
    error = "Please read the Terms of Use\n";
    }
  if ("" != error) {
       alert(error);
           choice.focus()
           return false;
    }
//return error;
   return true;
}


function call_alert(fld,errMsg,callFocus) {
        if ("" != errMsg) {
       alert(errMsg);
           if(callFocus)
                   fld.focus()
                return false;
    }
}


function isIllegal(strng,fieldname) {
var error = "";
var vval = strng.value;
//var illegalChars = / /;
  if (vval.length == 0) {
     error = "Please fill the "+fieldname+".\n"
  }
//  if (illegalChars.test(vval)) {
//    error  = "This contains illegal characters.\n";
//    }
  if ("" != error) {
       alert(error);
           strng.focus()
           return false;
    }
//return error;
   return true;
}

var alerted; alerted=0;
function checkFileType(fld) {

        var val = fld.value;
         val=val.replace(/[\"]/g, '');
        var strVal = val.substring((val.length-3),val.length);
        //&& alerted != 1 //        strVal=strVal.replace(/[\"]/g, '');
        if( '' != val && 'ZIP' != strVal.toUpperCase()) {
                alert("Sorry.. only zip files are allowed");
                alerted = 1;
                return false;
        }
                 //        else {alerted = 0;}

        return true;
}
