// JavaScript Document
var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;
	var minDigitsInIDNo = 9;
	var minDigitsInPhone = 6;

	function isInteger(s)
	{   
		var i;
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
	

	function stripCharsInBag(s, bag)
	{  
		 var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}

	function checkInternationalPhone(strPhone)
	{
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
	function checkIDNo(id_no)
	{
		s=stripCharsInBag(id_no,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIDNo);
	}
	function checkPhone(phone)
	{
		s=stripCharsInBag(phone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInPhone);
	}
	
	/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	
	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	
	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   } 
	   return this
	}
	
	function isDate(dtStr)
	{
		var daysInMonth = DaysArray(12)
		var pos1=dtStr.indexOf(dtCh)
		var pos2=dtStr.indexOf(dtCh,pos1+1)
		var strDay=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)
		strYr=strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if (pos1==-1 || pos2==-1){
			//alert("The date format should be : dd/mm/yyyy")
			return false
		}
		if (strMonth.length<1 || month<1 || month>12){
			//alert("Please enter a valid month")
			return false
		}
		if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			//alert("Please enter a valid day")
			return false
		}
		if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
			//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
			return false
		}
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			//alert("Please enter a valid date")
			return false
		}
		return true
	}
	
function CheckEmail(obj)
{
	var re = /^([A-Za-z0-9\_\-]+\.)*[A-Za-z0-9\_\-]+@[A-Za-z0-9\_\-]+(\.[A-Za-z0-9\_\-]+)+$/;
	var sEMail = obj.value;
	if (sEMail.search(re) == -1)
	{
			return (false);
	}
	else
	{
		return (true);
	}
}
function toggle_panel(name, total_rows, status)
{
	for(i=1;i<=total_rows;i++)
	{
		element = name + "_" + i;
		var e = document.getElementById(element);
		if(status == 0)
			e.style.display = 'none';
		else
			e.style.display = '';
	}
}
function toggle_panel_image(name, total_rows, status)
{
	if(status == 0)
		setCookie("tmp_" + name,"close",365);
	else
		setCookie("tmp_" + name,"open",365);
		
	if(status == 0)
	{
		if(getCookie(name) == "open")
		{
			return;	
		}
	}
	
	for(i=1;i<=total_rows;i++)
	{
		element = name + "_" + i;
		var e = document.getElementById(element);
		if(status == 0)
			e.style.display = 'none';
		else
			e.style.display = '';
	}
	
	img_name = name + "_img";
	var img = document.getElementById(img_name);
	
	tmp_cookie = getCookie("tmp_" + name);
	
	if(tmp_cookie == "")
	{
		cookie = getCookie(name);
		if(cookie == "open")
		{
			img.src = "images/btnToggleDown.gif";
		}
		else
		{
			img.src = "images/btnToggleUp.gif";
		}
	}
	else
	{
		if(tmp_cookie == "open")
		{
			img.src = "images/btnToggleDown.gif";
		}
		else
		{
			img.src = "images/btnToggleUp.gif";
		}
	}	
}
function toggle(name, total_rows)
{				
	for(i=1;i<=total_rows;i++)
	{
		element = name + "_" + i;
		toggle_visibility_status(element,getCookie(name));
	}
}
function toggle_cookie(name, total_rows)
{
	setCookie("tmp_" + name,"",365);
	
	img_name = name + "_img";
	var img = document.getElementById(img_name);
	
	if(getCookie(name) == "open")
	{
		setCookie(name,"close",365);
		setCookie("tmp_" + name,"close",365);
		img.src = "images/btnToggleUp.gif";
	}
	else
	{
		setCookie(name,"open",365);
		setCookie("tmp_" + name,"open",365);
		img.src = "images/btnToggleDown.gif";
	}
	
	//document.getElementById("txt_tmp").innerHTML += name + ": " + getCookie(name) + ": " + img.src + "\r\n";
					
	for(i=1;i<=total_rows;i++)
	{
		element = name + "_" + i;
		toggle_visibility_status(element,getCookie(name));
	}
}
function toggle_visibility(id) 
{
	var e = document.getElementById(id);
	if(e.style.display == '')
		e.style.display = 'none';
	else
		e.style.display = '';
}
function toggle_visibility_status(id,status) 
{
	var e = document.getElementById(id);
	if(status == "open")
	{
		e.style.display = '';
	}
	else
	{
		e.style.display = 'none';	
	}
}
function visibility_status(id,status) 
{
	var e = document.getElementById(id);
	e.style.visibility = status;
}
function Download(file) 
{
	document.getElementById("export").src=file;
}
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		response = xmlHttp.responseXML.documentElement;
		method = response.getElementsByTagName('method')[0].firstChild.data;
		result = response.getElementsByTagName('result')[0].firstChild.data;
		document.getElementById("tbl_dealer").innerHTML = result;
	} 
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}
