//function parentExists()
//{
// return (parent.location == window.location)? false : true;
//}
//if(!parentExists()){
//	window.location.href = "index.asp";
//}

function startTimer(){
	moveMenu();
	//setTimeout(moveMenu,100);
}

function moveMenu(){
	iV = document.body.scrollTop;
	//diff = parseInt(menu.style.top) - (iV+0);
	//if(diff != 0){
	//	if(diff < 0){
			//menu.style.top = parseInt(menu.style.top)+1+"px";
			//setTimeout(moveMenu,100);
	//	}
		
	//	if(diff > 0){
			//menu.style.top = parseInt(menu.style.top)-1+"px";
			//setTimeout(moveMenu,100);
	//	}
	//}
	//catmenu.style.top = iV+132;
	//tools.style.top = iV+131;
	menu.style.top = iV;
	
}

window.onscroll = startTimer;

function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected 
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function getOrder(listField){
	ordering.ordered.value = "";
	for (i = 0; i < listField.length; i++){
		ordering.ordered.value = ordering.ordered.value + listField[i].value;
		if(i < listField.length-1){
			ordering.ordered.value = ordering.ordered.value + ','
		}
	}
	ordering.submit();
}

function chkQty(boxQty,qty,field,update,form){
	if(((qty/boxQty) - (parseInt(qty/boxQty))) == 0 && qty != 0){
		if(update == 1){
			form.submit();
		}else{
			return true;
		}
	}else{
		if(isNaN(qty)){
			qty = 1
		}
		needed = Math.round((qty/boxQty)+0.5);
		if(needed > 1){
			b = "boxes";
		}else{
			b = "box";
		}
		if(confirm('This product is sold in boxes of '+boxQty+'. For the quantity you requested you will need '+needed+' '+b+'.\nWould you like the quantity correcting for this?')){
			field.value = needed*boxQty;
			if(update == 1){
				form.submit();
			}
		}
		return false;
	}
}

function trim(str){
  return str.replace(/^\s+|\s+$/g, '')
}

function checkForm(form){
	formElements = form.elements;
	err = "";
	
	for(i=0; i<formElements.length; i++){
		if(formElements[i].className == "validate"){
			if(trim(formElements[i].value) == ""){
				err = err + formElements[i].name + "\n";
			}
		}
		if(formElements[i].className == "validateNum"){
			if(trim(formElements[i].value) == "" || isNaN(formElements[i].value)){
				err = err + "Valid: "+formElements[i].name + "\n";
			}
		}
		if(formElements[i].className == "validateChk"){
			if(!formElements[i].checked){
				err = err + formElements[i].name + "\n";
			}
		}
		if(formElements[i].className == "validatePCode"){
			pcexp = new Array ();
			pcexp.push (/^([a-z]{1,2}[0-9]{1,2})(\s*)([0-9]{1}[abdefghjlnpqrstuwxyz]{2})$/i);
			pcexp.push (/^([a-z]{1,2}[0-9]{1}[a-z]{1})(\s*)([0-9]{1}[abdefghjlnpqrstuwxyz]{2})$/i);
			pcexp.push (/^(GIR)(\s*)(0AA)$/i);
			postCode = formElements[i].value;
			valid = false;
			for(j=0; j<pcexp.length; j++){
				if(pcexp[j].test(postCode)){
				  pcexp[j].exec(postCode);
				  postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
				  valid = true;
				  break;
				}
			}
			if(valid){
			}else{
				err = err + "Valid: "+formElements[i].name + "\n";
			}
		}
		if(formElements[i].className == "validateVerify"){
			if(trim(formElements[i].value) == ""){
				err = err + formElements[i].name + "\n";
			}else{
				if(trim(formElements[i].value) != trim(formElements[i+1].value)){
					err = err + formElements[i].name+"s do not match\n";
				}
			}
		}
		if(formElements[i].className == "validateEmail"){
			email = formElements[i].value;
  			atPos = email.indexOf("@");
			if (atPos > 0){
				dotPos = email.indexOf(".",atPos);
				if ((dotPos < atPos) || (email.length == dotPos+1) || (dotPos == -1) || (dotPos == atPos+1)){
					err = err + "Valid: "+formElements[i].name + "\n";
				}
			}else{
				err = err + "Valid: "+formElements[i].name + "\n";
			}
		}
		if(formElements[i].className == "validateDate"){
			dateStr = formElements[i].value;
			dateValid = true;
			datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
			matchArray = dateStr.match(datePat);
			if (matchArray == null) {
				dateValid = false;
			}else{
				month = matchArray[3];
				day = matchArray[1];
				year = matchArray[4];
				if (month < 1 || month > 12) {
					dateValid = false;
				}
				if (day < 1 || day > 31) {
					dateValid = false;
				}
				if ((month==4 || month==6 || month==9 || month==11) && day==31) {
					dateValid = false;
				}
				if (month == 2) {
					isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
					if (day>29 || (day==29 && !isleap)) {
						dateValid = false;
					}
				}
				if(year < 1855){
					dateValid = false;
				}
			}
			if(!dateValid){
				err = err + "Valid "+formElements[i].name +": DD/MM/YYYY\n";
			}
		}
	}
	
	if(err == ""){
		return true
	}else{
		alert("Please provide the following:\n\n"+err);
		return false
	}
}

function confirmAction(msg,url){
	if(confirm(msg)){
		window.location.href = url;
	}
}

function winOpen(url,name){
	if(name == ""){
		name = "full";
	}
	vars = window.open(url,name,"width=400,height=400,resizable=yes,scrollbars=yes");
}

function winOpenInfo(url,name){
	if(name == ""){
		name = "full";
	}
	vars = window.open(url,name,"width=400,height=200,resizable=yes,scrollbars=yes");
}