// Start - Click then Open Detail in List
var oldId;
function openDetails(objTr,obj)
{
   
	var newId = document.getElementById(objTr).id;
    if (oldId == null || oldId == newId)
    {
        oldId = newId;
        exCol(oldId);
    }
    else
    {
        if (document.getElementById(oldId).style.display != "none")
            exCol(oldId);
        exCol(newId);
    }
    oldId = newId;
}
function exCol(id)
{
    if(document.getElementById(id).style.display == "none")
    {
        if (navigator.appName.indexOf("Microsoft Internet Explorer") >= 0)
            document.getElementById(id).style.display = "block";
        else
            document.getElementById(id).style.display = "table-row";
    }
    else
    {
        document.getElementById(id).style.display = "none";
    }

    var tmpId = id.replace("trdetails","")

    tmpId = "imgexcol" + tmpId;

    if(document.getElementById(tmpId).src.indexOf("i-expand.png") >= 0)
    {
        /*document.getElementById(tmpId).src = siteUrl+"admin/images/i-collapse.gif";*/
		document.getElementById(tmpId).src = "images/i-collapse.png";
        document.getElementById(tmpId).title = "Collapse";
        document.getElementById(tmpId).alt = "Collapse";
    }
    else
    {
        /*document.getElementById(tmpId).src = siteUrl+"admin/images/i-expand.gif";*/
		document.getElementById(tmpId).src = "images/i-expand.png";
        document.getElementById(tmpId).title = "Expand";
        document.getElementById(tmpId).alt = "Expand";
    }
}

// End - Click then Open Detail in List
function sel_chk()
{
	var val=document.getElementsByName('chk[]');
	if(document.getElementById('all').checked==true)
	{
		for(i=0;i<val.length;i++)
		{
			val[i].checked=true;
		}
	}
	else
	{
		for(i=0;i<val.length;i++)
		{
			val[i].checked=false;
		}

	}
}


function sel_chk_All(id)
{
	var chkelements = document.getElementsByName(id);
	
	if(document.getElementById('all').checked==true)
	{
		for(i=0;i<chkelements.length;i++)
		{
			chkelements[i].checked=true;
		}
	}
	else
	{
		for(i=0;i<chkelements.length;i++)
		{
			chkelements[i].checked=false;
		}

	}
}

function submit_frm(f,a,p,v)
{	
	//alert(f);

	if(a == 's')
	{
		f.sb.value = v;	
		if(f.st.value=='')		
			f.st.value = 0;
		else if(f.st.value=='0')		
			f.st.value = 1;
		else if(f.st.value=='1')		
			f.st.value = 0;		
	}
	
	if(a=='p')
	{
		//alert(v);
		f.page.value = v;
	}
	
	if(a=='d')
	{		
		if(!delconfirm(f)) return false;
		f.a.value = 'delete';		
	}
	
	if(a=='c')
	{		
		if(!selectcheck(f)) return false;
		f.isActive.value = v;
		f.a.value = 'isActive';
		
	}
	if(a=='rp')
	{
		//alert(1);
		f.pagesize.value = v;	
	}
	if(a == 'ia')
	{
		    f.isApproveId.value = v;
			f.a.value = 'isApprove';
	}
	
	if(a == 'exp') // export to excel
	{
		  f.a.value = 'ExportToExcel';
	}
	
	if(a == 'delimg')  
	{
		//alert(a);
		alert(f.delimg.value);
		f.delimg.value = 'DeleteImage';
	}
	
	f.action = p;	
	f.submit();
}

//function for the page change on enter keypress event
function pageChange(e,fname,p,pagename,tvalue)
{   	
	if(e.keyCode == 13)	
	{
		submit_frm(fname,p,pagename,tvalue);			
		return false;
	}	
}

function pageChangefront(e,fname,p,pagename,tvalue)
{   	
		document.getElementById('testp').value = tvalue
		submit_frm(fname,p,pagename,tvalue);			
		return false;
}

function checkNumber(val) {	
  val=document.getElementById(val); 
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  /* Check if the keyed in character is a number
     do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective
     codes and replace the 48 and 57 */
	//alert(strLength);
	
	if(strLength > 1)
	{	
	  if (cCode < 48 || cCode > 57 ) {	  
		var myNumber = val.value.substring(0, (strLength) - 1);
		val.value = myNumber;
	  }
	}
	else
	{		
	  if (cCode < 49 || cCode > 57 ) {	  
		var myNumber = val.value.substring(0, (strLength) - 1);
		val.value = myNumber;
	  }			
	}
  return false;
}

// calculate the ASCII code of the given character
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function selectcheck(frmObj)
{
	var flag = 0;
	var strid = "0";
	
	for(i = 0; i < frmObj.elements.length; i++) 
	{		
		elm = frmObj.elements[i]
		if (elm.type == "checkbox"  && !isNaN(elm.id)) 
		{			
			if (elm.checked)
			{				
				if (flag==0)
				{					
					flag = 1;					
				}			
			}
		}
	}
	if (flag == 1)
	{
		return true;
	}
	else
	{
		alert("- Select at least one record.");
		return false;
	}
}



