﻿var popUpWin;
var popUpWinForRedeem;
var Btn="";
//Category
function DeleteCategory()
{
    var flag=false;
    flag=confirm("Are you sure to delete this Category?");
    if(flag==true)
    {
        return true;
    } 
    return false;
}
//Index.aspx
function validate()
        {
           if(document.getElementById(txtuser).value=="")
           {
             errMsg.innerHTML="Invalid Username or Password !<br>Please Try again.";
             document.getElementById(txtuser).focus();
             return false;
           }
           
           if(document.getElementById(txtpwd).value=="")
           {
             errMsg.innerHTML="Invalid Password or Password !<br>Please Try again.";
             document.getElementById(txtpwd).focus();
             return false;
           }
        }
     var confirmwin =null;
     function Pop_Window()
        {            
            confirmwin = window.open('forgotpassword.aspx','anycontent','width=500,height=280,top=100,left=200,menubar=No,resizable=0');
        }
        window.onunload=function()
        {
        if(confirmwin != null)
        {
            if(typeof confirmwin != 'undefined')
            confirmwin.close();
         }
        }
        
//Manage BG Members
var previousIndex=0;
function getPrevious(ctrl)
{ 
  previousIndex= ctrl.selectedIndex; 
}
function ConfirmChange(ctrl , currentvalue)
{         
    var flag=false;
    flag=confirm("Are you sure to change the status ?");  
    if(flag==true)
    {
       setTimeout('__doPostBack(\''+ctrl.name+'\',\'\')', 0);
    } 
    else
    {     

     ctrl.selectedIndex=currentvalue;     
    }   
}

//Manage Purchase
function DeleteInvoice()
{
    var flag=false;
    flag=confirm("Are you sure to delete this Invoice?");
    if(flag==true)
    {
        return true;
    } 
    return false;
}
//Manage User
function DeleteUser()
{
    var flag=false;
    flag=confirm("Are you sure to delete this User ?");
    if(flag==true)
    {
        return true;
    } 
    return false;
}
//SA Admin More
 
