var clickcounter;
//localStorage.bgpfclickcounter = 0;

var recordarray = new Array();
var localrecord = new Array();
var phonenumber;
var fname;
var lname;
var bloodgroup;
var rhfactor;

//******************************

var nutshell = ["<li>Cannot stand people who hide truth.</li>",
"<li>Pessimistic and too sensitive.</li>",
"<li>Cannot take orders easily.</li>",
"<li>Romantic and sentimental.</li>"
];

var basicbehavior = ["<li>Make object clear.</li><li>Possess great deal of confidence.</li><li>Honest, optimistic and energetic.</li>",
"<li>Careful about decision-making.</li><li>Make things clear in black and white.</li><li>Care to much about social rules and standard.</li>",
"<li>Make decision fast.</li><li>Can be flexible.</li><li>Do not care about rules.</li><li>Respect scientific and practical things.</li>",
"<li>Extremely practical.</li><li>Excellent in analyses.</li><li>Give fair criticisms.</li><li>Cannot decide when it comes to important issues.</li>"
];
var tolerance = ["<li>Strength and endurance depend on their aim.</li><li>Give up easily if they find job meaningless.</li>",
"<li>High tolerance for physical or repitive work.</li><li>Cannot take changes easily.</li><li>Lost interest in a hobby easily.</li>",
"<li>Maintain the longest interest in what they do.</li><li>Seem impatient.</li><li>Dislike repetitious work</li>",
"<li>Try to be hard working.</li><li>Tend to be impatient.</li>"
];

var futurepast = ["<li>Positive about past, thus do not regret about the past.</li><li>Seek financial stability for the future.</li>",
"<li>Try hard to forget past.</li><li>Pessimistic about future.</li>",
"<li>Hard to forget recent affairs, but able to forget past and memories.</li>",
"<li>Sentimental about the past.</li><li>More concern about the immediate problems than anything else.</li>"
];

var emotions = ["<li>Usually stable and calm.</li><li>Sensitive towards sincerity.</li><li>Give frank, direct opinions.</li>",
"<li>Able to display cool outlook even though angry.</li><li>Short-tempered.</li><li>Take longer to heal a broken heart.</li><li>Sensitive to others opinion.</li>",
"<li>Expressive.</li><li>Cool and objective.</li><li>Although joke a lot, could actually be a very shy.</li><li>Change moods like the weather.</li><li>Cannot stop complaining when they are upset.</li>",
"<li>Sentimental.</li><li>Usually cool and  steady, but can get upset with an immediate, unsolved problem.</li><li>Can get mood easily.</li>"
];

var work = ["<li>Ability to concentrate vary from time to time, depending on aim.</li><li>Mostly prefer to lead.</li><li>Can overlook details.</li><li>Common career choices: banker, politician, gambler, minister, investment broker, and pro athlete.</li>",
"<li>Perfectionist.</li><li>Handle on thing at a time.</li><li>Work a line between work and personal affairs.</li><li>Highly responsible.</li><li>Tend to choose hobbies which help them release stress.</li><li>Common career choices: accountant, librarian, economist, writer, computer programmer, and gossip columnist.</li>",
"<li>Creative and possess a new idea.</li><li>Cannot differentiate between work and hobby.</li><li>Cannot take orders.</li><li>Do not hesitate to introduce innovatives changes and are not worried about their criticisms.</li><li>Common career choices: cook, hairdresser, military leader, talk show host, and journalist.</li>",
"<li>Able to handle a wide scope of jobs.</li><li>Value hard work.</li><li>Quick in understanding.</li><li>Not highly responsible and unable to follow-up on a project until its completion.</li><li>Tend to be artistic in  approach.</li><li>Common career choices: bartender, lawyer, teacher, sales representative, and social worker.</li>"
];

//*********************************
$(document).ready(function() {
	$("#vserv_advt_slide, #vserv_advt_slide_popup").click(function(event) {
		localStorage.bgpfclickcounter = 0;
	});
});


$(document).ready(function() {
	$( '#button_0' ).click( function() {
		$.mobile.changePage("#bloodgroupmain", { transition: "slide", changeHash: true});
	});
});

