// JavaScript Document
function helptext_clear(obj, string) {
	if(obj.value == string){
		obj.value="";
		obj.className = "input_post";
	}
}

function helptext_check(obj, string) {
	if(obj.value==""){
		obj.className = "input_pre";
		obj.value=string;
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var request;

function createRequest() {
  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)
    alert("Error initializing XMLHttpRequest!");
}

function ajax_search(searchstring) {
	if(searchstring.length > 2){
		document.getElementById("loading_search").style.display = "block";
		createRequest();
		var url = "ajax_search.php?searchstring="+escape(searchstring)+"&rand="+Math.floor(Math.random()*100000);
		request.open("GET", url, true);
		request.onreadystatechange = updateSearch;
		request.send(null);
	}
}
function updateSearch() {
     if (request.readyState == 4){
		 if (request.status == 200){
       		//alert("Server is done!");
			var response = request.responseText;
			document.getElementById("search_results").style.display = "block";
			document.getElementById("search_results_content").innerHTML = response;
			document.getElementById("loading_search").style.display = "none";
		 }else if (request.status == 404){
         	alert("Request URL does not exist");
		 }else{
         	alert("Error: status code is " + request.status);
		 }
	 }
}

function findLinkedProducts(action, linkID, toLinkID, linked_price) {
	document.getElementById("loading_products").style.display = "block";
	createRequest();
	var url = "linked_product_services.php?action="+escape(action)+"&linkID="+escape(linkID)+"&toLinkID="+escape(toLinkID)+"&linked_price="+escape(linked_price)+"&rand="+Math.floor(Math.random()*100000);
     request.open("GET", url, true);
     request.onreadystatechange = updatePageLinked;
     request.send(null);
}
function updatePageLinked() {
     if (request.readyState == 4){
		 if (request.status == 200){
       		//alert("Server is done!");
			var response = request.responseText;
			document.getElementById("linked_product_details").innerHTML = response;
			document.getElementById("loading_products").style.display = "none";
		 }else if (request.status == 404){
         	alert("Request URL does not exist");
		 }else{
         	alert("Error: status code is " + request.status);
		 }
	 }
}

function groupSevices(action, productID, groupID, parentID, string) {
	document.getElementById("loading_groups").style.display = "block";
	createRequest();
	var url = "group_services.php?action="+escape(action)+"&productID="+escape(productID)+"&groupID="+escape(groupID)+"&parentID="+escape(parentID)+"&string="+escape(string)+"&rand="+Math.floor(Math.random()*100000);
     request.open("GET", url, true);
     request.onreadystatechange = updatePageGroups;
     request.send(null);
}
function updatePageGroups() {
     if (request.readyState == 4){
		 if (request.status == 200){
       		//alert("Server is done!");
			var response = request.responseText;
			document.getElementById("groups_info").innerHTML = response;
			document.getElementById("loading_groups").style.display = "none";
		 }else if (request.status == 404){
         	alert("Request URL does not exist");
		 }else{
         	alert("Error: status code is " + request.status);
		 }
	 }
}

function findProducts(groupID) {
	document.getElementById("loading_products2").style.display = "block";
	createRequest();
	var url = "find_products.php?groupID="+escape(groupID)+"&rand="+Math.floor(Math.random()*100000);
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
}
function updatePage() {
     if (request.readyState == 4){
		 if (request.status == 200){
       		//alert("Server is done!");
			var response = request.responseText;
			document.getElementById("product_details").innerHTML = response;
			document.getElementById("loading_products2").style.display = "none";
		 }else if (request.status == 404){
         	alert("Request URL does not exist");
		 }else{
         	alert("Error: status code is " + request.status);
		 }
	 }
}

function calcprice(){
	var name = document.getElementById('base_name').value+"\n";
	var baseprice = document.getElementById('base_price').value;
	var numgroups = document.getElementById('numgroups').value;
	
	var total = 0;
	for(i=0;i<numgroups;i++){
		eval("numoptions = document.getElementById('numoptions_'+i).value");
		eval("grouptype = document.getElementById('grouptype_'+i).value");
		if(grouptype == "radio"){
			for(var j=0;j<numoptions;j++){
				var radio = document.form1.elements['option_'+i];
				if(radio[j].checked ){
					data = radio[j].value;
					var date_array = new Array();
					date_array = data.split("@");
					name += "("+date_array[2]+")\n";
					total += parseFloat(date_array[1]); 
				}
			}
		}else{
			for(var j=0;j<numoptions;j++){
				eval("checkbox = document.getElementById('option_'+i+'_'+j)");
				if(checkbox.checked){
					//alert('option_'+i+'_'+j+'=checked');
					data = checkbox.value;
					var date_array = new Array();
					date_array = data.split("@");
					name += "("+date_array[2]+")\n";
					total += parseFloat(date_array[1]);
				}
			}
		}
	}
	total += parseFloat(baseprice);
	document.getElementById('price').value = total.toFixed(2);
	document.getElementById('name').value = name;
	document.getElementById('final_price').innerHTML = "Total price: &pound;"+total.toFixed(2);
	//alert(name);
	//-----debuging--------
	//alert_string = "baseprice="+baseprice+"\n";
	//alert_string += "numgroups="+numgroups+"\n";
	//alert(alert_string);
	//-----end debuging--------
}

function changepayment(target){
	if(target == "card"){
		document.getElementById('card_panel').style.display = "block";
	} else {
		document.getElementById('card_panel').style.display = "none";
	}
}

sfHover = function() {
	var sfEls = document.getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//-------------Admin JavaScript---------------------
function Init() { 
 	document.getElementById('iView').contentWindow.document.designMode = "on";
	//document.getElementById('iView').document.body.innerHTML="wertqwerqwer";
  	document.getElementById("iView").contentWindow.focus();
}
function content_init() {
	document.getElementById('iView').contentWindow.document.designMode = "on"; 
	//alert(document.getElementById('story_holder').innerHTML);
	setTimeout("document.getElementById('iView').contentWindow.document.body.innerHTML = document.getElementById('story_holder').innerHTML;",1000);
	//alert(document.getElementById('iView').contentWindow.document.body.innerHTML);
	document.getElementById("iView").contentWindow.focus();
}
function fixoldformating() {
	s = document.getElementById('story_holder').innerHTML;
	alert(s);
	//document.getElementById('iView').contentWindow.document.body.innerHTML = s.replace('*\n*', '<br />');
	out = "\n"; // replace this
	add = "<br />"; // with this
	temp = "" + s;

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	alert(temp);
	document.getElementById('iView').contentWindow.document.body.innerHTML = temp;
}
function resettext() {
	s = document.getElementById('story_holder').innerHTML;
	document.getElementById('iView').contentWindow.document.body.innerHTML = s;
}
function preSend() {
	story_content = document.getElementById('iView').contentWindow.document.body.innerHTML;
	document.form1.story.value = story_content;
	//alert(document.form1.story.value);
	document.form1.submit();
}
function boldIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('bold', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function underlineIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('underline', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function listIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('InsertOrderedList', false, null);
	document.getElementById("iView").contentWindow.focus();
	//document.getElementById('iView').focus(); 
}
function unorderedlistIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('InsertUnorderedList', false, null); 
	document.getElementById("iView").contentWindow.focus();
	//document.getElementById('iView').focus(); 
}
function viewsource(){
	alert(document.getElementById('iView').contentWindow.document.body.innerHTML);
}
function italicIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('Italic', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function printIt() { 
	document.getElementById('iView').contentWindow.document.execCommand('Print', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function newfont(newfont) {
	document.getElementById('iView').contentWindow.document.execCommand('fontname', false, newfont); 
	document.getElementById("iView").contentWindow.focus();
}
function newsize(newsize){
	document.getElementById('iView').contentWindow.document.execCommand('fontsize', false, newsize); 
	document.getElementById("iView").contentWindow.focus();
}
function formatblock(newtag){
	document.getElementById('iView').contentWindow.document.execCommand('FormatBlock', false, newtag); 
	document.getElementById("iView").contentWindow.focus();
}
function unformatIt(){
	document.getElementById('iView').contentWindow.document.execCommand('RemoveFormat', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function linkIt(){//Identify selected text
//var sText = document.selection.createRange();
//if (!sText==""){
//    //Create link
//     document.execCommand("CreateLink");
//     //Replace text with URL
//     if (sText.parentElement().tagName == "A"){
//       sText.parentElement().innerText=sText.parentElement().href;     }    
//  }else{
//    alert("Please select some blue text!");
//  }
	document.getElementById('iView').contentWindow.document.execCommand('CreateLink', true); 
	document.getElementById("iView").contentWindow.focus();
}
function unlinkIt(){
	document.getElementById('iView').contentWindow.document.execCommand('Unlink', false, null); 
	document.getElementById("iView").contentWindow.focus();
}
function show_col(id){
	//document.getElementById('col_pal').style.left=document.getElementById(id).style.left;
	document.getElementById('col_pal').style.visibility="visible";
}
function preSend() {
	story_content = document.getElementById('iView').contentWindow.document.body.innerHTML;
	document.form1.story.value = story_content;
	//alert(document.form1.story.value);
	document.form1.submit();
}
//---------------------------------------------------
