﻿function isNumeric(e)
{
    var charCode = (e.which) ? e.which : e.keyCode;
    
    if ((charCode >= 48 && charCode <= 57) || (charCode == 8 || charCode == 9 || charCode == 37 || charCode == 39))
        return true;

    return false;
}

function isDecimal(e)
{
   var charCode = (e.which) ? e.which : e.keyCode;
    
    if ((charCode >= 48 && charCode <= 57) || (charCode == 8 || charCode == 9 || charCode == 37 || charCode == 39 || charCode == 46))
        return true;

    return false;
}

function isDecimalValue(ctl)
{
    var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
    
    return reFloat.test(Trim(ctl.value, ' '));
}

function Trim (inputString, removeChar) 
{ 
    var returnString = inputString; 
    
    if (removeChar.length) 
    {
        while(''+returnString.charAt(0)==removeChar)
        {
            returnString=returnString.substring(1,returnString.length);
        }
        while(''+returnString.charAt(returnString.length-1)==removeChar)
        {
            returnString=returnString.substring(0,returnString.length-1);
        }
    }
    
    return returnString;
}

function isValidEmail(email)
{
     var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	 var returnval=emailfilter.test(email);
	 return returnval;
}

function isValidURL(ctl)
{
    var url = ctl.value;
    
    var toMatch = /[http:\/\/]*[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
    
    if (toMatch.test(url))
    {
        return true;
    }
    
    return false;
}

function valImage(ctl)
{
	var filePath = ctl.value;
 
	var validExtensions = new Array();
	var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase(); 

	validExtensions[0] = 'jpg';
	validExtensions[1] = 'jpeg';

	validExtensions[2] = 'bmp';
	validExtensions[3] = 'png';

	validExtensions[4] = 'gif';
	validExtensions[5] = 'tif'; 

	for(var i = 0; i < validExtensions.length; i++) 
	{
		if(ext == validExtensions[i]) return true;
	}

	return false;
}

function IsGridSelected(grdId, chkId, startVal)
{
    var chk;

    if(startVal>9)
    {
        chk = document.getElementById(grdId + startVal + '_' + chkId);
    }
    else
    {
        chk = document.getElementById(grdId + '0' + startVal + '_' + chkId);
    }
    
    while(chk!=null)
    {
        if(chk.checked)
        {
            return true;
        }
        
        startVal++;
        
        if(startVal>9)
        {
            chk = document.getElementById(grdId + startVal + '_' + chkId);
        }
        else
        {
            chk = document.getElementById(grdId + '0' + startVal + '_' + chkId);
        }
    }
    
    return false;
}

function valGridDelete(gridId, startIndex)
{
    if(!IsGridSelected(gridId + '_ctl', 'chk', startIndex))
    {
        alert("Please select an item");
        return false;
    }
    
    return confirm('Are you sure?');    
}
function valGridApprove(gridId, startIndex)
{
    if(!IsGridSelected(gridId + '_ctl', 'chk', startIndex))
    {
        alert("Please select an item");
        return false;
    }
    
    return confirm('Are you sure to Approve?');    
}
function isEmpty(ctl)
{
    if(Trim(ctl.value, ' ') == "")
    {
        return true;
    }
    return false;
}

function isNull(ctl)
{
    if(ctl.value == null)
    {
        return true;
    }
    return false;
}

function isEmail(ctl)
{
    if(isValidEmail(Trim(ctl.value, ' ')))
    {
        return true;
    }
    return false;
}

function isMatch(ctl1, ctl2)
{
    if(Trim(ctl1.value, ' ') == Trim(ctl2.value, ' '))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function isSelected(ctl)
{
    if(Trim(ctl.value, ' ') == "-1")
    {
        return false;
    }
    return true;
}

function isUnsafe(e)
{
    var charCode = (e.which) ? e.which : e.keyCode;
    
    var compareChar = String.fromCharCode(charCode);
    
    var unsafeString = "~`!@#$%^&*()+=|\}]{[:;\"':;<,>.?/";
    
    if (unsafeString.indexOf(compareChar) != -1)
    {
        alert("Character is not allowed");
        return false;
    }
    return true;
}

function isUnsafeString(ctl)
{
    if(!ctl.value.match(/[^a-zA-Z0-9_-]/))
    {
        return false;
    }    
    return true;
}

function controlEnter(obj, event)
{
    var key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    
    if(key == 13)
    {
        document.getElementById(obj).click();
        return false;
    }
    else
        return true;
}

function mkVisible(id)
{
    var ctl = document.getElementById(id);
    
    if(ctl == null)
    {
        ctl = document.getElementById("ctl00_content_" + id);
    }
    if(ctl != null)
    {
        ctl.style.visibility='visible';
        ctl.style.display='block';
    }
}

function mkHide(id)
{
    var ctl = document.getElementById(id);
    
    if(ctl == null)
    {
        ctl = document.getElementById("ctl00_content_" + id);
    }
    if(ctl != null)
    {
        ctl.style.display='none';
        ctl.style.visibility='hidden';
    }
}


function chkAll(gridId, chkId, startIndex)
{
debugger;
    var chkAll = document.getElementById(chkId);    
    
    var ctlStr = gridId + "_ctl";
    
    var chk = document.getElementById(ctlStr + "0" + startIndex + "_chk");
    
    var index = startIndex;
    
    var chkCtlStr;
    
    while(chk != null)
    {
        if(chkAll.checked == true)
        {
            chk.checked = true;
        }
        else
        {
            chk.checked = false;
        }
        
        index++;
        
        if(index < 10)
        {
            chkCtlStr = ctlStr + "0" + index + "_chk";
        }
        else
        {
            chkCtlStr = ctlStr + index + "_chk";
        }
        
        chk = document.getElementById(chkCtlStr);
    }
}

function chkEmptyUpdate(id)
{
    var ctl = document.getElementById(id);
    
    if(isEmpty(ctl))
    {
        alert("Field can't left blank");
        ctl.focus();
        return false;
    }
    
    return true;
}

function addImage()
{
    var divImage = document.getElementById("divImage");
    
    var filImage = document.getElementById("ctl00_ContentPlaceHolder2_FileUpload1");
    
    var newFilImage = filImage.cloneNode(false);
    
    newFilImage.value = null;
    
    newFilImage.id += "_0";
    
    newFilImage.name = newFilImage.id;
    
    divImage.appendChild(newFilImage);
    
    var br = document.createElement("br");
    
    divImage.appendChild(br);
    
    return true;
}
function editImage()
{
    var hdImgCount = document.getElementById("ctl00_content_hdImgCount");
    
    if((parseInt(hdImgCount.value) + getFileCount()) >= 6)
    {
        alert("Limit exceeded");
        return false;
    }
    
    var divImage = document.getElementById("divImage");
    
    var filImage = document.getElementById("ctl00_content_filImage");
    
    var newFilImage = filImage.cloneNode(false);
    
    newFilImage.value = null;
    
    newFilImage.id += "_0";
    
    newFilImage.name = newFilImage.id;
    
    divImage.appendChild(newFilImage);
    
    if(document.all)
    {
        var br = document.createElement("br");
    }
    
    divImage.appendChild(br);
    
    return true;
}

function getFileCount()
{
	var node_list = document.getElementsByTagName('input');
	
	var count = 0;
	
	for(var i = 0; i < node_list.length; i++)
	{
		var node = node_list[i];
		
		if(node.getAttribute("type") == "file")
		{
			count++;
		}
	}
	
	return count;
}

function disableKeyPress(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

function showDiv(ancId)
{
    var div = document.getElementById(ancId.replace(/anc/, "div"));
    
    div.style.visibility = (div.style.visibility == "hidden" ? "visible" : "hidden");
    div.style.display = (div.style.display == "none" ? "block" : "none");
}
