// JavaScript Document
function poplinks(url)
{
	newwindow=window.open(url,'ImageDisplay','height=400,width=600,left=0,top=0,resizable=yes,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}

function changecity(x){
	window.location.href = "gasreport.php?state="+x;
}

function changestation(x,y){
	window.location.href = "gasreport.php?state="+x+"&cityid="+y;
}

function changeothergas(x){
		if (x == "Other"){
		ap1=document.getElementById("showattrib");
		    if(ap1){
			ap1.style.display="block";
			ap1.style.visibility="visible";
				}				
		}
		else
		{
			ap1=document.getElementById("showattrib");
			if(ap1){
			ap1.style.display="none";
			ap1.style.visibility="hidden";
				}
		}
}

function gas(){
	 if(document.frmgas.txtState.value == "")
	{
		alert("Choose the 'State'.");
		document.frmgas.txtState.focus();
		return false;
	}
	
	 if(document.frmgas.txtCity.value == "")
	{
		alert("Choose the 'City'.");
		document.frmgas.txtCity.focus();
		return false;
	}
	if(document.frmgas.txtStationname.value == "")
	{
		alert("Choose 'Gas Station name'");
		document.frmgas.txtStationname.focus();
		return false;
	}
	if(document.frmgas.txtStationname.value == "Other")
	{
		if(document.frmgas.txt_Stationname.value == "")
		{
			alert("Enter 'Gas Station name'");
			document.frmgas.txt_Stationname.focus();
			return false;
		}
	}
	if(document.frmgas.gastype.value == "")
	{
		alert("Select 'Gas Type'.");
		document.frmgas.gastype.focus();
		return false;
	}
	if(document.frmgas.txtPrice.value == "")
	{
		alert("Enter 'Price'.");
		document.frmgas.txtPrice.focus();
		return false;
	}
var txtPrice=document.frmgas.txtPrice.value;
if(isNaN(txtPrice) || txtPrice == 0 || txtPrice.indexOf('-') >= 0 ){
		alert("Price must be a numberic value.");
		document.frmgas.txtPrice.focus();
		return false;
	 }
   if(document.frmgas.txtAddress.value == "")
	{
		alert("Enter 'Address1'.");
		document.frmgas.txtAddress.focus();
		return false;
	}
}

//Email Validation Script
function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   if (db) alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("Email address contains non ascii characters.");
		  return false;
	   }
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
return true;
}


//Function phone validation Script
// Declaring required variables
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 = 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 confirmMsg(msg){
	if(!confirm(msg)){
		return false;

	} else {
		return true;

	}
}




