
function mustField(field,minLength,msgx){
	field.value=trimText(field)
	if(field.value.length<=minLength){
		alert(msgx);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function delLink(lnk){
	if(confirm("Are you sure to delete image?"))
	window.navigate(lnk)
}

function allNums(field,numLen,msgx) {
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	if(numLen>0){
		if(field.value.length != numLen) ok = "no";
	}
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert(msgx);
		field.focus();
		field.select();
		return false;
    }
	return true;
}


function chkNum(field,numLen,msgx){
	var ok = "yes";
	if(numLen>0){if(!(/^\d{5}$/.test(field.value))) ok="no";}
	else{if(!(/^\d$/.test(field.value))) ok="no";}
	if (ok == "no") {
		alert(msgx);
		field.focus();
		field.select();
		return false;
    }
	return true;
}

//Working but takes lot of time!
function checkEmail(field,msgx){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)) return true;
	alert(msgx);
	field.focus();
	field.select();
	return false;
}

	//Opens New window
function newWin(url){
	window.open(url,'NewWindowName','width=450,height=350,alwaysRaised=yes,scrollbars=yes,dependent=yes');
}

	//Returns selected check boxes values
function SelectedChkBoxValues(frm,chkBoxName)
{
	var ids=""
	for (var i=0;i<frm.elements.length;i++)
		if (frm.elements[i].name == chkBoxName)
			if (frm.elements[i].checked == true)
				ids=ids + frm.elements[i].value + ","
	return ids;
}				

	//String reverse  usage: varStr=varStr.reverse();
String.prototype.reverse = function() {
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
}

//Trimming a String
function  trimText(fldName)
{
	var name=fldName.value;
	while(name.charAt(0)==' ')
	{
		name=name.substring(1,name.length);
	}
	while(name.charAt((name.length)-1)==' ')
		name=name.substring(0,(name.length)-1);
	
	return name;
}

// Check whether string  is empty.
function isEmpty(s)
{   
return ((s == null) || (s.length == 0))
}








//---------------- From Validations.Js---------------

function checkFile(field)
{
		var path,extn,dotposn
		path=trimText(field)
		dotposn=path.lastIndexOf(".")
		if (dotposn==-1)
		{
			alert("Invalid file selected for image. Only the following types of files are allowed for images...\n*.gif, *.jpg, *.jpeg,*.tif,*.tiff,*.bmp,*.ico")
 			return false
		}
		else 
		{
			extn=path.substring(dotposn,path.length).toLowerCase()
			if (!(extn==".jpg" || extn==".jpeg" || extn==".gif" || extn==".ico" || extn==".tif" || extn==".tiff" || extn==".bmp" || extn==".png"))
			{
				alert("Invalid file selected for image. Only the following types of files are allowed for images...\n*.gif, *.jpg, *.jpeg,*.tif,*.tiff,*.bmp,*.ico,*.png")
				return false
			}
		}
		return true
} 


//Checking for a valid number
function isInteger (s)

{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

//Checking for a valid Digit
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

//used in the function that checks for a Valid Zip Code
function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return false;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == 5) ||
             (s.length == 9)))
}

//checking for a valid ZIP Code
function checkZIPCode (fldName)
{   
     var normalizedZIP = putChars(fldName.value, "-")
      if (!isZIPCode(normalizedZIP, false)) 
         return false//showAlert (fldName, "ZIP field must be a 5 or 9 digit code (like 94043). Please reenter it now.");
      else 
      {  
         fldName.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    
}

//Reformats the ZIP Code as a String
function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
  //  else return (reformat (ZIPString, "", 5, "-", 4));
}

//Reformats a String into a specific Format
function reformat (s)
{   
	var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}


//checking for a Valid Phone Number
function checkPhone (fldName)
{   
      
	  var normalizedPhone = putChars(fldName.value, "()- ")
		
       if (!isPhoneNumber(normalizedPhone, false)) 
          return false//showAlert (fldName, "Phone field must be a 10 digit number (like 4155551212). Please reenter it now.");
       else 
          return true;
   
}

//Used in the function that checks for a valid Phone number
function isPhoneNumber (s)
{   
    return (isInteger(s) && s.length == 10)
}


//Reformats the Phone Number as a String
function reformatPhoneNumber (PhoneNo)
{   return (reformat (PhoneNo, "(", 3, ") ", 3, "-", 4))
}


//puts the specified characters(second argument) into the String 
function putChars (s, chars)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (chars.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


//checking for a Valid Date
function isDate(dateStr,strField,strFieldName) 
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
    
    if (matchArray == null) {
        alert("Please enter date in mm/dd/yyyy format.")
		return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { 
        alert("Month must be between 1 and 12 for " + strField);
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31 for " + strField);
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days for " + strField)
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days for " + strField);
            return false;
        }
    }
	year = parseInt(year)
	//alert(year.length)
	if(year < 1000 )
	{
		alert("Please enter a valid Year (Year should be in yyyy format)")
        return false;
	}

	//if(year )


    return true; 
}

//----------------all of above are from validations.js----------------