$(document).ready(function() {
	var height = parseInt(($(document).innerHeight() * 2)/3);

	ad_height = parseInt($(document).innerHeight() * 0.6);

	$("#vserv_advt_slide li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",height-50);
	});
	
	$("#vserv_advt_slide_popup li").each(function(){
		$(this).find("img").css("width","100%");
	});

	
	$("#vserv_advt_slide_popup li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",height-50);
	});
	
	$("#clickbank_advt_popup li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",ad_height);
	});
});

var advttimer = setInterval(function(){
	$.mobile.changePage('#popup_advt_page', {
		transition : 'pop',
			     role       : 'dialog'
	});
}, 120000);



$(document).ready(function() {
	$( '#saverecord' ).click( function() {
		phonenumber = document.getElementById("phoneno").value;
		fname = document.getElementById("fname").value;
		lname = document.getElementById("lname").value;

		if(document.getElementById("bgroupo").checked)
			bloodgroup = "O";
		if(document.getElementById("bgroupa").checked)
			bloodgroup = "A";
		if(document.getElementById("bgroupb").checked)
			bloodgroup = "B";
		if(document.getElementById("bgroupab").checked)
			bloodgroup = "AB";

		
		if(document.getElementById("rhfactorplus").checked)
			rhfactor = "+";
		if(document.getElementById("rhfactorminus").checked)
			rhfactor = "-";

		if(phonenumber.length == 0)
		{
			alert("Plz enter phone number.");
			document.getElementById("phoneno").focus();
		}

		else if(fname.length == 0)
		{
			alert("Plz enter first name.");
			document.getElementById("fname").focus();
		}
		
		else
		{
			localrecord[0] = phonenumber;
			localrecord[1] = fname.toUpperCase();
			localrecord[2] = lname.toUpperCase();
			localrecord[3] = bloodgroup.toUpperCase();
			localrecord[4] = rhfactor;

			if(!localStorage.record)
			{

				recordarray[0] = localrecord;

				localStorage.record = JSON.stringify(recordarray);

				alert("Record Save");

			}
			else
			{
				var getrec = JSON.parse(localStorage.record);

				var reclen = getrec.length;

				var newrec = new Array(reclen+1);

				for(var i=0;i<newrec.length;i++)
				{
					newrec[i] = getrec[i];
				}

				newrec[newrec.length - 1] = localrecord;

				localStorage.clear();

				localStorage.record = JSON.stringify(newrec);

				alert("Record Save");
			}
		}

	});
});


$(document).ready(function() {
	$( '#button_7' ).click( function() {

		if(localStorage.getItem("record") === null)
			alert("No Record Found.");
		
		else if(JSON.parse(localStorage.record).length == 0)
			alert("No Record Found.");
		else
		{
			var getrec = JSON.parse(localStorage.record);

			$("#list-div").remove();

			$("#delete-div").append("<div id='list-div'></div>");


			getrec.sort(sort_by_column);


			var reclen = getrec.length;
			var innerrec = new Array();

			var liststr = "";
			var idnum;

			for(var i=0;i<reclen;i++)
			{
				innerrec = getrec[i];
				idnum = "list_" + i;
				liststr = $("<a id=" + idnum +  " class='ui-btn ui-btn-up-a ui-shadow ui-btn-corner-all' data-role='button' data-corners='true' data-shadow='true' data-iconshadow='true' data-wrapperels='span' data-theme='a' ><span class='ui-btn-inner'><span class='ui-btn-text'>" + innerrec[1] + " " + innerrec[2] + "</a>\n");
				$("#list-div").append(liststr);
				liststr.click(deletefn);

			}

			$.mobile.changePage("#deleterec", { transition: "slide", changeHash: true});
		}
	});
});

