//Function to create Ajax Object-----------------------------------------------------------------------------------
function createajax()
{
   var objectname = false;	
	try { /*Internet Explorer Browsers*/ objectname = new ActiveXObject('Msxml2.XMLHTTP'); } 
	catch (e)
	{
		try { objectname = new ActiveXObject('Microsoft.XMLHTTP'); } 
		catch (e)  
		{
			try { /*// Opera 8.0+, Firefox, Safari*/ objectname = new XMLHttpRequest();	} 
			catch (e) { /*Something went wrong*/ alert('Your browser is not responding for Javascripts.'); return false; }
		}
	}
	return objectname;
}
//Function to display the error message in box---------------------------------------------------------------------
function errormessage(message)
{
	var msg = '<div class="errorbox">' + message + '</div>';
	return msg;
}

//Function to display the success message in box-------------------------------------------------------------------
function successmessage(message)
{
	var msg = '<div class="successbox">' + message + '</div>';
	return msg;
}

//Function to Validate the email ID---------------------------------------------------------------------------------
function checkemail(a)
{
	var r1 = new RegExp('(@.*@)|(\\.\\.)|(@\\.)|(^\\.)');
	var r2 = new RegExp('^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$');
	return (!r1.test(a) && r2.test(a));
}

//Function to make the display as block as well as none-------------------------------------------------------------
function showhide(elementid,imgname)
{
	var element = document.getElementById(elementid);
	if(element.style.visibility == 'collapse')
	{
		element.style.visibility = 'visible';
		if(document.getElementById(imgname))
			document.getElementById(imgname).src = "../images/minus.jpg";
	}
	else
	{
		element.style.visibility = 'collapse';
		if(document.getElementById(imgname))
			document.getElementById(imgname).src = "../images/plus.jpg";
	}
}
//Function to make the display as block as well as none-------------------------------------------------------------
function displayelement(displayelementid,hideelementid)
{
	var delement = document.getElementById(displayelementid);
	var helement = document.getElementById(hideelementid);
	delement.style.display = 'block'; helement.style.display = 'none'; 
}

//Function to enable the delete button------------------------------------------------------------------------------
function enabledelete()
{
	document.getElementById('delete').disabled = false;
	document.getElementById('delete').className = 'swiftchoicebutton';
}

//Function to enable the save button--------------------------------------------------------------------------------
function enablesave()
{
	document.getElementById('save').disabled = false;
	document.getElementById('save').className = 'swiftchoicebutton';
}

//Function to disable the save button--------------------------------------------------------------------------------
function disablesave(){
	document.getElementById('save').disabled = true;
	document.getElementById('save').className = 'swiftchoicebuttondisabled';
	document.getElementById('delete').style.cursor = '';
}

//Function to disable the delete button-----------------------------------------------------------------------------
function disabledelete()
{
	document.getElementById('delete').disabled = true;
	document.getElementById('delete').className = 'swiftchoicebuttondisabled';
	document.getElementById('delete').style.cursor = '';
}

//Function to change the css of active tab and select the tab in display grid part----------------------------------
function gridtab3(activetab,tabgroupname,tabdescription)
{
	var totaltabs = 3;
	var activetabheadclass = 'grid-active-tabclass';
	var tabheadclass = 'grid-tabclass';
	
	for(var i=1; i<=totaltabs; i++)
	{
		var tabhead = tabgroupname + 'h' + i;
		var tabcontent = tabgroupname + 'c' + i;
		if(i == activetab)
		{
			document.getElementById(tabhead).className = activetabheadclass;
			document.getElementById(tabcontent).style.display = 'block';
			document.getElementById('tabdescription').innerHTML = tabdescription;
		}
		else
		{
			document.getElementById(tabhead).className = tabheadclass;
			document.getElementById(tabcontent).style.display = 'none';
			//document.getElementById('tabdescription').innerHTML = '';
		}
	}
}

//Function to change the css of active tab and select the tab in display grid part----------------------------------
function gridtab6(activetab,tabgroupname,tabdescription)
{
	var totaltabs = 6;
	var activetabheadclass = 'grid-active-tabclasst6';
	var tabheadclass = 'grid-tabclasst6';
	
	for(var i=1; i<=totaltabs; i++)
	{
		var tabhead = tabgroupname + 'h' + i;
		var tabcontent = tabgroupname + 'c' + i;
		if(i == activetab)
		{
			document.getElementById(tabhead).className = activetabheadclass;
			document.getElementById(tabcontent).style.display = 'block';
			document.getElementById('tabdescription').innerHTML = tabdescription;
		}
		else
		{
			document.getElementById(tabhead).className = tabheadclass;
			document.getElementById(tabcontent).style.display = 'none';
		}
	}
}

