﻿// whitespace characters
var whitespace = " \t\n\r";

function openUrlInWindow(strURL)
{
    window.location.href=strURL;
}
    
function RequiredField(valControl, valLabel, valMessage)
{
    
    var ctrl = document.getElementById(valControl);
    if(ctrl != null)
    {
        ctrl.value=trimAll(ctrl.value);
        
        if(ctrl.value.length==0)
        {
            document.getElementById(valLabel).innerHTML=valMessage;
            ctrl.focus();
            return false;
        }
        
        return true;
    }
    else
    {
        return true;
    }
}

function RequiredFieldWithoutFocus(valControl, valLabel, valMessage)
{
    var ctrl = document.getElementById(valControl);
    ctrl.value=trimAll(ctrl.value);
     if(ctrl != null)
    {
      
        if(ctrl.value.length==0)
        {
            document.getElementById(valLabel).innerHTML=valMessage;
           
            return false;
        }
        return true;
    }
    else
    {
      return true;
    }
    
    
}

// function for dropdown list 
function RequiredFieldforDropDownList(valControl, valLabel, valMessage)
{
    var ctrl = document.getElementById(valControl);
    if(ctrl != null)
    {
    
        if(ctrl.selectedIndex == 0  )
        {
            document.getElementById(valLabel).innerHTML=valMessage;
            ctrl.focus();
            return false;
        }
        return true;
    }
    else
    {
        return true;
    }
}

function RequiredFieldForListControls(valControl)
{
    var ctrl = document.getElementById(valControl);
      if(ctrl != null)
    {
        ctrl.value=trimAll(ctrl.value);
        
    //    if(ctrl.value.length==0)
        if(!ctrl.checked)
        {
           return false;
        }
        return true;
        }
      else
    {
        return true;
    }
}


function trimAll(sString)
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function ValidateEmail(valControl, valLabel, valMessage)
{
      var ctrl = document.getElementById(valControl);
      ctrl.value=trimAll(ctrl.value);
      var strvalue= ctrl.value;
      
      var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
      var regex = new RegExp(emailReg);
      
      if(regex.test(strvalue))
        {
           return true;
        }
     else
        {
          document.getElementById(valLabel).innerHTML=valMessage;
          ctrl.focus();
          return false;
        }
}



function ValidatePhoneNumber(valControl, valLabel, valMessage)
{
      var ctrl = document.getElementById(valControl);
      if(ctrl==null)
        return true;
        
      ctrl.value=trimAll(ctrl.value);
      var strvalue1= ctrl.value;
      
      var phno1 = /^[0-9]\d{2}-\d{3}-\d{4}$/
      var phno2 = /^[0-9]\d{2}-\d{4}$/
      var regex1 = new RegExp(phno1);
      var regex2 = new RegExp(phno2);
      if(strvalue1.length>0)
      {
          if(regex1.test(strvalue1) || regex2.test(strvalue1) )
            {
               return true;
            }
         else
            {
              document.getElementById(valLabel).innerHTML=valMessage;
              ctrl.focus();
              return false;
            }
       }
       else 
       {
       return true;
       } 
        
}

function ValidateSSNNumber(valControl, valLabel, valMessage)
{
      var ctrl = document.getElementById(valControl);
      if(ctrl==null)
        return true;
        
      ctrl.value=trimAll(ctrl.value);
      var strvalue1= ctrl.value;
      
      var phno1 = /^[0-9]\d{2}-\d{2}-\d{4}$/
      //var phno2 = /^[0-9]\d{2}-\d{4}$/
      var regex1 = new RegExp(phno1);
     // var regex2 = new RegExp(phno2);
      if(strvalue1.length>0)
      {
          if(regex1.test(strvalue1) )
            {
               return true;
            }
         else
            {
              document.getElementById(valLabel).innerHTML=valMessage;
              ctrl.focus();
              return false;
            }
       }
       else 
       {
       return true;
       } 
        
}