function deletefn()
{
	var idstr = $(this).attr("id");
	var idnum = idstr.substring(5);

	var num_id = parseInt(idnum);

	var getrec = JSON.parse(localStorage.record);
	getrec.sort(sort_by_column);

	var reclen = getrec.length;

	var newrec1 = new Array(reclen-1);

	for(var i=0,j=0;i<reclen,j<reclen-1;i++,j++)
	{
		if(num_id == i)
			i++;

		newrec1[j] = getrec[i];
	}


	localStorage.clear();

	localStorage.record = JSON.stringify(newrec1);

	$("#list-div").remove();

	$("#delete-div").append("<div id='list-div'></div>");

	var getrec = JSON.parse(localStorage.record);

	getrec.sort(sort_by_column);


	var reclen = getrec.length;
	var innerrec = new Array();

	var liststr = "";
	var idnum;

	for(var i=0;i<reclen;i++)
	{
		innerrec = getrec[i];
		idnum = "list_" + i;
		liststr = $("<a id=" + idnum +  " class='ui-btn ui-btn-up-a ui-shadow ui-btn-corner-all' data-role='button' data-corners='true' data-shadow='true' data-iconshadow='true' data-wrapperels='span' data-theme='a' ><span class='ui-btn-inner'><span class='ui-btn-text'>" + innerrec[1] + " " + innerrec[2] + "</a>\n");
		$("#list-div").append(liststr);
		liststr.click(deletefn);

	}

	$.mobile.changePage("#deleterec", { transition: "slide", changeHash: true});
	

	alert("Record Deleted");
	
}

function sort_by_column(a,b)
{
	return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0)); // a[1] for col. 2 etc...
}

$(document).ready(function() {
	$( '#button_5' ).click( function() {
		if(localStorage.getItem("record") === null)
			alert("No Record Found.");

		else if(JSON.parse(localStorage.record).length == 0)
			alert("No Record Found");
		else
		{

			var getrec = JSON.parse(localStorage.record);
			

			$("#personality-list-div").remove();

			$("#personality-div").append("<div id='personality-list-div'></div>");


			getrec.sort(sort_by_column);


			var reclen = getrec.length;
			var innerrec = new Array();

			var liststr = "";
			var idnum;

			for(var i=0;i<reclen;i++)
			{
				innerrec = getrec[i];
				idnum = "list_" + i;
				liststr = $("<a id=" + idnum +  " class='ui-btn ui-btn-up-a ui-shadow ui-btn-corner-all' data-role='button' data-corners='true' data-shadow='true' data-iconshadow='true' data-wrapperels='span' data-theme='a' ><span class='ui-btn-inner'><span class='ui-btn-text'>" + innerrec[1] + " " + innerrec[2] + "</a>\n");
				$("#personality-list-div").append(liststr);
				liststr.click(displayfn);

			}

			$.mobile.changePage("#personalitytype", { transition: "slide", changeHash: true});
		}
	});
});

function displayfn()
{
	clickcounter = Number(localStorage.bgpfclickcounter);

	if(isNaN(clickcounter))
		clickcounter = 0;

	if(clickcounter >= 5)
	{
		alert("Time to buy me a beer. \nKindly click and visit offers. \nYou will get 5 chances to see your friend personality.\n(If advt. not shown...plz check your internet connection and reload apps.)");
		$.mobile.changePage("#advt_page", { transition: "slide", changeHash: true});
	}
	else
	{
		if(localStorage.bgpfclickcounter)
			localStorage.bgpfclickcounter = Number(localStorage.bgpfclickcounter) + 1;
		else
			localStorage.bgpfclickcounter = 1;

	
		var idstr = $(this).attr("id");
		var idnum = idstr.substring(5);

		var num_id = parseInt(idnum);
		var getrec = JSON.parse(localStorage.record);
		getrec.sort(sort_by_column);

		var innerrec1 = new Array();

		innerrec1 = getrec[num_id];

		var group = innerrec1[3];
		var rh =  innerrec1[4];

		var name_group = innerrec1[1] + " " + innerrec1[2] + "<br/>Blood Group " + group + rh;

		var nutshellstr = "";
		var basicbehaviorstr = '';
		var tolerancestr = '';
		var futurepaststr = '';
		var emotionsstr = '';
		var workstr = '';

		if(group === "O")
		{
			nutshellstr = nutshell[0];
			behaviourstr = basicbehavior[0];
			tolerancestr = tolerance[0];
			pastfuturestr = futurepast[0];
			emotionstr = emotions[0];
			workstr = work[0];
		}

		if(group === "A")
		{
			nutshellstr = nutshell[1];
			behaviourstr = basicbehavior[1];
			tolerancestr = tolerance[1];
			pastfuturestr = futurepast[1];
			emotionstr = emotions[1];
			workstr = work[1];
		}

		if(group === "B")
		{
			nutshellstr = nutshell[2];
			behaviourstr = basicbehavior[2];
			tolerancestr = tolerance[2];
			pastfuturestr = futurepast[2];
			emotionstr = emotions[2];
			workstr = work[2];
		}

		if(group === "AB")
		{
			nutshellstr = nutshell[3];
			behaviourstr = basicbehavior[3];
			tolerancestr = tolerance[3];
			pastfuturestr = futurepast[3];
			emotionstr = emotions[3];
			workstr = work[3];
		}

		document.getElementById("name-group").innerHTML = name_group;
		document.getElementById("p-nutshell").innerHTML = "<ul style='text-align:left;'>" + nutshellstr + "</ul>";
		document.getElementById("p-behaviour").innerHTML = "<ul style='text-align:left;'>" + behaviourstr + "</ul>";
		document.getElementById("p-tolerance").innerHTML = "<ul style='text-align:left;'>" + tolerancestr + "</ul>";
		document.getElementById("p-vision").innerHTML = "<ul style='text-align:left;'>" + pastfuturestr + "</ul>";
		document.getElementById("p-emotion").innerHTML = "<ul style='text-align:left;'>" + emotionstr + "</ul>";
		document.getElementById("p-work").innerHTML = "<ul style='text-align:left;'>" + workstr + "</ul>";

		$.mobile.changePage("#personalitydisplay", { transition: "slide", changeHash: true});

	}
}

