﻿// JScript File

//Function to trim the space in the left side of the string
function ltrim ( s ){
  return s.replace( /^\s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
   return s.replace( /\s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
   var temp = s;   
   return temp.replace(/^\s+/,'').replace(/\s+$/,'');
}

//used to trim numeric
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

//replace all
String.prototype.ReplaceAll=function(oldChr,newChar){
  if(this.indexOf(oldChr)>=0)
     {
        var str=this.replace(oldChr,newChar);
        return str.ReplaceAll(oldChr,newChar);
     }
     else
     {
       return this;
     }
 
  }
// Check File Upload
function IsFileUpload(strID){
    var oFileUpload = document.getElementById(strID);
//    var tmp=new Image();
//    tmp.src=oFileUpload.value;
//    if(tmp.complete){
//        tmp=null;
//        return true;
//    }else{
//        tmp=null;
//        return false;
//    }  

    if(trim(oFileUpload.value).length <= 0){
        return false;    
    }else{
        return true;
    }
  
}
//Check image type

function IsValidImage(strID)
 {
   var oFileUpload = document.getElementById(strID);
   var imagePath = oFileUpload.value;
   var pathLength = imagePath.length;
   var lastDot = imagePath.lastIndexOf(".");
   var fileType = imagePath.substring(lastDot,pathLength);
   if((fileType == ".jpeg") ||(fileType == ".gif") || (fileType == ".jpg") || (fileType == ".JPEG") || (fileType == ".GIF") || (fileType == ".JPG"))
    {
       return true;
    } 
    else {
        return false;
        }
 }



//Check Blank
function IsBlank(strID){    
    var oField = document.getElementById(strID);
    if(oField!=null){
        var strValue = oField.value.trim();
        RemoveTrim(oField);
        if(strValue.length <= 0){ 
            return true;    
        }else{
            return false;
        }  
    } 
    return false;     
}

//remove trim from the field
function RemoveTrim(oField){
    var fieldTagName=oField.tagName.toLocaleLowerCase();
     var strValue = oField.value.trim();
    if(fieldTagName=="input"){
        var fieldTagType=oField.type.toLocaleLowerCase();
        if(fieldTagType=="text"){
             window.document.getElementById(oField.id).value=strValue;
        }
    }
    else if(fieldTagName=="textarea"){
        window.document.getElementById(oField.id).value=strValue;
    }
    
}


//Change Css class name of element
/*
function ChangeCSSClass(strID,isError){  
    var oField = document.getElementById(strID);
      if(oField!=null){
        if(isError){       
                oField.className="row errorfield";
            
        }else{
                oField.className = "row";
        } 
     }      
}
*/
function ChangeCSSClass(strID,isError){  
    var oField = document.getElementById(strID);
    var errorClass;
    var successClass;
      if(oField!=null){
         var myClass=oField.className;
         if(myClass.indexOf("row")>-1){errorClass="row errorfield";successClass="row";}
         else if(myClass.indexOf("field")>-1){errorClass="field error";successClass="field";}
         else if(myClass.indexOf("inp-txt")>-1){errorClass="inp-txt errorfield";successClass="inp-txt";}
         else{errorClass="errortext";successClass="";}
            
            
            
            if(isError){       
                    oField.className=errorClass;                
                
            }else{
                    oField.className = successClass;
            } 
         }      
 }
 
//validate email
function IsValidEmail(strID)
{
    var email=document.getElementById(strID).value;
    var regexEmail=/^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)$/;
    return regexEmail.test(email)
}
//validate website
function IsValidWebsite(strID)
{
    var website=document.getElementById(strID).value;
    var regexWebsite=/^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$/;
    return regexWebsite.test(website)
}

//validate phone
function IsValidPhone(strID)
{
    var phone=document.getElementById(strID).value;
    var regexphone=/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/;
    return regexphone.test(phone)
}

//validate zip
function IsValidZip(strID)
{
    var zip=document.getElementById(strID).value;
    var regexzip=/^\d{5}(-\d{4})?$/;
    return regexzip.test(zip)
}

//validate zip
function IsValidUSZip(strID,strCountryId)
{
    var country=document.getElementById(strCountryId).value;
    if(country=='USA')
    {
    var zip=document.getElementById(strID).value;
    var regexzip=/^\d{5}(-\d{4})?$/;
    return regexzip.test(zip)
    }
    else
    {
    return true;
    }
}

function getDateObject(str,seperator)
{
var arr=str.split(seperator);
//var myDate=new Date(arr[2],arr[0],arr[1]);//year,month,day
var myDate=new Date();
myDate.setUTCFullYear(arr[2]);
myDate.setUTCMonth(arr[0]-1);
myDate.setUTCDate(arr[1]);
return myDate;
}

// Manually Server post back
function DoServerPostBack() { 
    
    try{ 
        var currentForm = document.forms['aspnetForm'];        
        if (!currentForm) {
            currentForm = document.aspnetForm;
        }
        currentForm.submit();  
          
    }catch(e)
    {
        //alert(e);
    }
}

//check matching on two text
 function IsEqualText(text1ID,text2ID){
   var strText1=window.document.getElementById(text1ID).value.trim();
   var strText2=window.document.getElementById(text2ID).value.trim();
   if(strText1==strText2){
   return true;
   }
   else
   {
   return false;
   }
     
 }

//satrt of validating date
var dtCh= "/";
var minYear=1900;
var maxYear=3000;

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 IsDate(strID){
    dtStr=window.document.getElementById(strID).value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	if(strYear.length==2)strYear="20"+strYear
	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 : mm/dd/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)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
window.document.getElementById(strID).value=month+"/"+day+"/"+year
return true
}
////end of validating date

//Check start date larger or not
function IsStartDateLarger(startDateID,endDateID)
{
   var startDate=new Date();
   startDate=getDateObject(window.document.getElementById(startDateID).value,"/");
   var endDate=new Date();
   endDate=getDateObject(window.document.getElementById(endDateID).value,"/")
   var bln;
   if(startDate > endDate)
     {
     bln=1;
     }
     else
     {
     bln=0;
     }
    return bln;
}
//Check Start Date Greater Than To day or not
function IsExpireDateLowerThanCurrent(toDayStr,endDateID)
{
    var toDay=new Date();   
    toDay=getDateObject(toDayStr,"/")
    var endDate=new Date();
   endDate=getDateObject(window.document.getElementById(endDateID).value,"/")

   if(endDate < toDay)
     {
     bln=1;
     }
     else
     {
     bln=0;
     }
    return bln;
}
