var url = "/ajax.php";

function load(page) {
var load = window.open(page,'','scrollbars=no,menubar=no,height=400,width=500,resizable=no,toolbar=no,location=no,status=no');
}

function showPage(page,scrollbars,width,height){
var load = window.open(page,'','scrollbars=' + scrollbars + ',menubar=no,height=' + height + ',width=' + width + ',resizable=no,toolbar=no,location=no,status=no');
}


function showSecondTopFloater(){
	me = document.getElementById("second_top_floater");
	if (!(me != null)){
		me = document.all.second_top_floater;
	}
		if (me != null && me.style.display=="block"){
			me.style.display="none";
		    //link.innerHTML="絵文字表示";
			}
		else {
			if (me != null && me.style.display=="none"){
				me.style.display="block";
			    //link.innerHTML="絵文字非表示";
			} else {
			    me.style.display="block";
			}
		}
}


function showSecondTopFloaterRight(){
	me = document.getElementById("second_top_floater_right");
	if (!(me != null)){
		me = document.all.second_top_floater_right;
	}
		if (me != null && me.style.display=="block"){
			me.style.display="none";
		    //link.innerHTML="絵文字表示";
			}
		else {
			if (me != null && me.style.display=="none"){
				me.style.display="block";
			    //link.innerHTML="絵文字非表示";
			} else {
			    me.style.display="block";
			}
		}
}



function uniqueClientUsername(username,lang){
	if (username.length < 1){
		return true;
	}
	var data="function=isUniqueUsername";
	data += "&username=" + username;
	data += "&lang=" + lang;
	httpRequest("POST",url,true,handleUniqueUsernameUpdate,data);
	//$.post(url, { "function":"isUniqueUsername","username":username,"lang":lang},handleUniqueUsernameUpdate);
}


function handleUniqueUsernameUpdate(response){
if(request.readyState == 4){
if (request.status == 200){
			var response = request.responseText;
			if (response.length > 0){
				alert(response);
				document.getElementById("username").value="";
			}
   } else {
       alert("A problem occurred. Request.status was " + request.status);
   }

	} 
}




function uniqueClientPassword(password,lang)
{
	if (password.length < 1){
		return true;
	}
	var data="function=isUniquePassword";
	data += "&password=" + escape(password);
	data += "&lang=" + lang;
	httpRequest("POST",url,true,handleUniquePasswordUpdate,data);
}


function handleUniquePasswordUpdate(){
if(request.readyState == 4){
if (request.status == 200){
			var response = request.responseText;
			if (response.length > 0){
				alert(response);
				document.getElementById("password").value="";
			}
   } else {
       alert("A problem occurred. Request.status was " + request.status);
   }

	} 
}


function flashFix(o){document.write(o)}


function addLoadEvent(func) { 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') { 
	    window.onload = func; 
	  } else { 
	    window.onload = function() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    } 
	  } 
	} 

function openWindow(page) {
var load = window.open(page,'','scrollbars=no,menubar=no,height=400,width=500,resizable=no,toolbar=no,location=no,status=no');
}

//	addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); 



