﻿// create the prototype on the String object
//added by MN on 11/1 to trim the spaces
String.prototype.trim = function() 
{
 // skip leading and trailing whitespace
 // and return everything in between
 //return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
  return this.replace(/(^\s *)|(\s*$)/g,"")
}

function CheckFields(FrmName,FldName,FldType,maxChar,displayName)
  {
  var s;
  Field = eval("document." + FrmName + "." +FldName)
  Field.value=Field.value.trim();  
  if(FldType=='text')
  {
  if(Field.value=="" || Field.value.length > maxChar)
  {
    if(Field.value=="") 
		alert(displayName+" cannot have blank or invalid characters.")
	else	
		alert(displayName+" can have maximum of "+maxChar+" characters.")
  Field.focus()
  return false
  }}
  
  else if(FldType=='fldsize')
  {
  if(Field.value.length > maxChar)
  {
  alert(displayName+" cannot be greater than "+maxChar)
  Field.focus()
  return false
  }}

  else if(FldType=='logintext')
  {
  if(Field.value=="" || Field.value.length > maxChar)
  {
    if(Field.value=="") 
		alert(displayName+" cannot be blank.")
	else	
		alert(displayName+" can have maximum of "+maxChar+" characters.")
  Field.focus()
  return false
  }
  if(Field.value.indexOf("@")>=0 || Field.value.indexOf(".")>=0 || Field.value.indexOf("'")>=0 || Field.value.indexOf('"')>=0 || Field.value.indexOf("~")>=0 || Field.value.indexOf("!")>=0 || Field.value.indexOf("#")>=0 || Field.value.indexOf("$")>=0 || Field.value.indexOf("%")>=0 || Field.value.indexOf("^")>=0 || Field.value.indexOf("&")>=0 || Field.value.indexOf("*")>=0 || Field.value.indexOf("(")>=0 || Field.value.indexOf(")")>=0 || Field.value.indexOf("-")>=0 || Field.value.indexOf("+")>=0 || Field.value.indexOf("=")>=0 || Field.value.indexOf("|")>=0 || Field.value.indexOf("/")>=0 || Field.value.indexOf("?")>=0 || Field.value.indexOf(">")>=0 || Field.value.indexOf("<")>=0 || Field.value.indexOf(",")>=0 || Field.value.indexOf(";")>=0 || Field.value.indexOf(":")>=0 || Field.value.indexOf("{")>=0 || Field.value.indexOf("}")>=0 || Field.value.indexOf("[")>=0 || Field.value.indexOf("]")>=0 || Field.value.indexOf("`")>=0 || Field.value.indexOf("\\")>=0)
  {
  alert("Please enter a valid "+displayName+".\n\nOnly characters, numbers and underscores ( _ ) are allowed."); 	   
  Field.focus()
  return false  }
  }
  
  else if(FldType=='textarea')
  {
  if(Field.value.length>maxChar)
  {
  	alert(displayName+" can have maximum of "+maxChar+" characters.")
	Field.focus()
	return false  }
  }
  else if(FldType=='number')
  {if(isNaN(Field.value))
  {
  alert("Please enter numeric value for "+displayName+".")
  Field.focus()
  return false  }
  }
  else if(FldType=='numberblank')
  {
  if(Field.value=="")
  {
  alert(displayName+" cannot have blank or invalid characters.")
  Field.focus()
  return false  }
  }
  else if(FldType=='numberAllowBlank')
  {
  if(Field.value!="" && isNaN(Field.value))
  {
  alert(displayName+" cannot have invalid characters.")
  Field.focus()
  return false  }
  }
  
  else if(FldType=='percent')
  {
   if(Field.value=="")
  {
  alert(displayName+" cannot have blank or invalid characters.")
  Field.focus()
  return false  
  }
  else if(Field.value > 100)
  {
  alert("Please enter a value not more than 100 for "+displayName+".")
  Field.focus()
  return false  }
  
  }
  

  else if(FldType=='decimal')
  {
     if(Field.value=="" || Field.value.length > maxChar || Field.value==0 ) 
    {
        if ((Field.value=="")||(Field.value==0))
		alert(displayName+" cannot have blank or invalid characters.")
	else 
		alert(displayName+" can have maximum of "+maxChar+" characters.")
  Field.focus()
  return false
  }  
  
  	str = Field.value;
	re = /^\d*\.{0,1}\d+$/;
	pos = str.search(re);
	if (pos < 0) {
		alert("Please enter only Numeric or Decimal values for "+displayName+".")
		Field.focus()
		return false } 
  }	
  else if(FldType=='decimalAllowBlank')
  {
  	str = Field.value;
	if(str!="") 
	{
	re = /^\d*\.{0,1}\d+$/;
	pos = str.search(re);
	if (pos < 0) {
		alert("Please enter only Numeric or Decimal values for "+displayName+".")
		Field.focus()
		return false } 
	}	
  }	
  
  else if(FldType=='date') 
  {
  DateFld=new Date(Date.parse(Field.value))
//alert(DateFld)
if (isNaN(DateFld)) 
  {
  alert("Please enter proper "+displayName+".")
  Field.focus()
  return false }
 var dt=Field.value
   
  //added by pegasus 11
  }

  else  if(FldType=='numeric')
  {
	var charpos = Field.value.search("[^0-9]");
	if(Field.value=="") {
		alert(displayName+" cannot have blank or invalid characters.");
		 Field.focus()
		return false
		}	
		else if(charpos >= 0) 
		{     
			alert("Please enter numeric value for "+displayName+".");
			 Field.focus()
			return false; 
			    
		}
   }
   
   else  if(FldType=='cartqty')
  {
	var charpos = Field.value.search("[^0-9]");
		if(Field.value=="") {
		alert(displayName+" cannot have blank or invalid characters.");
		 Field.focus()
		return false
		}	
		else if(charpos >= 0) 
		{     
			alert("Please enter numeric value for "+displayName+".");
			 Field.focus()
			return false; 
			    
		}
		if(Field.value==0) {
		alert(displayName+" must be greater than 0.");
		 Field.focus()
		return false
		}	
   }
   
   
   else if(FldType=='email')
  {
 
  if(Field.value.indexOf("@")==-1 || Field.value.indexOf(".")==-1 || Field.value.indexOf(" ")!=-1 || Field.value.length<=6 || Field.value.indexOf("'")>=0 || Field.value.indexOf('"')>=0 || Field.value.indexOf("~")>=0 || Field.value.indexOf("!")>=0 || Field.value.indexOf("#")>=0 || Field.value.indexOf("$")>=0 || Field.value.indexOf("%")>=0 || Field.value.indexOf("^")>=0 || Field.value.indexOf("&")>=0 || Field.value.indexOf("*")>=0 || Field.value.indexOf("(")>=0 || Field.value.indexOf(")")>=0 || Field.value.indexOf("--")>=0 || Field.value.indexOf("+")>=0 || Field.value.indexOf("=")>=0 || Field.value.indexOf("|")>=0 || Field.value.indexOf("/")>=0 || Field.value.indexOf("?")>=0 || Field.value.indexOf(">")>=0 || Field.value.indexOf("<")>=0 || Field.value.indexOf(",")>=0 || Field.value.indexOf(";")>=0 || Field.value.indexOf(":")>=0 || Field.value.indexOf("{")>=0 || Field.value.indexOf("}")>=0 || Field.value.indexOf("[")>=0 || Field.value.indexOf("]")>=0 || Field.value.indexOf("`")>=0 || Field.value.indexOf("\\")>=0)
  {
  alert("Please enter a valid "+displayName+"."); 	   
  Field.focus()
  return false  }
  }
  
  
  else if(FldType=='combobox')
	{
		if (Field.value==""){
			alert("Please select "+displayName+".");
			Field.focus();
			return false ;
		}
	}

   
   // added by pegasus 11
   
   else if(FldType=='singlequotesnotallowed')
  {
  if(Field.value.length > maxChar)
  {
    alert(displayName+" can have maximum of "+maxChar+" characters.")
    Field.focus()
    return false
  }
  if(Field.value.indexOf('\'')>=0 )
  {
    alert("Please enter a valid "+displayName+".\n\n single quote/s ( \' ) are not allowed."); 	   
    Field.focus()
    return false  } 
  }
 }
 
 //---------------------- delete confirm added by pegasus 11
 
 function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		//alert("Please enter a valid year")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    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 deleteconfirm(msg){
	if(true==confirm(msg))
		return true;
	else
		return false;
}




