/*code for city - state selection and gameplex cafe interlinking*/
$(document).ready( function() {
                   //hide the 'other' city input box first
       $('#city').each( function() {
		var name = $(this).attr('name');

		if($(this).val()!='other'){
			$(this).next().removeAttr('name').hide();
		}
	});

	var jObj = eval('('+jstr+')');


	var state_sel = $('#state');

	for(i=0; i<jObj.list.length; i++){
		optn = document.createElement("OPTION");
		optn.text = jObj.list[i].state;
		optn.value = jObj.list[i].state;
		document.frmordercd.state.options.add(optn);
	}

        state_sel.change(function(){
           //hide the 'other' box for city if that is visible
		  if($('#city').val()=='other') {
				$('#city').next().removeAttr('name','city').fadeOut('fast');
		  }
		  var optstr='<option value="0">' + '-Select City-' + '</option>';
		  $.each(jObj.list, function(i, stateObj){
			   if(stateObj.state ==  state_sel.val()){
				$.each(stateObj.cities, function(key,val){
						optstr += '<option value="' + val.name + '">' + val.name + '</option>';
					}
				)
				optstr += '<option value="other">Other</option>';
				$('#city').html(optstr);
				return false;
			}
		   });
        });

	$('#city').change(function(){
		var desiredName = $(this).attr('name');
		var cityval = $('#city').val();

		//show a box if the city has a gameplex
		if(isGameplex(cityval)){
			$('#orderform').hide();

			$.getJSON('getgxdata.php?city='+cityval+'&jsoncallback=?',showGx);

			//$.get('getgxdata.php','city='+cityval,showGx)
		}else {
			$('#gameplexdetails').hide();
			$('#orderform').show();
			if(cityval=='other') {
				$('#city').next().attr('name',desiredName).fadeIn('fast');
			}
			else {
				$('#city').next().removeAttr('name').fadeOut('fast');
			}
			//show the form
		}
	});

         //repopulate for form reload
	if(orig_state!=''){
		  state_sel.val(orig_state);
		  state_sel.trigger('change');
		  $('#city').val(orig_city);
		  if($('#city').val() != orig_city) { //the only possibility is that 'other' was chosen so show the text box
			 $('#city').val('other');
			 $('#city').next().attr({name:'city', value: orig_city}).show();
		  }
	}


});
function isGameplex(city){
		var ispresent = false;
		for(i=0; i<gxcityArr.length; i++){//alert(typeof city +' == '+typeof gxcityArr[i]);
		//alert(city.toString() === gxcityArr[i].toString());
			if(city.toString() === gxcityArr[i].toString()){
				ispresent = true;
				break;
			}
		}
		return ispresent;
	}
function showGx(data){

	//var jCafeObj = eval('('+data+')');
	var htmlstr = '';
	htmlstr += '<table width="580"><tbody ><tr><td colspan="5" class="text">You could collect a free Crazy Kart game CD from  one of the Gameplex cafes in your city:</td></tr><tr bgcolor="#DB4800" class="headinglight" height="10"><td width="25%" bgcolor="#DB4800" class="grey" style="padding-left:5px">Name of Cafe </td><td width="40%" bgcolor="#DB4800" class="grey" style="padding-left:5px">Address</td><td width="15%" bgcolor="#DB4800" class="grey" style="padding-left:5px">Area</td><td width="10%" bgcolor="#DB4800" class="grey" style="padding-left:5px">Landmark</td><td width="10%" bgcolor="#DB4800" class="grey" style="padding-left:5px">Phone Number </td></tr>';
	for(i=0; i<data.cafes.length; i++){
		htmlstr += '<tr class="text" height="20" bgcolor=';
		if(i % 2==0){ htmlstr += '#A4A4A4>'; } else	{ htmlstr += '#BDBDBD>';}
		htmlstr += '<td style="padding-left:5px" >Zapak Gameplex</td><td style="padding-left:5px">'+data.cafes[i].address+'</td><td style="padding-left:5px" >'+data.cafes[i].area+'</td><td style="padding-left:5px">NA</td><td style="padding-left:5px" >NA</td></tr>';
	}
	//alert(htmlstr);
	htmlstr +=  "</tbody></table>";
	$('#gameplexdetails').html(htmlstr).show();
	/*$('#gameplexdetails').html(htmlstr);
		$('#gameplexdetails').show();*/
}
/*end of jquery based code for state -city and gameplex interlinking*/


var dtCh= "/";
var minYear=1920;
var maxYear=2005;

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 strMonth=dtStr.substring(0,pos1)
	var strDay=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(strDay=='DD' && strMonth=='MM' && strYear=='YYYY' )
	{
		return (0);
	}
	if (strMonth == 'MM' || strMonth.value<1 || month<1 || month>12)
	{
		return (1);
	}

	if(strDay =='DD' ||strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		return (2);
	}

	if (strYear =='YYYY' || strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		return (3);
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		return (4);
	}
}


function checkmail(mailObj)
{
	var emailfilter ;
	emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i ;
	var returnval=emailfilter.test(mailObj.value);
	if (returnval==false)
	{
		mailObj.select();
		return false;
	}
	return true;
}

