function init() {
	Notes = "Off";
	Popup = "Off";
	document.getElementById("preloader").style.opacity = 0;
	setTimeout('document.getElementById("preloader").style.display = "none";',250);
	document.body.setAttribute('onunload','onload');
	if (!navigator.standalone) {window.scrollTo(0, 1);}
	setTimeout('slideIn();',500);
}   

function mainInit() {
	Notes = "Off";
	Popup = "Off";
	document.body.setAttribute('onunload','onload');
	setTimeout('slideIn();',1000);
	if (!navigator.standalone) {
		window.scrollTo(0, 1);
		document.getElementById("mainPage").style.height = 416 + 'px';
		setTimeout("alert('iBride Application is designed to run in full screen. Please press the plus sign at the bottom of your screen and select \"Add to Home Screen\" option.')",2000);
	}
	//preventScroll();
	setTimeout('titleAnimation();',2000);
} 

function titleAnimation() {
	document.getElementById("animatedText").style.opacity = 1;
	document.getElementById("animatedText").style.webkitTransform = "scale(1)";	
}

function noteOn() {
	Notes = "On";
	initdb();
	if (Popup=="On") {
		hide_popup(); 
		document.getElementById("mainNote").style.display = "block"; 
		document.getElementById("mainNote").style.opacity = "1"; 
		setTimeout('document.getElementById("mainPage").style.webkitTransform = "translate3d(-480px,0,0)";',1000); 
		return;
	}
	document.getElementById("mainNote").style.display = "block"; 
	document.getElementById("mainNote").style.opacity = "1"; 
	document.getElementById("mainPage").style.webkitTransform = 'translate3d(-480px,0,0)';
}

function noteOff() {
	Notes = "Off";
	document.getElementById("mainPage").style.webkitTransform = 'translate3d(0px,0,0)';
	setTimeout('document.getElementById("mainNote").style.display = "none";', 1000);
}

function editNote(client) {
	document.getElementById("mainPage").style.webkitTransform = 'translate3d(-480px,0,0)';
	Redirect('my-note-template.html', 900);
	setTimeout('query(client);', 200);
}

function noteFadeOn() {
	Notes = "On";
	initdb();
	document.getElementById("mainPage").style.webkitTransform = 'translate3d(-480px,0,0)';
	setTimeout('document.getElementById("mainPage").style.display = "none";',1000);
	document.getElementById("mainNote").style.display = "block";
	setTimeout('document.getElementById("mainNote").style.opacity = 1;', 500);	
}

function noteFadeOff() {
	Notes = "Off";
	document.getElementById("mainNote").style.opacity = 0;
	document.getElementById("mainPage").style.display = "block";
	setTimeout('document.getElementById("mainPage").style.webkitTransform = "translate3d(0px,0,0)";',1000);
}

function listingBack(){
	document.getElementById("listing").style.display = "block";
}

function loading() {
	document.write('<div id="preloader"><p>&nbsp;</p>');
	document.write('<p align="center">LOADING ...</p></div>');
	document.getElementById("preloader").style.opacity = 1;
} 

function slideIn() {  
	document.getElementById("mainPage").style.webkitTransform = "translate3d(0px,0,0)";
}

function slideOut() {  
	slide = "translate3d(-" + 480 + "px,0,0)";
	if (Popup=="On") {
		hide_popup(); 
		setTimeout('document.getElementById("mainPage").style.webkitTransform=slide;',1000); 
		return;
	}
	document.getElementById("mainPage").style.webkitTransform=slide;
}

function DivStartPosition() {  
	WindowWidth = window.innerWidth;
	DivWidth = WindowWidth + "px";
	DivStartPosition = "-" + WindowWidth + "px";
	document.getElementById("mainPage").style.left = DivStartPosition;
}

function show_popup() {
	if (Notes=="On") {return;}
	if (Popup=="On") {hide_popup(); return;}
	Popup = "On";
	document.getElementById('popup-menu').style.webkitTransform='translate(0,0px)';
}