$(document).ready(function() {
	$( '#findrecord' ).click( function() {

		var bloodgroupemergenycy = "";
		var rhfactoremergency = "";

		if(document.getElementById("bgroup1o").checked)
			bloodgroupemergenycy = "O";
		if(document.getElementById("bgroup1a").checked)
			bloodgroupemergenycy = "A";
		if(document.getElementById("bgroup1b").checked)
			bloodgroupemergenycy = "B";
		if(document.getElementById("bgroup1ab").checked)
			bloodgroupemergenycy = "AB";

		if(document.getElementById("rhfactor1plus").checked)
			rhfactoremergency = "+";
		if(document.getElementById("rhfactor1minus").checked)
			rhfactoremergency = "-";

		if(localStorage.getItem("record") === null)
			alert("No Record Found.");

		else if(JSON.parse(localStorage.record).length == 0)
			alert("No Record Found");
		else
		{
			var getrec = JSON.parse(localStorage.record);

			var emergencyrec = new Array();

			var emergnecyinnerrec = new Array();

			$("#emergency-list-div").remove();

			$("#emergency-div").append("<div id='emergency-list-div'></div>");

			for(var i=0;i<getrec.length;i++)
			{
				emergnecyinnerrec = getrec[i];

				if(emergnecyinnerrec[3] === bloodgroupemergenycy && emergnecyinnerrec[4] === rhfactoremergency)
				{
					emergencyrec.push(getrec[i]);
				}
			}

			if(emergencyrec.length == 0)
			{
				alert("No Record Found");
			}
			else
			{
				var innerarray = new Array();
				var reclen = emergencyrec.length;
				var idnum;
				var liststr = "";

				emergencyrec.sort(sort_by_column);

				for(var i=0;i<reclen;i++)
				{
					innerarray = emergencyrec[i];
					idnum = "list_" + i;
					liststr = $("<a href='tel:" + innerarray[0] + "' id=" + idnum +  " class='ui-btn ui-btn-up-a ui-shadow ui-btn-corner-all' data-role='button' data-corners='true' data-shadow='true' data-iconshadow='true' data-wrapperels='span' data-theme='a' ><span class='ui-btn-inner'><span class='ui-btn-text'>" + innerarray[1] + " " + innerarray[2] + "</a>\n");
					$("#emergency-list-div").append(liststr);
				}

				$.mobile.changePage("#emergencylistpage", { transition: "slide", changeHash: true});
			}
		}
		
	});
});


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

$(window).load(function(){
	$("#clickbank_advt1").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
		
	});
});

$(window).load(function(){
	$("#clickbank_advt2").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button

	});
});

$(window).load(function(){
	$("#clickbank_advt3").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button

	});
});

$(window).load(function(){
	$("#clickbank_advt4").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button

	});
});


$(window).load(function(){
	$("#vserv_advt_slide").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
	});	
});

$(window).load(function(){
	$("#vserv_advt_slide_popup").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
	});	
});

$(window).load(function(){
	$("#clickbank_advt_popup").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
	});	
});

//////////////////////////////////////////////////
///////////////////////////////////////////////////