function extractDigits(mystr)
{
	// extracts only digits from a string and returns a new numeric string
/*	var newstr = '';
	for( i=0; i<mystr.length; i++ )
	{
	//convert the i-th character to ascii code value
	c = mystr.charCodeAt(i); 
	if( (c>=48) && (c<=57) ) newstr = newstr.concat(mystr.substr(i,1));
	}
	return newstr*/
	return mystr.replace(/[^0-9]/g,'');
}


// The following functions were written by Gordon McComb
// More information can be found here: http://www.javaworld.com/javaworld/jw-02-1997/jw-02-javascript.html
function stripCharString (InString, CharString)  {
        var OutString="";
   for (var Count=0; Count < InString.length; Count++)  {
        var TempChar=InString.substring (Count, Count+1);
      var Strip = false;
      for (var Countx = 0; Countx < CharString.length; Countx++) {
        var StripThis = CharString.substring(Countx, Countx+1)
         if (TempChar == StripThis) {
                Strip = true;
            break;
         }
      }
      if (!Strip)
        OutString=OutString+TempChar;
   }
        return (OutString);
}
function allowInString (InString, RefString)  {
        if(InString.length==0) return (false);
        for (var Count=0; Count < InString.length; Count++)  {
        var TempChar= InString.substring (Count, Count+1);
      if (RefString.indexOf (TempChar, 0)==-1)  
        return (false);
   }
   return (true);
}