//**************** Validate **************
function ValidateDate(str)
{

		
		var dateStr1=str;
					
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
		
		var matchArray1 = dateStr1.match(datePat); // is the format ok?
		var ma1=dateStr1
		maarray1=ma1.match(datePat);
		if (matchArray1 == null) 
			{
				//alert("Date1 is not in a valid format.")
				return false;
			}
		
		month1 = matchArray1[1]; // parse date into variables
		day1 = matchArray1[3];
		year1 = matchArray1[4];
		yea1=year1.length
		if(yea1 < 4)
		{
			//alert("Date1 should be in mm/dd/yyyy format");
			return false;
		}
		
	if (month1 < 1 || month1 > 12) 
		{ 
			// check month range
			//alert("Date1 Error: Month must be between 1 and 12.");
			return false;
		}
	if (day1 < 1 || day1 > 31) 
		{
			//alert("Date1 Error: Day must be between 1 and 31.");
			return false;
		}

	if ((month1==4 || month1==6 || month1==9 || month1==11) && day1==31) 
		{
			//alert("Date1 Error: Month in Date1  doesn't have 31 days!");
			return false;
		}
	if (month1 == 2) 
		{ // check for february 29th
			var isleap = (year1 % 4 == 0 && (year1 % 100 != 0 || year1 % 400 == 0));
			if (day1==29 && !isleap) 
				{
					alert("February " + year1 + " doesn't have " + day1 + " days! \n As it is not a Leap Year");
					return false;
			   }
			if (day1>29) 
				{
					alert("February " + year1 + " doesn't have " + day1 + " days!");
					return false;
			   }   
		}

		return true;

}
//**************** Validate **************




 function SpecialChar(valControl, valLabel, valMessage)
{
    var ctrl = document.getElementById(valControl);
    ctrl.value=trimAll(ctrl.value);
    
	var strvalue= valControl.value;
	strlength= strvalue.length;
	var iChars = "`~!@#$%^&*+=\\\';,.{}|\":<>?";
		for (var i = 0; i < strlength; i++)
         {
                if (iChars.indexOf(strvalue.charAt(i)) != -1)
                {       
                         document.getElementById(valLabel).innerHTML=valMessage;
                         ctrl.focus();
                         return false;
		        }
         }
}

function ValidateURL(valControl, valLabel, valMessage)
{
    var ctrl = document.getElementById(valControl);
    ctrl.value=trimAll(ctrl.value);
    var strvalue= document.getElementById(valControl).value;
    var emailReg = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
     
      if(emailReg.test(strvalue))
        {
           return true;
        }
     else
        {
          document.getElementById(valLabel).innerHTML=valMessage;
          ctrl.focus();
          return false;
        }

}

// checks Numeric text
function f_isNumber(str)
{
	nstr = '0123456789';
    err = 0;
    for (f=0;f<str.length;f++){
      if (nstr.indexOf(str.charAt(f)) == -1) err++;
    }
    if (err!=0) return false;
    else return true;
}


// Function to check for Entered digit should be numbers only.
function numbersonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode
    // if (unicode!=8||unicode!=9)
    if (unicode<8||unicode>9)
    {
        //if the key isn't the backspace key or tab key (which we shouldallow)
        if (unicode<48||unicode>57) //if not a number
            return false //disable key press
    }
}

// Function to check for Entered digit should be Alphabets only.
function digitsonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode
    // if (unicode!=8||unicode!=9)
    if (unicode<8||unicode>9)
    {
        //if the key isn't the backspace key or tab key (which we shouldallow)
       if (unicode < 65 || unicode > 90 && unicode < 97 || unicode > 122) //if not a digit
         return false //disable key press
     }
}



/****************************************************************/
// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
/****************************************************************/
// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    
    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function doClick()
{
    if(document.getElementById('frameContent')==null)
        return;
    
    var x, y, msg='';

    // for all except Explorer
    if (self.innerHeight) {
    x = self.innerWidth;
    y = self.innerHeight;
    msg += 'self.innerHeight/Width: '
    + y + ', ' + x + 'px';

    // Explorer 6 Strict Mode
    } else if (document.documentElement
    && document.documentElement.clientHeight) {
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
    msg += 'document.documentElement.clientHeight/Width: '
    + y + ', ' + x + 'px';

    // other Explorers
    } else if (document.body) {
    x = document.body.clientWidth;
    y = document.body.clientHeight;
    msg += 'document.body.clientHeight/Width: '
    + y + ', ' + x + 'px';
    }
    
    var the_height=parseInt(document.getElementById('frameContent').contentWindow.document.body.scrollHeight);
    
    setTimeout('InitializeLayout('+x+','+the_height+');',100);
}

function InitializeLayout(x,y)
{    
    var dvMain = document.getElementById("frameContent");
    dvMain.style.height=(y)+'px';
}