//Function to change the css of active tab and select the tab in display grid part----------------------------------
function gridtab4(activetab,tabgroupname,tabdescription)
{
	var totaltabs = 4;
	var activetabheadclass = 'grid-active-tabclasst6';
	var tabheadclass = 'grid-tabclasst6';
	
	for(var i=1; i<=totaltabs; i++)
	{
		var tabhead = tabgroupname + 'h' + i;
		var tabcontent = tabgroupname + 'c' + i;
		if(i == activetab)
		{
			document.getElementById(tabhead).className = activetabheadclass;
			document.getElementById(tabcontent).style.display = 'block';
			document.getElementById('tabdescription').innerHTML = tabdescription;
		}
		else
		{
			document.getElementById(tabhead).className = tabheadclass;
			document.getElementById(tabcontent).style.display = 'none';
		}
	}
}

//Function to change the css of active tab and select the tab in display grid part----------------------------------
function gridtab2(activetab,tabgroupname,tabdescription)
{
	var totaltabs = 2;
	var activetabheadclass = 'grid-active-tabclass';
	var tabheadclass = 'grid-tabclass';
	
	for(var i=1; i<=totaltabs; i++)
	{
		var tabhead = tabgroupname + 'h' + i;
		var tabcontent = tabgroupname + 'c' + i;
		if(i == activetab)
		{
			document.getElementById(tabhead).className = activetabheadclass;
			document.getElementById(tabcontent).style.display = 'block';
			document.getElementById('tabdescription').innerHTML = tabdescription;
		}
		else
		{
			document.getElementById(tabhead).className = tabheadclass;
			document.getElementById(tabcontent).style.display = 'none';
		}
	}
}

//Function to get the value of selected radio element---------------------------------------------------------------
function getradiovalue(radioname)
{
	if(radioname.value)
		return radioname.value;
	else
	{
		for(var i = 0; i < radioname.length; i++) 
		{
			if(radioname[i].checked) 
				return radioname[i].value;
		}
	}
}

//Function to generate random password------------------------------------------------------------------------------
function randomPassword()
{
	chars0 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; chars1 = "1234567890";
	pass = ""; pass1 = ""; pass2 = "";
	for(x=0;x<4;x++) { i = Math.floor(Math.random() * 62); pass1 += chars0.charAt(i); }
	for(y=0;y<4;y++) { j = Math.floor(Math.random() * 62); pass2 += chars1.charAt(j); }
	pass = pass1 + pass2;
	return pass;
}

//Function to get the district code according to the state selected---------------------------------------------
function districtcodeFunction(selectid, comparevalue, selectstatus)
{
	var statecode = document.getElementById('state').value;
	var districtDisplay = document.getElementById('districtcodedisplay');
	passData = "statecode=" + statecode  + "&dummy=" + Math.floor(Math.random()*1100011000000);
	ajaxcalld = createajax();
	var queryString = "./ajax/selectdistrictonstate.php";
	ajaxcalld.open("POST", queryString, true);
	ajaxcalld.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxcalld.onreadystatechange = function()
	{
		if(ajaxcalld.readyState == 4)
		{
			districtDisplay.innerHTML = ajaxcalld.responseText;
			if(selectid && comparevalue)
			{
				autoselect(selectid, comparevalue);
				if(selectstatus == 'disable')
					document.getElementById(selectid).disabled = true;
			}
		}
	}
	ajaxcalld.send(passData);return true;
}

//Function to check the particular option in <input type =check> Tag, with the compare value------------------------
function autocheck(selectid,comparevalue)
{
	var selection = selectid;
		if('yes' == comparevalue)
		{
			selection.checked = true;
			return;
		}
		else
		{
			selection.checked = false;
			return;
		}
}

//Function to select the particular option in <SELECT> Tag, with the compare value----------------------------------
function autoselect(selectid,comparevalue)
{
	var selection = document.getElementById(selectid);
	for(var i = 0; i < selection.length; i++) 
	{
		if(selection[i].value == comparevalue)
		{
			selection[i].selected = "1";
			return;
		}
	}
}


