/*****
* Create Date               : 2002/03/30
* Company name              : SUTRA Systems 
******/




// Function Description      : Used in all page which where used input dates 
function popcal(vField, vDate) { 
    var nW = window.open("", "calendar", "resizable=no,scrollbars=no,width=230,height=200"); 
    nW.location.href = "/Common/calendar.asp?field=" + vField + "&date=" + vDate; 
    nW = null; 
}


function popcal_new(vField, vDate) { 
    var nW = window.open("", "calendar", "resizable=no,scrollbars=no,width=230,height=200"); 
    nW.location.href = "/Common/calendar_new.asp?field=" + vField + "&date=" + vDate; 
    nW = null; 
}




// Function Description      : Called on click of enter key
function onEnterClick(e) {
  var whichCode = (window.Event) ? e.which : e.keyCode;     
  var strCheck = '13' ; 
    if (strCheck.indexOf(whichCode) != -1)
    {
        return fnbtnAddNewClick();
    }
}






// Description          : Called on cilck of button "Close" (Common Function)
function fnbtnClose_Click()
{
    //if ( confirm( strMessage[16] ) )
    //{
        window.close() ;
    //}
}





// Description          : Called on cilck of button "Reset"  (Common Function)
function fnbtnReset_Click()
{
    //if ( confirm( strMessage[14] ) )
    //{
        document.thisForm.reset() ;
    //}
}






// Description          : Function for submitting a form  (Common Function)
function fnSubmitForm(strAction, strMethod, strTarget, sourceName) 
{
    document.thisForm.method = strMethod ;
    document.thisForm.target = strTarget ;
    document.thisForm.action = strAction ;
    document.thisForm.sourceName = sourceName;  
    document.thisForm.submit() ;
}





// Function Description      : Alerts message and sets focus on the textbox
function fnAlertMessage(TextBox, ErrorMessage, blnHidden)
{
    alert(ErrorMessage) ;
    if ( blnHidden == false )
    {
        TextBox.focus();
        TextBox.select();
    }
}




// Function Description      : function used to remove the space left and right
function fnRemoveAllSpaces(string) {
    var temp = "";
    string = '' + string;
    splitstring = string.split(" ");
    for(i = 0; i < splitstring.length; i++)
        temp += splitstring[i];
    return temp;
}





// Function Description      : removes extra white space
function remove_excess_whitespace(item)
{
  var tmp = "";
  var item_length = item.length;
  var item_length_minus_1 = item.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.charAt(index) != ' ')
    {
      tmp += item.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.charAt(index);
        }
      }
    }
  }
  return tmp ;
}





// Description          : function used to check the textbox data length, checks for double byte data also
function isTextBoxLengthMore(TextBox, iLen, ErrorMessage, blnHidden)
{
    var strVal = new String(TextBox.value);
    var i;
    var len = 0;
    for(i=0; i < strVal.length; i++) 
    {
        if (escape(strVal.charAt(i)).length >= 4 ) 
        {
            len = len + 2;
        }else 
        {
            len++;
        }
    }
    if (len > iLen) 
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true ;
    }
    return false ;
}






// Function Description      : function used to check the if input data is empty
function isEmptyTextBox(TextBox, ErrorMessage, blnHidden)
{   

    if (TextBox == null)
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true;
    }
    else 
    {
        TextBox.value = remove_excess_whitespace(TextBox.value) ;
        var strTemp = TextBox.value;
        if( strTemp.length == 0 )
        {
            fnAlertMessage( TextBox, ErrorMessage, blnHidden );
            return true;
        }
    }
    return false;
}






// Function Description      : returns true if password textbox is empty
function isEmptyTextBoxPassword(TextBox, ErrorMessage, blnHidden)
{
    if (TextBox == null)
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true;
    }
    else
    {
        var strTemp = TextBox.value;
        if( strTemp.length == 0 )
        {
            fnAlertMessage( TextBox, ErrorMessage, blnHidden );
            return true;
        }
    }
    return false;
}











// Function Description      : Function to check for special characters in a string
function ContainsSpecialCharacters(val, regexp) {
    var exp = regexp ;
    var r = val.search(exp);
    if(r == -1)
    {
        return false;
    }
    return true;
}