function delconfirm(frmObj)
{	
	var flag = 0;
	var strid = "0";
	
	for(i = 0; i < frmObj.elements.length; i++) 
	{	
		elm = frmObj.elements[i];
		if (elm.type == "checkbox"  && !isNaN(elm.id)) 
		{	
			if (elm.checked)
			{
				if (flag==0)
				{
					flag = 1;
				}
			}
		}
	}
	if (flag == 1)
	{
		if(confirm('- Are you sure you want to delete?'))
		{
			return true;
		}
		else
			return false;
	}
	else
	{
		alert("- Select at least one record.");
		return false;
	}
}

function checkall(frmObj)
{
	var elm;
	for(i = 0; i < frmObj.elements.length; i++) 
	{	
		elm = frmObj.elements[i];
		
		if (elm.type == "checkbox" && !isNaN(elm.id)) 
		{
			elm.checked = true;
		}
	}
}

function clearall(frmObj)
{
	var elm;	
	for(i = 0; i < frmObj.elements.length; i++) 
	{	
		elm = frmObj.elements[i]
		if (elm.type == "checkbox" && !isNaN(elm.id)) 
		{			
			elm.checked = false;
		}
	}
}

/* New added due to SOP */
function showsop()
{
	document.getElementById('sop').style.display = "block";
	document.getElementById('helpsop').style.display = "none";
} 
function hidesop()
{
	document.getElementById('sop').style.display = "none";
	document.getElementById('helpsop').style.display = "block";
} 

function setrate(li,ra)
{
	document.latenighttxt_review.lateid.value = li;	
	document.latenighttxt_review.review.value = ra;
	document.latenighttxt_review.action = "setreviewlatenight.php";
	document.latenighttxt_review.submit();
}



/*********** Ajax Loader New *************/
function ProcessLoader()
{
	document.getElementById('dvprocessing').style.display = '';
}
// Created for quick edit of Displayorder - created by sanjay patel
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


function SendGridBindRequest(url,targetdiv,action,chkidfordelete)
{	
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp != null)
	{
            xmlHttp.onreadystatechange = function() { 
			if(xmlHttp.readyState==1)
			{
				// ProcessLoader();
			}
			
		  if((xmlHttp.readyState==4||xmlHttp.readyState=="complete")&&(xmlHttp.status==200))
			{   
			//alert('sadf');
			 	var str =  xmlHttp.responseText;
			   	///alert(str);
				
				document.getElementById(targetdiv).innerHTML = '';
			    document.getElementById(targetdiv).innerHTML = str;
				
				//document.getElementById('dimmer').style.width=0;
       			//document.getElementById('dimmer').style.height=0;
       			//document.getElementById('dimmer').style.visibility="";
        		//document.getElementById('dvprocessing').style.display = 'none';
				switch(action)
                {
                    case 'D':
                        alert("Records deleted successfully.");
                        break;
					case 'DELETE':
                        alert("Records deleted successfully.");
                        break;
		        }
	      }
		};
     
	 	var appendurl = '';
	    switch(action)
        {
			case 'DELETE': // this is delete wow product and image gallery individual image.
						if(!confirm('- Are you sure you want to delete?'))
						{	return false; 	}
					appendurl = '&QT=3';
			break;
			
			case 'D':
                var chkelements = document.getElementsByName(chkidfordelete);
				//alert(chkidfordelete);
			    var ids = "";
                var countChecked = 0;
                for(var i = 0; i < chkelements.length; i++)
                {
                    if(chkelements.item(i).checked == true)
                    {
                        countChecked++;
                        if(ids != "")
                           ids += ",";
                        ids += chkelements.item(i).value;
                    }
                }
				 
                if(countChecked == 0)
                {
                    alert(" Please select atleast one record for delete.");
                    return false;
                }
				
                if(!confirm('This will delete the selected records.Are you sure you want to continue?'))
                {
                    return false;
                }
				
               
				var totalpagerecords = document.getElementById('ptrecords').value;
			    appendurl = '&QT=4&ptotalr='+totalpagerecords+'&dids='+ids;
				//alert(appendurl);
				
				break;
			
			case 'P': 
				  appendurl = '';
			break;
			case 'LOGIN': 
				  var UserName = document.getElementById('UserName').value;
				  var Password = document.getElementById('Password').value;
				  	if(UserName == '')
					{
						alert('Please Enter the User Name');
						document.getElementById('UserName').focus();
						return false;
					}
					else if(UserName != '' && validemail(UserName) == 1)
					{
						alert('Please Enter the valid Email Address');
						document.getElementById('UserName').focus();
						return false;
					}
					else if(Password == '')
					{
						alert('Please Enter the Password');
						document.getElementById('Password').focus();
						return false;
					}
					
				  appendurl = '&UserName='+UserName+'&Password='+Password;
			break;
			
			case 'CUST_CONFIRM': 
				  if(!confirm('- Are you sure you want to Confirm the Appointment ?'))
						{	return false; 	}
				  appendurl = '';
			break;
			
				
	    }		
			
			var url = url+'&ajax=Y'+appendurl;
			//alert(url);
			
			xmlHttp.open('GET', url, true);
			xmlHttp.send(null);
	}
	else
	{
		alert("Your browser does not support HTTP Request");
	}
    return true;
}



function validemail(value)
{
	var valEmail =  trimAll(value);
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//var reg = /^[a-z][a-z_0-9\-\.]+@[a-z_0-9\-\.]+\.[a-z]{2,4}$/i
	if(!reg.test(valEmail))
	{	
		return 1;
	}
			
}	