function isNumeric(FormName,FieldName,DisplayName,Required)
{
	var invalidnum=new Array("-"," ","!","@","#","$","%","^","&","*","(",")","+","=","'",'"',"`","?","/","|",".","{","}","[","]",";",":","~","<",">",",","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M");
	var Field = eval("document." + FormName + "." + FieldName);
	var strphone = Field.value.trim();
	var strlen = strphone.length;
	if (Required=="yes")
	{
		if(strphone=="")
		{
			alert(DisplayName + " can't be left blank");
			Field.focus();
			return false;
		}
		if(strphone.substring(0,1)==" ")
		{
			alert(DisplayName + " first character can't be a space");
			Field.focus();
			return false;
		}
	}
	if (strlen!=0){
		for(var i=0;i<=strlen;i++){
			chr=strphone.charAt(i);
			for(var j=0;j<84;j++){	
				if(invalidnum[j]==chr){
					if (chr==" "){
						alert("Please remove space from for " + DisplayName);
					}else{
						alert("Please enter numeric value for " + DisplayName);
					}
					Field.focus();
					return false;
				}//end of if	
			}//end of inner for		
		}//end of outer for
		
	}//end of outer if
	
	return true;
}


var win = null;
function newWindow(mypage,myname,w,h,features) {
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}


function showpopup(mypage,myname,w,h,features){	
	//debugger;
	//---------------
	var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 /*if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
//-----------------
	//alert(viewportwidth);
	//alert(viewportheight);
	popupstat='true'
	document.getElementById("dv_mask").style.width='' + viewportwidth + 'px';
	document.getElementById("dv_mask").style.height='' + viewportheight + 'px';
	document.getElementById("dv_mask").style.visibility='visible';
	//window.open(url,'',properties)
*/	
	 var winl = (screen.width-w)/2;
		var wint = (screen.height-h)/2;
		if (winl < 0) winl = 0;
		if (wint < 0) wint = 0;
		var settings = 'height=' + h + ',';
		settings += 'width=' + w + ',';
		settings += 'top=' + wint + ',';
		settings += 'left=' + winl + ',';
		settings += features;
		win = window.open(mypage,myname,settings);
		win.window.focus();
}



//------------------- for preloading images -----------------------------------------

