// function to pop up a new window 
function addNew(str) {  
        var tp = (screen.height-450)/2;
        var lt = (screen.width-740)/2 +100;    
        var newwin2 = window.open(''+str.name+'.jsp', ''+str.value+'', 'height=300,width=500,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');
        return true;
        }

//function checking the validity of a string
function string(str){  
    //  check for valid string 
    strString=str.value;
    var strNotValidChars = "/*-+<:>.{}[]()&^%$#@!~`,";
    var strChar;
    var blnResult = true;
    for (i = 0; i < strString.length; i++){
    strChar = strString.charAt(i);
    if (strNotValidChars.indexOf(strChar) != -1){
    blnResult = false;
    }
    if(!blnResult){ 
    alert("Enter a valid string");
    str.value="";
    str.focus();
    break;
    }
    }
    return blnResult;
    }

//function checking number validity
 function numeric(str){      //  check for valid numeric 
    strString=str.value;
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;
    for (i = 0; i < strString.length; i++){
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1){
    blnResult = false;
    }
    if(!blnResult){ 
    alert("Enter a valid number");
    str.focus();str.value="";
    break;
    }
    }
    return blnResult;
    }
//function checking validity of a url

    function validate(form) { 
    var v = new RegExp(); 
    var status=true;
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
    if (!v.test(form["publisherUrl"].value)) {  
    alert("You must supply a valid URL."); 
    status=false;  
    } 
    return status;
    } 

// function cheking if the input string is blank or not.
function isBlank(str){
                                                                                                                             
        for (var i=0; i<str.length; i++){
                if (str.charAt(i) != " ") return false;   
        }
        return true;
  }

// function cheking if the input string contains only "/n" or blank.
function enterCheck(str){
        for (var i=0; i<str.length; i++){
                if (str.charAt(i) != "\n" && str.charAt(i) != " ") return false; 
        }
        return true;
  }

function blankcheckspace(str){
        for (var i=0; i<str.length; i++){
                if (!(str.charAt(i) != "\n" && str.charAt(i) != " ")) return false; 
        }
        return true;
  }
 
function isProper(string) {
    if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
}               


// function to trim the gievn input string                                                                                                                                                                                                                                                            
function trim(inputString) {
        if (typeof inputString != "string") { 
                return inputString;
        }
        var retValue = inputString;
        var ch = retValue.substring(0, 1);
        while (ch == " ") {
                retValue = retValue.substring(1, retValue.length);
                ch = retValue.substring(0, 1);
        }
        ch = retValue.substring(retValue.length-1, retValue.length);
        while (ch == " ") {
                retValue = retValue.substring(0, retValue.length-1);
                ch = retValue.substring(retValue.length-1, retValue.length);
        }
        return retValue;
  }


// function to chek the date format (mm/dd/yyyy)
function dateCheckReport( strValue ) {
  var objRegExp = /^\d{1,2}(\/)\d{1,2}(\/)\d{1,4}$/ 
  //check to see if in correct format
  if(!objRegExp.test(strValue)){ 
        return false; 
//doesn't match pattern, bad date
  }
  else{
        var reg = new RegExp("-|/");
        var arrayDate = strValue.split(reg);
        var mon="", day="";
        if(arrayDate[0].length!=2)
                return false;
        else mon=arrayDate[0];
        if(arrayDate[1].length!=2)
                return false;
        else day=arrayDate[1];
        if(arrayDate[2].length!=4)  
                return false;
        else yr=arrayDate[2];
    //create a lookup for months not equal to Feb.
        var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
        var tempDay = day;
        if(tempDay.indexOf("0") == 0){
                tempDay = day.substring(1,day.length)

        }

        var intDay = parseInt(tempDay);

        //check if month value and day value agree
        if(arrayLookup[mon] != null) {

                if(intDay <= arrayLookup[mon] && intDay != 0)
                        return true; //found in lookup table, good date

        }
        //check for February
        var intYear = parseInt(arrayDate[2]);
        var tempMon = mon;
        if(tempMon.indexOf("0") == 0){
                tempMon = mon.substring(1,mon.length)

        }
        var intMonth = parseInt(tempMon);

        if(intMonth==2)
     if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
                        return true; //Feb. had valid number of days
        }
        return false; //any other values, bad date

}

//This is to check whether the passed date in /mm/dd/yyyy format subtracting the given number of days(from) is less than todays date .
function checkRemainderDate(str,from){

var fromDate = new Date(from)
 var todayDate =  getCurrentDate();
 var subDate =  DateSubtract(fromDate,str,0,0);
if(!compareDates(todayDate,subDate)){
return false; 
}
return true;
} 