function OpenView(invoiceno)
{
  if(popUpWin)
  {
     if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = window.open("invoiceapprove.aspx?invoiceno="+invoiceno+"",'popUpWin', 'width=400,height=500,top=100,left=200,menubar=No,resizable=0');
}

function OpenRedeemWindow(invoiceno)
{
  if(popUpWin)
  {
     if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = window.open("RedeemInvoiceApprove.aspx?invoiceno="+invoiceno+"",'popUpWin', 'width=400,height=420,top=100,left=200,menubar=No,resizable=0');
}


//For check boxes
function ToggleCheck(parentCtrl,seltag)
{
try {
        var status=false;
        var ctrl=document.getElementById(parentCtrl);
        var len=ctrl.getElementsByTagName('input');
    	
        if(seltag.checked==true)
        {
            status=true;
        }

        for(var loop=0;loop<len.length;loop++)
        {
            if(len[loop].type=='checkbox'){
	            if(status==true){
		            len[loop].checked=true;
	            }
	            else{
		            len[loop].checked=false;
	            }
            }
        }
    }
    catch(e)
    {
    }	        
}

//Clear check boxes        
function ClearCheck(parentCtrl)
{
try{            
    var status=true;
    var ctrl=document.getElementById(parentCtrl);
    var len=ctrl.getElementsByTagName('input');
    
    for(var loop=1;loop<len.length;loop++){
        if(len[loop].type=='checkbox'){
               if(len[loop].checked==false){
                    status = false;
                    break;
                }			        
        }
    }
    
    if(status==false)
    {
        if(len[0].type=='checkbox'){
            len[0].checked = false;
        }
    }
    else{
        if(len[0].type=='checkbox'){
            len[0].checked = true;
        }
    }
}
catch(e)
{
}
}

//Manage Vendor
function DeleteVendor()
{
    var flag=false;
    flag=confirm("Are you sure to delete this Vendor ?");
    if(flag==true)
    {
        return true;
    } 
    return false;
}
//print report
 function popUp(reportType)
    {
    
      var strUrl,left, top, width, height;
      
      strUrl='printreport.aspx?reportType='+reportType;
      left=150;
      top=10; 
      width=700;
      height=700;
      
      
      if(popUpWin)
      {
        if(!popUpWin.closed) popUpWin.close();
      }
      
      popUpWin = open(strUrl, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

    }
    
    //send message
    function validateMessage()
        {         
           if(document.getElementById(txtSubject).value=="")
           {
             errMsg.innerHTML="This message has no subject.";
             errMsg.className="errorMsg";
             document.getElementById(txtSubject).focus();
             return false;
           }
        }
      
   
   function trim(str) {
   try{
        return str.replace(/^\s+|\s+$/g,"");
      } catch(e)
        {
    
        }
    }  
    
   function checkEmptyText(message,ctrl) {   
   if(trim(document.getElementById(ctrl).value) == ""){
        alert(message);
        return false;
    }
    else
        return true;
   }
   
   function checkEmptyMessage(ctrl1,ctrl2,ctrl3)
    {      
       if(document.getElementById(ctrl1).selectedIndex == 0)
       {
            alert('Please select a User group for sending mail');
            document.getElementById(ctrl1).focus();
            return false;
       }         
     else if(trim(document.getElementById(ctrl2).value) == "")
       {
         alert('Please enter subject');
         document.getElementById(ctrl2).focus();
            return false;
       }
       else if(trim(document.getElementById(ctrl3+"_RTE").contentWindow.document.body.innerText)== "")
       {
            var cont=false;
            cont=confirm('The mail content is empty. Do you want to send it anyway ?');
            if(cont == true)
            {
                return true;
            }
            else
            {                        
                return false;                
            }
      
       }
       else
       {
        return true;
       }
   }
   // Part of html integration
function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//manage purchase
function ValidateVendor(source, arguments)
{
    if(document.getElementById(ddlcontrol).selectedIndex==0)
    {
        arguments.IsValid=false;
        return;
    }
    else
    {
       arguments.IsValid=true;
       return;
    }
}

//manage purchase
function ValidateAmount(source, arguments)
{
for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
summary = Page_ValidationSummaries[sums];
}

       var valAmount = new RegExp("^\\d*\\.{0,1}\\d+$");
     if(arguments.Value=="")
     {
            summary.headertext = "Please input data on all mandatory fields.";
            summary.displaymode = "SingleParagraph";
             arguments.IsValid=false;
            return;
     }
     else if (arguments.Value.match(valAmount) == null)  {
     
            summary.headertext = "Amount should be numeric(eg: 100.00)";
            summary.displaymode = "SingleParagraph";
            arguments.IsValid=false;
            return;
        } else {
          
            summary.headertext = "Please input data on all mandatory fields.";
            summary.displaymode = "SingleParagraph";
            arguments.IsValid=true;
            return;
     
      }

    

}

function ClearData()
{

    try{            
        var status=true;
        var len=document.getElementsByTagName('input');
        for(var loop=1;loop<len.length;loop++)
        {
            if(len[loop].type=='text')
            {
               len[loop].value = '';	        
            }
        }
    }
    catch(e)
    {
    }
}
function calcPoints(cntrl1,cntrl2,cntrl3)
{
var TotalPoints=document.getElementById(cntrl1).value;
document.getElementById(cntrl2).innerHTML= roundNumber((TotalPoints * .75),2);
document.getElementById(cntrl3).innerHTML= roundNumber((TotalPoints * .25),2);
}

//Multiple Purchase
function OpenSearchBguser()
{ 
    if(document.getElementById('SelectBguser').value != "")
    {
        SampleControlName= SampleControlName.replace(/Sample/i,'')
        var parentcontrols=document.getElementById('SelectBguser').value.split(',');
        var CntrlName=SampleControlName + parentcontrols[0];
        var CntrlCode=SampleControlName + parentcontrols[1];
       if(popUpWin)
        {
            if(!popUpWin.closed) popUpWin.close();
        }
        popUpWin = window.open("searchBuyingGroup.aspx?cmpName='"+CntrlName+"'&cmpCode='"+CntrlCode+"'",'popUpWin', 'width=416,height=475,top=100,left=200,menubar=No,scrollbar=yes,resizable=0');
    }
    else
    {
    alert("Select the textbox where you want to enter the buying group details. Then click on this link.")
    }
   }
   
//CR on 9 Sept 2009 - Function added for Redeem list popup 
function OpenSearchBguserForRedeem()
{
    if(document.getElementById('SelectedBGForRedeem').value != "")
    {        
        var parentcontrols=document.getElementById('SelectedBGForRedeem').value.split(',');
        var CntrlName = parentcontrols[0];
        var CntrlCode = parentcontrols[1];
        var CntrlTotalPoints = parentcontrols[2];
       if(popUpWinForRedeem)
        {
            if(!popUpWinForRedeem.closed) popUpWinForRedeem.close();
        }
        popUpWinForRedeem = window.open("searchBuyingGroup.aspx?openerType=RedeemList&cmpName='"+CntrlName+"'&cmpCode='"+CntrlCode+"'&totalpointHolder='"+ CntrlTotalPoints +"'" ,'popUpWin', 'width=416,height=475,top=100,left=200,menubar=No,scrollbar=yes,resizable=0');
    }
    else
    {
    alert("Select the textbox where you want to enter the buying group details. Then click on this link.")
    }
}
   

function SelectBGUser(companyName,companyCode,cntrl1,cntrl2)
{ 
    try{  
       window.opener.document.getElementById(cntrl1).value =companyName.replace(/&#39;/g,"'");
       window.opener.document.getElementById(cntrl2).value = companyCode.replace(/&#39;/g,"'");
       window.opener.document.getElementById(cntrl1).focus();
       window.close();
       }
       catch(e)
       {
       window.close();
       }
}

//CR on 9 Sept 2009 - Function added for Redeem list popup call back
function SelectBGUserForRedeem(companyName,companyCode,totalPoints,cntrl1,cntrl2,cntrl3)
{ 
    try{  
       window.opener.document.getElementById(cntrl1).value =companyName.replace(/&#39;/g,"'");
       window.opener.document.getElementById(cntrl2).value = companyCode.replace(/&#39;/g,"'");
       window.opener.document.getElementById(cntrl3).value = totalPoints.replace(/&#39;/g,"'");
       window.opener.document.getElementById(cntrl1).focus();
       window.close();
       }
       catch(e)
       {
       window.close();
       }
}

function SetFocus(cntrl)
{
    SampleControlName= SampleControlName.replace(/Sample/i,'')
    document.getElementById(SampleControlName+cntrl).focus();
}

 function AllowDecimal(e) 
  {
    var charCode = (e.which) ? e.which : window.event.keyCode
    if((charCode > 47 && charCode < 58) || charCode==8 || charCode==0 || charCode == 9 || charCode == 46 ) 
        return true ; 
    return false;

 }
 function AllowMoney(e) 
 {
    var charCode = (e.which) ? e.which : window.event.keyCode
    if((charCode > 47 && charCode < 58) || charCode==8 || charCode==0 || charCode == 9 || charCode == 46 || charCode == 36 ) 
        return true ; 
    return false;

}   
  function ValDecimalValue(value) {
        if(value== "")
        {
        return false;
        }
        var valAmount = new RegExp("^\\d*\\.{0,1}\\d+$");
        if (value.match(valAmount) == null) 
        {
           return false;        
        }
        else
        {
            return true;
        }
  }
  
   function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
  }
    function addElement(Cntrl) {
    
       var numi = document.getElementById(Cntrl);
       var newspan = document.createElement('span');
       var divIdName = "Err_"+ Cntrl;
       newspan.setAttribute('id',divIdName);
       if(document.getElementById(divIdName)== null)
       {
           newspan.innerHTML = "<bold><font color='red' size='4px'>*</font></bold>";
           numi.appendChild(newspan);
       }
  }
  function removeElement(Cntrl) {
      var d = document.getElementById(Cntrl);
      var olddiv = document.getElementById("Err_"+ Cntrl);
      if(d != null && olddiv != null)
      {
        d.removeChild(olddiv);
      }
 }
 function ValBguser(cntrl1,cntrl2) {
    SampleControlName= SampleControlName.replace(/Sample/i,'');
    var ControlValueName= document.getElementById(SampleControlName+cntrl1).value;
    var ControlValueCode= document.getElementById(SampleControlName+cntrl2).value;
    if(ControlValueName != "")
    {
         removeElement(SampleControlName+"td_"+cntrl1);
    }
    if(ControlValueCode != "")
    {
         removeElement(SampleControlName+"td_"+cntrl2);
    }
    
    }
    function DisableKeypress(e)
    {
  
        var charCode = (e.which)!= null ? e.which : window.event.keyCode
        if(charCode == 9 ) 
        return true ; 
        return false;

    }
    
    //Manage News
function DeleteArticle()
{
    var flag=false;
    flag=confirm("Are you sure to delete this article ?");
    if(flag==true)
    {
        return true;
    } 
    return false;
}

function OpenPolicies()
{
  if(popUpWin)
  {
     if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = window.open("termsandcondition.html",'popUpWin', 'width=550,height=550,top=100,left=100,menubar=No,resizable=0');
}