function yearloop()
{
	document.write("<option value='0'>YYYY</option>")
	for(yearcnt=2005;yearcnt>=1920;yearcnt--)
		document.write("<option value=" + yearcnt + ">" + yearcnt + "</option>")
}
function validateform()
{
	var valid=0;
	if(document.frmordercd.fname.value=='')
	{
		valid++;
		document.getElementById("err0").innerHTML=ximage;
		document.getElementById("err0_msg").innerHTML="Please enter your name";
	}
	
	if(document.frmordercd.email.value=='')
	{
		valid++;
		document.getElementById("err2").innerHTML=ximage;
		document.getElementById("err2_msg").innerHTML="Please enter your Email ID";
	}
	else
	{

		if(!(checkmail(document.frmordercd.email)))
		{
			valid++;
			document.getElementById("err2").innerHTML=ximage;
			document.getElementById("err2_msg").innerHTML="Please enter a valid Email ID";
			document.frmordercd.email.focus();
		}
	}
	
	
	if(document.frmordercd.address.value=='')
	{
		valid++;
		document.getElementById("err5").innerHTML=ximage;
		document.getElementById("err5_msg").innerHTML="Please enter atleast one line of Address";
	}
	
	if(document.frmordercd.pincode.value=='')
	{
		valid++;
		document.getElementById("err5").innerHTML=ximage;
		
		
		document.getElementById("err9_msg").innerHTML="Please enter your PIN Code";

		
	}
	else
	{
		
		if(document.frmordercd.pincode.value.length!=6 || !(isInteger(document.frmordercd.pincode.value)))
		{
			valid++;
			document.getElementById("err9").innerHTML=ximage;
			document.getElementById("err9_msg").innerHTML="Please enter a valid PIN Code";
		}
	}
	if(document.frmordercd.city.value==0)
	{
		valid++;
		document.getElementById("err10").innerHTML=ximage;
		document.getElementById("err10_msg").innerHTML="Please select your City";
	}
	
	/*else if ( $('#city').val()=='other' &&( $('#city').next().val()=='' || $('#city').next().val()=='Enter City')) // the conditiions $('#city').next().val() make use of jquery to select the value of the 'other' textbox value. 
	{
		valid++;
		document.getElementById("err10").innerHTML=ximage;
		document.getElementById("err10_msg").innerHTML="Please enter your City in the box provided";
	}*/

	if(document.frmordercd.state.value==0)
	{	
		valid++;
		document.getElementById("err13").innerHTML=ximage;
		document.getElementById("err13_msg").innerHTML="Please select your State";
	}
	if(document.frmordercd.mobile.value=='')
	{
		valid++;
		document.getElementById("err11").innerHTML=ximage;
		
		document.getElementById("err11_msg").innerHTML="Please enter your Contact Number";
	}
	
	
	

	if(valid > 0)
	{

			return false;
	}
	else
	{
		//document.frmordercd.action='/xml/ckordercdsubmit.zpk';
		return true;
	}
}




function remove_err(ob)
{
	if(ob.name=="fname" && ob.value!="")
	{
		document.getElementById("err0").innerHTML='';
		document.getElementById("err0_msg").innerHTML='';
	}
	
	if(ob.name=="email"  && ob.value!="")
	{
		if(!(checkmail(document.frmordercd.email)))
		{
			document.getElementById("err2").innerHTML=imgpath;
			document.getElementById("err2_msg").innerHTML="Please enter a valid Email ID";
		}
		else
		{
			document.getElementById("err2").innerHTML='';
			document.getElementById("err2_msg").innerHTML='';
		}
	}
	
	if(ob.name=="address" && ob.value!="")
	{
		document.getElementById("err5").innerHTML='';
		document.getElementById("err5_msg").innerHTML='';
	}
	/*if(ob.name=="landmark" && ob.value!="")
	{
		document.getElementById("err8").innerHTML='';
		document.getElementById("err8_msg").innerHTML='';
	}*/
	if(ob.name=="pincode" && ob.value!="")
	{
		document.getElementById("err9").innerHTML='';
		document.getElementById("err9_msg").innerHTML='';
		if(document.frmordercd.pincode.value.length!=6 || !(isInteger(document.frmordercd.pincode.value)))
		{
			document.getElementById("err9").innerHTML=imgpath;
			document.getElementById("err9_msg").innerHTML="Please enter a valid PIN Code";
		}
	}
	if(ob.name=="city")
	{
		document.getElementById("err10").innerHTML='';
		document.getElementById("err10_msg").innerHTML='';
	}
	if(ob.name=="state")
	{
		document.getElementById("err13").innerHTML='';
		document.getElementById("err13_msg").innerHTML='';
		document.getElementById("err10").innerHTML='';
		document.getElementById("err10_msg").innerHTML='';
	}
	if(ob.name=="mobile" && ob.value!="")
	{
		document.getElementById("err11").innerHTML='';
		document.getElementById("err11_msg").innerHTML='';
		
	}
}

	function deleteOptions(obj2)
	{
		while (obj2.options.length>1)
		{
			deleteIndex=obj2.options.length-1;
			if (obj2.options[deleteIndex].value != "11")
			{
				obj2.options[deleteIndex]=null;
			}
		}
	}
	 function stChange(theForm)
	 {		 
		 
		  
		deleteOptions(theForm.city);
		
		 
		 var optn = document.createElement("OPTION");
		 optn.text = "-Select City-";
		 optn.value = "-Select City-";

	 for(i=0;i<ckCityCount[theForm.state.selectedIndex-1];i++)
		 {
			var optn = document.createElement("OPTION");
			optn.text = ckCity[theForm.state.selectedIndex-1][i];
			optn.value = ckCity[theForm.state.selectedIndex-1][i];

			theForm.city.options.add(optn);
			
		 }
	 }
            
			function addoptions()
		  {  
			
				for(i=0;i<ckState.length;i++)      
				{
					var optn = document.createElement("OPTION");
					optn.text = ckState[i];
					optn.value = ckState[i];

					frmordercd.state.options.add(optn);
            }                  
        }