function hide_popup() {
	Popup = "Off";
	document.getElementById('popup-menu').style.webkitTransform='translate(0,280px)';
}

function Redirect(redirectTo, timeoutPeriod) {
	setTimeout("location.href = redirectTo;",timeoutPeriod);
}

function OpenNew(redirectTo, timeoutPeriod) {
	setTimeout('window.open("redirectTo","website");',timeoutPeriod);
}

function GoBackButton()
{ 
   var myurl = "http://www.ibride.me"; 
   if(document.referrer.substring(0,20) == myurl) 
    {  setTimeout("document.location.href=document.referrer;",1000);	} 
  else 
    {  setTimeout("document.location.href=myurl;",1000);	} 
}

function buttonDown(id,url) {
	id.src=url;
}

function menuDown(id) {
	id.style.backgroundColor = "#AEAEAE";
	id.style.border = "1px solid #000";
}

function menuUp(id) {
	id.style.backgroundColor = "#FFF";
}

function redButtonDown(id) {
	id.className='button down'; 
}

function redButtonUp(id) {
	id.className='button'; 
}

function touchButton(id,image) {
	id.style.background='url('+ image + ')';
}

function preventScroll() {
	document.body.addEventListener('touchmove', function(e) {e.preventDefault();});
}

touchMove = function(event) {
	// Prevent scrolling on this element
	// event.preventDefault();
}

function loadImg(id,img) {
	document.getElementById(id).innerHTML = '<img src=\'' + img + '\' alt="" width="84" height="120" />';
}

function confirmCancel() {
	var answer = confirm('Are you sure?\nYour changes will not be saved!')
	if (answer){
		noteFadeOff();
	}
	else{
		return;
	}
}

function confirmCancel2() {
	var answer = confirm('Are you sure?\nYour changes will not be saved!')
	if (answer){
		listingBack();
		noteFadeOff();
	}
	else{
		return;
	}
}

function preloadImages() { 
  pic1= new Image(148,44); 
  pic1.src="/images/iphone/menu-cancel-save-down.png"; 
  pic2= new Image(148,44); 
  pic2.src="/images/iphone/menu-cancel-save.png"; 
  pic3= new Image(148,44); 
  pic3.src="/images/iphone/menu-save-down.png"; 
  pic4= new Image(148,44); 
  pic4.src="/images/iphone/menu-save.png"; 
}

function noteArea(){
	if (!navigator.standalone) {document.getElementById("noteArea").style.height = 322 + 'px';} 
	document.getElementById("noteArea").innerHTML = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td colspan="2" align="left" valign="top"><label>Category:</label><input name="noteCategory" type="text" id="noteCategory" size="21" maxlength="50" readonly="readonly" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td></tr><tr><td colspan="2" align="left" valign="top"><label>Name:</label><input name="noteName" type="text" id="noteName" size="24" maxlength="50" readonly="readonly" class="note" onchange="javascript:window.scrollTo(0, 1);" /</td></tr><tr><td colspan="2" align="left" valign="top"><label>Phone:</label><input name="notePhone" type="text" id="notePhone" size="20" maxlength="20" readonly="readonly" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td></tr><tr><td width="50%" align="left" valign="top"><label>Contact:</label><input name="noteContact" type="text" autocorrect="off" id="noteContact" size="9" maxlength="10" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td><td align="left" valign="top"><label>Cost:</label><input name="noteCost" type="text" autocorrect="off" id="noteCost" size="7" maxlength="7" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td></tr><tr><td width="50%" align="left" valign="top"><label>Deposit:</label><input name="noteDeposit" type="text" autocorrect="off" id="noteDeposit" size="7" maxlength="7" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td><td align="left" valign="top"><label>Date:</label><input name="noteDate" type="text" autocorrect="off" id="noteDate" size="9" maxlength="11" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td></tr><tr><td width="50%" align="left" valign="top"><label>Balance:</label><input name="noteBalance" type="text" autocorrect="off" id="noteBalance" size="7" maxlength="7" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td><td align="left" valign="top"><label>Due Date:</label><input name="noteDue" type="text" autocorrect="off" id="noteDue" size="7" maxlength="7" class="note" onchange="javascript:window.scrollTo(0, 1);" /></td></tr><tr><td colspan="2" align="left" valign="top"><p>Description of your package:</p><textarea name="noteDescription" cols="25" rows="6" autocorrect="off" class="note" id="noteDescription" onchange="javascript:window.scrollTo(0, 1);">Your notes here ...</textarea></td></tr></table>';
	populateForm();
}