// Function Description      : Function to check for special characters in a string
function TextBoxContainsSpecialCharacters(TextBox, regexp, ErrorMessage, blnHidden)
{
    if( ContainsSpecialCharacters(TextBox.value, regexp) == true )
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return false ;
    }
    return true ;
}





// Function Description      : Function to check is date is within From Date and To Date
function WithinDateRange(TextBox, strFromDate, strToDate, strMessage, blnHidden)
{
    var strDate = TextBox.value;
    if ( Date.parse(strDate) < Date.parse(strFromDate) || Date.parse(strDate) > Date.parse(strToDate) )
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return false;
    }
    return true ;
}




// Function Description      : Function to check for spaces in a string
function TextBoxContainsSpacesInBetween(TextBox, ErrorMessage, blnHidden)
{
    if ( TextBox.value.split(" ").length > 1 ) 
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden )
        TextBox.value = fnRemoveAllSpaces(TextBox.value);
        return true;
    }
    return false ;
}




// Function Description      : Function to check for valid numeric
function isNotPositiveNumeric(TextBox, ErrorMessage, blnHidden, lowerlimit){
    TextBox.value = fnRemoveAllSpaces(TextBox.value);
    var Val = TextBox.value;
    if(  Val == "" )
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true;
    }
    if(  isNaN(Val)  )
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true;
    }
    else if ( parseFloat(Val) <= parseFloat(lowerlimit) )
    {
        fnAlertMessage( TextBox, ErrorMessage, blnHidden );
        return true;
    }
    return false;
}





function isFromDateLaterThanToDate(TextFromDate, TextToDate, ErrorMessage, blnHidden)
{
    var strFromDate = TextFromDate.value ;
    var strToDate = TextToDate.value ;
        
    if ( Date.parse(strFromDate) > Date.parse(strToDate) )
    {
        fnAlertMessage( TextFromDate, ErrorMessage, blnHidden );
        return true;
    }
    return false ;
}





function isFromNoGreaterThanToNo(TextFromNo, TextToNo, ErrorMessage, blnHidden)
{
    var strFromNo = TextFromNo.value ;
    var strToNo = TextToNo.value ;
    
    if ( parseFloat(strFromNo) > parseFloat(strToNo) )
    {
        fnAlertMessage( TextFromNo, ErrorMessage, blnHidden );
        return true;
    }
    return false ;
}



// Function Description      : returns true if textbox contents are same
function TextBoxContentsTally(TextBox1, TextBox2, ErrorMessage, blnHidden)
{
    if ( TextBox1.value != TextBox2.value )
    {
        fnAlertMessage( TextBox1, ErrorMessage, blnHidden );
        return false;
    }
    return true ;
}






// Function Description      : returns true if textbox contents are same
function OldAndNewPasswordsAreSame(TextBox1, TextBox2, ErrorMessage, blnHidden)
{
    if ( TextBox1.value == TextBox2.value )
    {
        fnAlertMessage( TextBox1, ErrorMessage, blnHidden );
        return true;
    }
    return false ;
}





// Function Description      : returns true if listbox box item is selected else returns false
function isNotListBoxSelected(ListBox, ErrorMessage)
{
    if (ListBox.selectedIndex == 0)
    {
        alert(ErrorMessage);
        ListBox.focus();
        return true ;
    }
    return false;
}



function isNotMultipleListBoxSelected(ListBox, ErrorMessage)
{
    if (ListBox.selectedIndex < 0)
    {
        alert(ErrorMessage);
        ListBox.focus();
        return true ;
    }
    return false;
}



function isMoreThanOneListBoxItemSelected(ListBox, ErrorMessage)
{
    var i = 0;
    var iSelectCount=0;
    for(i=0; i<ListBox.length; i++)
    {
        if( ListBox.options[i].selected )
        {
            iSelectCount = iSelectCount + 1;
        }
    }
    if (iSelectCount > 1)
    {
        alert(ErrorMessage);
        ListBox.focus();
        return true;
    }
    return false;
}





