
//alert("in formfuncs!");

//nb ID is passed in, not 'this'
function getDropdownValue(id){//generic
	var theObj=document.getElementById(id);
        var index=theObj.selectedIndex;
        var code=theObj.options[index].value;
        return code;
}

function setDropdownValue(id, newval){//generic
	var theObj=document.getElementById(id);
        var index=theObj.selectedIndex;
        theObj.options[index].value = newval;
        return true;
}

function copyDropdownIndex(id_from, id_to) {
        //var theObj=document.getElementById(id);
        //theObj.options[index].value = newval;
        var obj_from=document.getElementById(id_from);
	if (obj_from !== null) {
		var new_index=obj_from.selectedIndex;
		var obj_to=document.getElementById(id_to);
		if (obj_to !== null) {
			//try {
				obj_to.selectedIndex= new_index;
			//} catch (err) {
				//alert("set failed");
			//}
		}
	}
        return true;
}

//----------------------------------------------

function format(expr, decplaces){
	var str=""+Math.round(eval(expr)*Math.pow(10,decplaces));
	while ( str.length <= decplaces){
		str = "0"+str;
	}
	var decpoint=str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function isEmpty(inp){ inp=inp.replace(/ /g,"");if (inp==null || inp == "") { return true; } return false; }
function isFilled(inp){ inp=inp.replace(/ /g,"");if (inp==null || inp == "") { return false; } return true; }

function verNum(form,input,min,max){
  var theObj=eval("document."+form+"."+input);
  var theVal=parseFloat(theObj.value);
  if (isNaN(theVal)){
    alert("Only Numbers Allowed in "+input+" field")
	theObj.value = min;
	return;
  }
  if (min != max && (theVal < min || theVal > max)){
    alert("Only Numbers Between "+min+" and "+max+" allowed")
	theObj.value = min;
	return;
  }
  return;
}

function CreateArray(length)
{
   this.size=(length+3);
   for(var i = 1; i <= length+3; i++)
      this[i] = "Unacceptable character\nOnly alpha-numerics, underscores, hyphens and periods are allowed.";
}

function valMail(address)
{
	
   if(address.length == 0) return 0; 
	
   invalidchars = new Array(' ', ',', '[', ']', '/', '=', ';', '`', '+', '!', '#', '$', '%', '^', '&', '*', '(', ')', '~', ':', '\"', '\'', '\b', '<', '>', '?', '|', '{', '}');
   var numchars = invalidchars.length;
   error_messages = new CreateArray(numchars);  
   error_messages[0] = "Valid";
   error_messages[1] = "Address must have an @ then a '.'\nand cannot begin with @";
   error_messages[2] = "Address cannot have '.' next to @ or as final character";
   error_messages[3] = "Spaces are invalid";
   error_messages[4] = "Commas are invalid";
   error_messages[5] = "Brackets are invalid";
   error_messages[6] = "Brackets are invalid";
   error_messages[7] = "Slashes are invalid";
   error_messages[8] = "Equals are invalid";
   error_messages[9] = "Semi-Colons are invalid";
   error_messages[10] = "Accents are invalid";

   var atloc=address.indexOf('@');
   var dotloc=address.lastIndexOf('.');
         
   // Tests For one '.' and one '@' in correct order
   // And makes sure first substring isn't null
   if(atloc<1||dotloc==-1||dotloc<atloc)
   {
      alert("\nInvalid E-Mail Address\n" + error_messages[1] + "\nExample: bob@foo.com" );           
      return 1;
   }
                
   // Tests second and third substrings for nullness
   else if(dotloc < atloc+2 || address.length < dotloc+2)
   {
      alert("\nInvalid E-Mail Address\n"+error_messages[2] + "\nExample: bob@foo.com");     
      return 2;
   }             
        
   // Tests for individual syntax errors
   // Only common keystroke errors included!        
   for(var ct=0;ct<numchars;ct++)
   {
      status=invalidchars[ct];
      if(address.indexOf(invalidchars[ct])!=-1)
      {
         alert("\nInvalid E-mail Address\n" + error_messages[ct+3] + "\nExample: bob@foo.com");
         return (ct+3);
      }
   }
   return 0;
}

function checkMail(form){
	var obj=eval('document.'+form);
	var address=obj.email.value;
	var ret = valMail(address);
	if (ret != 0){
		obj.email.value = "";
	}
}


function recalc(){
	var delcharge=0;
	form=document.details;
	if ( isFilled(form.deladdress.value) ){
		delcharge=parseFloat(form.delcountry.options[form.delcountry.selectedIndex].value);
	} else {
		delcharge=parseFloat(form.country.options[form.country.selectedIndex].value);
	}
	form.price.value="£"+format(parseFloat(form.price1.value),2);
	form.total.value="£"+format(parseFloat(form.price1.value) + delcharge,2);
	form.service.value = "£"+format(delcharge,2);
	form.service1.value = delcharge*100;
}

var tncWind=null;

function openTNC(file){
	if (tncWind==null || tncWind.closed){
		tncWind=top.open(file,'Terms','toolbar=no,location=no,scrolling=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600');
		if (tncWind.opener==null){
			tncWind.opener=self;
		}
	}
	tncWind.focus();
	tncWind.top.name='mywindow';
} 
//------------------------------------------------------



function getHTMLifExists(id) {
	var ret=false;
	var obj=document.getElementById(id);
	//alert("getting id"+id);
	if (obj !== null) { 
		//alert("got id"+id);
		ret=obj.innerHTML;
	}
	return ret;
}

function setHTMLifExists(id, new_value) {
	var ret=false;
	var obj=document.getElementById(id);
	if (obj !== null) { 
		obj.innerHTML = new_value; 
		ret=true;
	}
	return ret;
}


//nb 'this' is passed in, not ID
function getSelectedValue(obj)
{
	var selected_index = obj.selectedIndex;
	return obj.options[selected_index].value;
}