// function to chek the email format with alerts
function emailCheck (emailStr) {
    
    if(emailStr.length <= 0){
        return true;  
    }
		
    var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)  
        
	if (matchArray==null) {
	      alert("Please Enter a valid Email");
	    return false
	}
    var user=matchArray[1]
	var domain=matchArray[2]
		
	if (user.match(userPat)==null) {
	   alert("Please Enter a valid Email");
	    return false
	}
    var IPArray=domain.match(ipDomainPat) 
	if (IPArray!=null) {
	    // this is an IP address
	    for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
		   alert("Please Enter a valid Email");
		    return false
		}
	    }
	    return true
	}
		
    // Domain is symbolic name
    var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    alert("Please Enter a valid Email");
	    return false
	}
		
    /* domain name seems valid, but now make sure that it ends in a
       three-letter word (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */
		
    /* Now we need to break up the domain to get a count of how many atoms
       it consists of. */
    var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		alert("Please Enter a valid Email");
		return false
	    }
		
    // Make sure there's a host name preceding the domain.
    if (len<2) {
	var errStr=" Email is missing a hostname!"
	    alert("Please Enter a valid Email");
	    return false
	    }
    return true;
}


// function to check the email format
function emailCheckCommon (emailStr) {
    
    if(emailStr.length <= 0){

    return true  
    }
		
    var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)  
        
	if (matchArray==null) {
	    return false
	}
    var user=matchArray[1]
	var domain=matchArray[2]
		
	if (user.match(userPat)==null) {
	    return false
	}
    var IPArray=domain.match(ipDomainPat) 
	if (IPArray!=null) {
	    // this is an IP address
	    for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
		    return false
		}
	    }
	    return true
	}
		
    // Domain is symbolic name
    var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}
		
    /* domain name seems valid, but now make sure that it ends in a
       three-letter word (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */
		
    /* Now we need to break up the domain to get a count of how many atoms
       it consists of. */
    var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		return false
	    }
		
    // Make sure there's a host name preceding the domain.
    if (len<2) {
	var errStr=" Email is missing a hostname!"
	    return false
	    }
    return true;
}



// function adds number of days , months and years to the given startdate.
function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

// function adds number of days , months and years to the given startdate.
function DateSubtract(startDate, numDays, numMonths, numYears)
{
            var returnDate = startDate;
	//var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()-60000*60*24*numDays);
	
	return returnDate;

}
// simplified function which calls the DateAdd function to add only the years
function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

// simplified function which calls the DateAdd function to add months
function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

// simplified function which calls the DateAdd function to add days.
function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}

// function which gives the current date
function getCurrentDate(){
		return (new Date());
}

// function to format the date object to mm/dd/yyyy format
function formatDate(dateObj){
    var gotDate = new Date(dateObj.getTime());
    var month = (gotDate.getMonth() + 1) + "";
    if(month.length == 1) month = "0" + month;
    var day = gotDate.getDate() + "";
    var year=gotDate.getYear();
if (year<150)
    year=year+1900;
    if(day.length == 1) day = "0" + day;
    var returnDate = month + "/" + day + "/" + year; 
    return returnDate;
}
 
// function that compares two dates - returns false if (from > to)
// from and to are values in the text field.
function compareDates(from, to) { 
    var status=true;
    if (Date.parse(from) >= Date.parse(to)) {
    alert("Enter valid dates");
        status=false;
    }  
    return status;
}

// functon to check the login on pressing the enter key
function checkLoginEnter(event){
    if(ifEnterKey(event))
        return checkLogin();
    return true;
}

// function to check the login page
function checkLogin(){
    if(isBlank(document.f1.username.value)){
        alert("Please enter the ID to login.");
        document.f1.username.focus();
        return false;
    }
if(isBlank(document.f1.password.value)){
        alert("Please enter the password to login.");
        document.f1.password.focus();
        return false;
    }
    document.f1.submit();
    return true;
}
// function sets the focus to the SunId text box on body onLoad 
function focusUser(){
    document.f1.username.focus();
}



// function to chk if the key entered is enter key
function ifEnterKey(event){
     if (document.all) {
        kcode=window.event.keyCode;
     }
     else
        kcode=event.which;

     if (kcode==13) {
        return true;
     }
     else {
        return false;
     }
}

function centerPopUp(popupPage,popW,popH){
    var w = screen.width , h = screen.height;

    if (document.all || document.layers) {
       w = screen.availWidth;
       h = screen.availHeight;
    }

    var leftPos = (w-popW)/2;
    var topPos = (h-popH)/2;

    window.open(popupPage,'popup','width=' + popW +',height='+popH+',top='+topPos+',left='+leftPos+',scrollbars=yes,resizable=yes');

}