// Function Description      : Checks if given text is empty : return true or false
function isEmpty(TextObj)
{   
    // -- Remove all the spaces from string object and return
    var tmpText = TextObj;
    for(var i=0;i < TextObj.length;i++)
    {
         tmpText =  tmpText.replace(" ","");
    }
    return tmpText.length == 0;
}




// Function Description      : Function to check the validity of a date in format yyyy/mm/dd
function CheckDate(fulldate)
{   
    var tempdate= fulldate.split("/");  
    if(isNaN(tempdate[0]))
    {       
        return false;
    }
    if(isNaN(tempdate[1]))
    {
        return false;
    }
    if(isNaN(tempdate[2]))
    {
        return false;
    }
    
    if(isEmpty(tempdate[0]))
    {
        return false;
    }
    
    if(isEmpty(tempdate[1]))
    {
        return false;
    }
    
    if(isEmpty(tempdate[2]))
    {
        return false;
    }
    
    if((tempdate[0] < 1900) || (tempdate[0] > 9999))
    {
        return false;
    }   
    
    if(tempdate[2] > 31 || tempdate[2]<1) 
    {   
        return false;
    }
    
    if(tempdate[1] >12 || tempdate[1]<1)
    {       
        return false;
    }       
    
    if((tempdate[0] % 4==0) || ((tempdate[0] % 100==0) && (tempdate[0]%400==0)))
    {       
        if (tempdate[1] ==2)
        {
            if (tempdate[2] >29)
            {       
                return false;
            }
        }
                        
        if ((tempdate[1]==4) || (tempdate[1]==6)||(tempdate[1]==9)||(tempdate[1]==11))
        {
            if (tempdate[2] > 30)
            {               
                return false;
            }
        }
                
    }           
    else if (tempdate[1]==2)
    {   
        if (tempdate[2] > 28)
        {                   
            return false;
        }
    }   
    else if ((tempdate[1]==4)||(tempdate[1]==6)||(tempdate[1]==9)||(tempdate[1]==11))
    {
        if (tempdate[2] > 30)
        {
            return false;
        }
    }   
    return true;
}



// Function Description      : Function to check if the date is in proper format and contains valid date
function isDateNotValid(TextBoxDate, strMessage, blnHidden)
{
    var strDate = TextBoxDate.value;
    
    if ( CheckDate(strDate) == true )
    {
        return false;
    }
    else
    {
        if( blnHidden == false )
        {
            TextBoxDate.focus();
            TextBoxDate.select();
        }
        alert(strMessage);
        return true;
    }
}




function Trim(theString) {
// -- Remove the trailing and leading spaces from string object and return
    var regexpr = /^(\s+)/;
    theString = theString.replace(regexpr,"");
    regexpr = /(\s+)$/;
    theString = theString.replace(regexpr,"");
    return theString
}


function fnCheckFile(p_strFileName) {
    document.thisForm.method = "post" ;
    document.thisForm.htbActualFileName.value = p_strFileName ;
    document.thisForm.action ="CheckFileExists.asp" ;
    document.thisForm.submit();

}

function isEmailValid(strEmailId )
{
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmailId) )
    {
        return (true);
    }   
    return (false);
}

function validateEmailAddress(strCommaString) {


    var SelectedArray = new Array; 
    SelectedCommaArray = strCommaString.value.split(",");
    

    var ElementValue;
    
    for(i=0; i < SelectedCommaArray.length ; i++)   {
        
        var ElementValue1 = Trim(SelectedCommaArray[i]) ;
        
        if ( isEmailValid(ElementValue1 ) == false ) {
            alert(ElementValue1 + " : " + strMessage[8]) ;
            strCommaString.focus();
            return false;
        }

    }
    return true;


}

function setFileAndSubmit(filePath, productCode)
{
    hdnSelectedFile = document.getElementById('hdnSelectedFile');    
    hdnSelectedFile.value = filePath;
    
    if(null != productCode && 'undefined' != productCode)
    {
        hdnProductCode = document.getElementById('hdnProductCode');
        hdnProductCode.value = productCode;
    }
    
    document.forms[0].submit();    
}
