//function to validate Sign Up form
function validate()
{
	var error_message = document.getElementById('error_message');

	var first_name, email;
	var zip, phone;

    first_name = document.getElementById('first_name');
    email = document.getElementById('email');

    zip = document.getElementById('zip');
    phone = document.getElementById('phone');

    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;


    if (trim(first_name.value) == '')   {//validate input first name is null or not
      error_message.innerHTML = 'Enter first name..';
      first_name.focus ();
      return false;
    }

    if (trim(email.value) == '')   {//validate input email is null or not
      error_message.innerHTML = 'Enter email address.';
      email.focus ();
      return false;
    }
    else if (!email.value.match(emailRegEx)) {	//validate input email is in valid format or not
      error_message.innerHTML = 'Enter valid email address.';
      email.focus ();
      return false;
    }

    if (trim(zip.value) != ''){
        if(zip.value.length != 5){
            error_message.innerHTML = 'Zip code should be 5 digit.';
            zip.focus ();
            return false;
        }
    }

    if (trim(phone.value) != ''){ 
        if(phone.value.length != 12){
            error_message.innerHTML = 'Phone number should be 10 digit.';
            phone.focus ();
            return false;
        }
    }

    else {
      return true;
    }
}

//function to trim input values
function trim(str) {
    return str.replace(/^\s+|\s+$/g,'');
}

//function to validate input for integer
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function searchPress(event, what, numDecimals) {
		var strValue; var el;
        if(what.id == null) return;

		var isIE = document.all ? true : false;
		var isNS = (navigator.appName.indexOf('Netscape') > -1) ? true : false;

		if(isIE){
			strValue = event.keyCode;
			el = event.srcElement;
		}
		if(isNS){
			strValue = event.which;
			el = event.target
		}
        var key = String.fromCharCode(strValue);
		if (strValue == 8 | strValue == 0) {
			return true;
        }
		var dot = false;
		var str = what.value;

		//Decimal check
		var dpos = str.lastIndexOf(".")
		if (dpos > 0) {
			var decimalnumbers = str.substring(dpos,str.length)
			if (decimalnumbers.length > numDecimals) {
				return false
			}
		}
		if(key == "." && numDecimals < 1) return false;

		if (str.indexOf(".") >= 0) {
			dot = true;
		}
		if (((key < '0' || key > '9') && key != ',' && (key != '.')) || (key == '.' && dot)) {
			return false;
		}else{
			if(key == "." && el.value.indexOf('.') >= 0){
					return false
			}
			return true
		}
		return true;
}

function checkPhone(cntrl)
{
    if(cntrl.value.length == 3)//to add colon after entering hours
    {
        cntrl.value = cntrl.value + '-'
    }
	if(cntrl.value.length == 7)//to add colon after entering hours
    {
        cntrl.value = cntrl.value + '-'
    }
}

//fectch city as per state change
function showCities(statename,cityname )
{
    var selStates = document.getElementById(statename);
    document.getElementById(cityname).innerHTML = '';
    if(selStates.value!="")
    {
        $.ajax({
         type: "GET",
         url: "includes/ajax.getCities.php",
         data: "statename="+selStates.value,
         success: function(msg){
           if(msg!='')
            {
                var arrCities = msg.split("|#|");
                var lenCities = arrCities.length;

                var firstOption = document.createElement("option");
                firstOption.value=  '';
                firstOption.setAttribute("value",'');
                firstOption.innerHTML = "...";
                document.getElementById(cityname).appendChild(firstOption);

                for(var cntCities = 0; cntCities<lenCities-1;cntCities++)
                {
                    var arrEachCity = arrCities[cntCities].split("|@|")
                    var newOption = document.createElement("option");
                     newOption.value=  arrEachCity[0];
                     newOption.setAttribute("value",arrEachCity[0]);
                     newOption.innerHTML = arrEachCity[1];
                     document.getElementById(cityname).appendChild(newOption);
                }


            }
         }
       });
    }
}

function changestatecityvalues(newval, changeval)
{
    document.getElementById(changeval).value =  document.getElementById(newval).value ;
}