function populateMenu(){
	var menu1 = '<div id="popup-call" ontouchstart="touchButton(this,\'/images/iphone/menu-call-on-down.png\');" ontouchend="touchButton(this,\'/images/iphone/menu-call-on.png\');" onclick="hide_popup(); updateTableUrl(); window.location=\'tel:' + notePhoneInitial + '\';"></div>';
	if ( noteSmsInitial == "" ) { var menu2 = '<div id="popup-sms-off"></div>'; }
	else { var menu2 = '<a href=\'sms:' + noteSmsInitial + '\'><div id="popup-sms" onclick="updateTableUrl();" ontouchstart="touchButton(this,\'/images/iphone/menu-sms-on-down.png\');" ontouchend="touchButton(this,\'/images/iphone/menu-sms-on.png\');"></div></a>'; }
	if ( noteAddressInitial == "" ){ var menu3 = '<div id="popup-gps-off"></div>'; }
	else { var menu3 = '<a href=\'http://maps.google.com/maps?q=' + noteAddressInitial + '\'><div id="popup-gps" onclick="updateTableUrl();" ontouchstart="touchButton(this,\'/images/iphone/menu-gps-on-down.png\');" ontouchend="touchButton(this,\'/images/iphone/menu-gps-on.png\');"></div></a>'; }
    var menu4 = '<div id="popup-email" ontouchstart="touchButton(this,\'/images/iphone/menu-email-on-down.png\');" ontouchend="touchButton(this,\'/images/iphone/menu-email-on.png\');" onclick="hide_popup(); slideOut(); Redirect(redirectTo=\'../quote.html\', timeoutPeriod=\'800\');""></div>';
	var menu5 = '<div id="popup-cancel" ontouchstart="touchButton(this,\'/images/iphone/menu-cancel-down.png\');" ontouchend="touchButton(this,\'/images/iphone/menu-cancel.png\');" onclick="hide_popup();"></div>';
	var menu6 = '<p>&nbsp;</p>';
	
	var menuItems = menu1 + "\n" + menu2 + "\n" + menu3 + "\n" + menu4 + "\n" + menu5 + "\n" + menu6;
	document.getElementById('popup-menu').innerHTML = menuItems;
	
	if ( noteWebInitial == "" ) { return; }
	var websiteLink = '<img src="../../../images/iphone/menu-website-on.png" onclick="slideOut(); updateTableUrl(); Redirect(redirectTo=\'' + noteWebInitial + '\', timeoutPeriod=\'800\')" />';
	document.getElementById('website-button').innerHTML = websiteLink;
}

function populateForm(){
	document.getElementById('noteCategory').value = noteCategoryInitial;
	document.getElementById('noteName').value = noteNameInitial;
	document.getElementById('notePhone').value = notePhoneInitial;
	document.getElementById('noteContact').value = noteContactInitial;
	document.getElementById('noteCost').value = noteCostInitial;
	document.getElementById('noteDeposit').value = noteDepositInitial;
	document.getElementById('noteDate').value = noteDateInitial;
	document.getElementById('noteBalance').value = noteBalanceInitial;
	document.getElementById('noteDue').value = noteDueInitial;
	document.getElementById('noteDescription').value = noteDescriptionInitial;
}

function dhtmlLoadScript(){
	var e = document.createElement("script");
    e.src = "/javascript/iphone-db.js";
    e.type="text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}

function loadAdImage(url) {
	document.getElementById("ad").innerHTML = '<p align="center"><img src="' + url +'" alt="" align="middle" onclick="show_popup();" /></p>'
}