//Function to call the function on load of the page if function exists---------------------------------------------
function bodyonload()
{	if(typeof enabledisableoldform == 'function') { enabledisableoldform(); }

}

//Function to select all the check boxes in a group-----------------------------------------------------------------
function checkAll(checkallboxname, checkboxname)
{
	var field = document.getElementsByTagName('input');
	var selection = (document.getElementById(checkallboxname).checked == true)?true:false;
	for(i=0; i<field.length; i++)
	{
		if(field[i].type == 'checkbox' && field[i].name == checkboxname)
		field[i].checked = selection;
	}
}

//Function to the value of the check box which is in group----------------------------------------------------------
function get_check_value(checkboxname)
{
	var c_value = "";
	var checkbox = document.getElementsByName(checkboxname);
	for (var i=0; i < checkbox.length; i++)
	{
		if (checkbox[i].checked)
		{
			c_value = c_value + checkbox[i].value + "^^^";
		}
	}
return c_value;
}

function getprocessingimage()
{
	var imagehtml = '<img src="images/imax-loading-image.gif" border="0"/>';
	return imagehtml;
}

function onblurvalue()
{
	var dtStr =  document.getElementById('customerid').value;
	var val=dtStr.replace(/-/g,"");
	document.getElementById('customerid').value = val;
}

function validatecustomerid(cusid)
{
	
	var numericExpresion1 = /^\d{17}$/;
	var numericExpresion = /^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{5}$/;
	var numericExpresion2 = /^[0-9]{4}(\s)[0-9]{4}(\s)[0-9]{4}(\s)[0-9]{5}$/;
	if(cusid.match(numericExpresion)) return true;
	else if(cusid.match(numericExpresion1)) return true;
	else if(cusid.match(numericExpresion2)) return true;
	else return false;
}
function validatepincode(pincodenumber)
{
	var numericExpression = /^[^0]+[0-9]{5}$/i;
	if(pincodenumber.match(numericExpression)) return true;
	else return false;
}

function validatecell(cellnumber)
{
	var numericExpression = /^[7|8|9]\d{9}(?:(?:([,][\s]|[;][\s]|[,;])[7|8|9]\d{9}))*$/i;
	//var numericExpression = /^[7|8|9]+[0-9]{9,9}(?:(?:[,;][7|8|9]+[0-9]{9,9}))*$/i;
	if(cellnumber.match(numericExpression)) return true;
	else return false;
}

function validatephone(phonenumber)
{
	var numericExpression = /^[^9]\d{5,7}(?:(?:([,][\s]|[;][\s]|[,;])[^9]\d{5,7}))*$/i;
	if(phonenumber.match(numericExpression)) return true;
	else return false;
}


function emailvalidation(emailid1)
{
	var emailExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(emailid1.match(emailExp)) return true; 
	else return false; 
}

function pinnovalidation(pinno)
{
	var numericExpresion = /^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$/;
	if(pinno.match(numericExpresion)) return true;
	return false;
}

function computeridvalidate(compid)
{
	var numericExpresion = /^[0-9]{3}0[0|9]-[0-9]{9}$/;
	if(compid.match(numericExpresion)) return true;
	return false;
}

function getradiovalue(radioname)
{
	if(radioname.value)
		return radioname.value;
	else
		for(var i = 0; i < radioname.length; i++) 
		{
			if(radioname[i].checked) {
				return radioname[i].value;
			}
		}
}


function validatecontactperson(contactname)
{
	var numericExpression = /^([A-Z\s\()]+[a-zA-Z\s()])(?:(?:[,;]([A-Z\s()]+[a-zA-Z\s()])))*$/i;
	if(contactname.match(numericExpression)) return true;
	else return false;
}

function validatebusinessname(contactname)
{
	var numericExpression = /^([A-Z0-9\s\-()]+[a-zA-Z0-9\s-()])(?:(?:[,;]([A-Z0-9\s-()]+[a-zA-Z0-9\s-()])))*$/i;
	if(contactname.match(numericExpression)) return true;
	else return false;
}

//Validation of website - Rashmi -18/11/2009
function validatewebsite(website)
{
	var websiteExpression = /^(www\.)?[a-zA-Z0-9-\.,]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov|co.in|in)$/i;
	if(website.match(websiteExpression)) return true;
	else return false;
}