function addCenterElement(code) {
  var ni = document.getElementById('messageDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = code + '<br><a href=\'#\' onclick=\'removeCenterElement('+divIdName+')\'>Close</a>';
  ni.appendChild(newdiv);
}



function removeCenterElement(divNum) {
  var d = document.getElementById('messageDiv');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}


// for iphone
function updateLayout(){
	switch(window.orientation){
	case 0: case 180:
		var orient = "portrait";
		break;
	case 90: case -90:
		var orient = "landscape";
		break;
	}
	document.body.setAttribute("orient",orient);
	setTimeout(function() { window.scrollTo(0,1);},0);
}


function setAdvice(content){
if (document.getElementById("advice") != null){
document.getElementById("advice").value=content;
}
}


function setBackground(bg){
//if (document.getElementById("mobilescreen") != null){
//document.getElementById("mobilescreen").src = '/images/' + bg + '.jpg';
//}
}

$(document).ready(function(){
   $(".toggleMeElmo").click(function () {
     if ($(".elmo").is(":hidden")) {
        $(".elmo").slideDown("slow");
      } else {
        $(".elmo").hide();
      }
   });
   $(".focus").focus();
   $(".toggleTheInterview").click(function () {
     if ($(".TheInterview").is(":hidden")) {
        $(".TheInterview").slideDown("slow");
      } else {
        $(".TheInterview").hide();
      }
   });
   $(".toggleTheInterview2").click(function () {
     if ($(".TheInterview2").is(":hidden")) {
        $(".TheInterview2").slideDown("slow");
      } else {
        $(".TheInterview2").hide();
      }
   });
   $(".toggleThankYou").click(function () {
     if ($(".ThankYou").is(":hidden")) {
        $(".ThankYou").slideDown("slow");
      } else {
        $(".ThankYou").hide();
      }
   });
   $(".toggleRecipe").click(function () {
     if ($(".Recipe").is(":hidden")) {
        $(".Recipe").slideDown("slow");
      } else {
        $(".Recipe").hide();
      }
   });
   $(".toggleExpandamenu").toggle(
								   function(){
									$("#expandamenu").slideDown("slow");
							   },
								   function(){
										$("#expandamenu").slideUp("slow");
							   }
								  );
   
 


});


var request = null;

/* import this into any file to use Ajax XMLHttpRequests
   params:
   reqType - GET or POST
   url - of servlet that will return data
   asynch - true or false
   respHandle - function that will handle the response
   5th param is optional and should contain the data a POST request is supposed to send
*/
function httpRequest(reqType,url,asynch,respHandle){
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }
     }
   }

	if (request){
		if(reqType.toLowerCase() != "post"){
			initReq(reqType,url,asynch,respHandle);
		} else {
			var args = arguments[4];
			if (args != null && args.length > 0){
				initReq(reqType,url,asynch,respHandle,args);
			}
		}
		
	} else {
        alert("Error initializing XMLHttpRequest!");
	}
	
}


function initReq(reqType, url, bool, respHandle){
	try{
	request.onreadystatechange = respHandle;
	request.open(reqType,url,bool);
	if (reqType.toLowerCase() == "post"){
			request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		request.send(arguments[4]);
	} else {
		request.send(null);
	}
	} catch(errv){
		alert("The application cannot contact the server at the moment.\n"+
			 "Please try again in a few seconds.\n" + 
			  "Error detail:" + errv.message);
	}
}
   function getPriceSchemes() {
     //var service = document.getElementById("service_id").value;
     var dbName = "";
     var dbNameElement = document.getElementById("dbname");
     if (dbNameElement != null)
     dbName = dbNameElement.value;
     var url = "/servlet/thetamusic.ymds.server.admin.tools.AjaxServlet?action=getPriceSchemeInfo&dbname=" + dbName;
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
   }

   function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) {
      response = request.responseText.split("|");
      updatePriceSchemes(document.getElementById("service_id").value);
       }
  }
   }

function updatePriceSchemes(selectedPrice){

  //var response = request.responseText.split("|");
   for (var i=0; i<response.length; i++) {
    var battalion = response[i];
    var unit = battalion.split("%");
    var service_id = unit[0];
    var service_options = unit[1];
    var optionList = document.getElementById("price_scheme");
    if (service_id == selectedPrice){
      ClearOptions(optionList);
      var eachone = service_options.split(",");
      if (eachone.length > 2){
        AddToOptionList(optionList,0, "All Price Schemes");
      }
      for (var j=0;j<eachone.length;j+=2){
        AddToOptionList(optionList, eachone[j+1], eachone[j]);
      }
    }
  }

 	// each section of response
  if (selectedPrice == 0 || optionList.length == 0){
    var optionList = document.getElementById("price_scheme");
    ClearOptions(optionList);
    AddToOptionList(optionList,0, "All Price Schemes");
  }
  // set price scheme to last set price scheme
  document.getElementById("price_scheme").value = document.getElementById("last_price_scheme_id").value;
}


function ClearOptions(OptionList) {
   for (x = OptionList.length; x >= 0; x--) {
      OptionList[x] = null;
   }
}


function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
  ot = new String(OptionText);
  ov = new String(OptionValue);
  ot.replace('"','');
  ov.replace('"','');
   OptionList[OptionList.length] = new Option(ot, ov);
}