function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function postValidate(refForm) {
	// validates promotion form
	strError="";
	
	// internet only
	var io=document.getElementById('internetonly').checked;
	
	if (refForm.vendor_name.value.length==0) {
		strError+=" - Please enter the Vendor Name\n";	
	}
	if ((refForm.vendor_postcode1.value.length==0) && (!io)) {
		strError+=" - Please enter the Vendor Postcode\n";	
	}
	if (refForm.title.value.length==0) {
		strError+=" - Please enter the Promotion Title\n";	
	}
    if ((refForm.logo.value.length>1) && (!check_extension(refForm.logo.value))) {
		 strError+=" - The filetype for your logo is invalid. Please use images with JPEG or JPG extensions\n";	
    }	
	if (refForm.fullinfo.value.length==0) {
		strError+=" - Please enter a description\n";	
	}	
	if (!refForm.terms.checked) {
		strError+=" - You must agree to the terms and conditions\n";	
	}
	if ((refForm.vendor_postcode1.value.length>0) && (!io)) {
		// check for postcode in arrValidPC array (created in postcodes.php)
		var ok=false;
		for (i=0;i<arrValidPC.length;i++) {
			if (refForm.vendor_postcode1.value.toLowerCase()==arrValidPC[i].toLowerCase()) {
				var ok=true;
				break;
			}
		}
		if (!ok) {
			strError+=" - Invalid postcode entered\n";
		}
		
	}
	if (strError.length>0) {
		alert(strError);
		return false;
	} else {
		return true;	
	}
					  
}

function postValidate_1(refForm) {
	// validates page 1 of promotion form
	strError="";
	
//	if (refForm.vendor_name.value.length==0) {
//		strError+=" - Please enter the Vendor Name\n";	
//	}
	if (refForm.title.value.length==0) {
		strError+=" - Please enter the Promotion Title\n";	
	}
    if ((refForm.logo.value.length>1) && (!check_extension(refForm.logo.value))) {
		 strError+=" - The filetype for your logo is invalid. Please use images with JPEG or JPG extensions\n";	
    }	
	if (refForm.fullinfo.value.length==0) {
		strError+=" - Please enter a Description\n";	
	}	
	if (strError.length>0) {
		alert(strError);
		return false;
	} else {
		return true;	
	}
					  
}

function postValidate_2(refForm) {
	// validates page 2 of promotion form (products)
	strError="";
	if (refForm.title.value.length==0) {
		strError+=" - Please enter the Product Title\n";	
	}	
	//if (refForm.logo.value.length==0) {
		//strError+=" - Please supply a product image\n"; 
	//}
    if ((refForm.logo.value.length>1) && (!check_extension(refForm.logo.value))) {
		 strError+=" - The filetype for your image is invalid. Please use images with JPEG or JPG extensions\n";	
    }	
	if (strError.length>0) {
		alert(strError);
		return false;
	} else {
		return true;	
	}					  
}
function postValidate_3(refForm) {
	// validates page 3 of promotion form (locations)
	strError="";
// internet only
	var io=document.getElementById('internetonly').checked;
	
	if ((refForm.vendor_postcode1.value.length==0) && (!io)) {
		strError+=" - Please enter the Vendor Postcode\n";	
	}
	if (!refForm.terms.checked) {
		strError+=" - You must agree to the terms and conditions\n";	
	}
	if ((refForm.vendor_postcode1.value.length>0) && (!io)) {
		// check for postcode in arrValidPC array (created in postcodes.php)
		var ok=false;
		for (i=0;i<arrValidPC.length;i++) {
			if (refForm.vendor_postcode1.value.toLowerCase()==arrValidPC[i].toLowerCase()) {
				var ok=true;
				break;
			}
		}
		if (!ok) {
			strError+=" - Invalid postcode entered\n";
		}
		
	}	
	if (strError.length>0) {
		alert(strError);
		return false;
	} else {
		return true;	
	}					  
}
function check_extension(strWhat){
    var arrAllowed = ["jpeg","jpg"];
    for (i=0;i<=arrAllowed.length;i++){
        if (strWhat.toLowerCase().indexOf(arrAllowed[i])>0){
            return true;
        }
    }
    return false;
}

function questionValidate(refForm) {
	// validates question form
	strError="";
	if (refForm.txt.value.length==0) {
		strError+=" - Please enter a message\n";	
	}
	if (strError.length>0) {
		alert(strError);
		return false;
	} else {
		return true;	
	}
					  
}

function checkContinous(ref) {
	if(ref) {
		// continous is checked
		document.forms[0].end_day.disabled=true;
		document.forms[0].end_month.disabled=true;		
		document.forms[0].end_year.disabled=true;				
	} else {
		// continous is unchecked
		document.forms[0].end_day.disabled=false;
		document.forms[0].end_month.disabled=false;		
		document.forms[0].end_year.disabled=false;						
	}
}

function checkPostCode (toCheck) {
  // if its empty, its ok
  if (toCheck.length==0) {
		return true;  
  }
 		toCheck=toCheck.toUpperCase();
		for (i=0;i<arrValidPC.length;i++) {
			if (toCheck.indexOf(arrValidPC[i])>-1) {
				return true;
				break;
			}
		}
		alert("Invalid Postcode");
		return false;
}

function postPromotionDisable (bSetting) {
	fields=["vendor_address1","vendor_address2","vendor_town","vendor_postcode","vendor_postcode2"];	
	for (var i=0;i<fields.length;i++){
		ref=document.getElementById(fields[i]);
		if (bSetting) {
			ref.disabled=true;
			ref.value="";
			ref.style.backgroundColor='#cccccc';
		} else {
			ref.disabled=false;
			ref.style.backgroundColor='transparent';	
		}
	}	
}

// search form checking
function checkSearchForm() {
	if(checkPostCode(document.getElementById('search').postcode.value)) {
		document.getElementById('search').submit();
	}
	return false;
}

function checkSigninform() {
	// validate
	document.getElementById('signin').submit();
}

function checkforenter(functiontocall) {
	// set up from pages that contain a form.  Listens for every keydown, and if it is an enter key, performs the validation
	// function specified by functiontocall
	document.onkeydown = function(e) {
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if (code==13) {
			eval(functiontocall+"()");
		}
	}	
}
function LimitText(f, c, w) {
 	if (f.value.length > c) {
 		f.value = f.value.substring(0, c);
 	}
 	w.value = c - f.value.length;
}

/* CALENDAR FOR INDEX PAGE */
function navigateCalendar(month,year) {
        var url = "index_calendar_get.php?month="+month+"&year="+year;
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callbackCalendar;
        req.send(null);
}

function callbackCalendar() {
        if(req.readyState == 4) {
                if(req.status == 200) {
                        response = req.responseText;
                        document.getElementById("calendar").innerHTML = response;
                } else {
                        alert("There was a problem retrieving the data:\n" + req.statusText);
                }
        }
}

function productPopup(iProductId) {
	var refWindow=window.open("promotion_detail_product_image.php?id="+iProductId,"Product","left=30,top=30,resizable=1,scrollbars=no,status=no,width=1,height=1");
}

function productPopupresizeWinTo( idOfDiv ) {
			  var oH = document.getElementById(idOfDiv);
			  var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
			  var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
			  var x = window; 
			  x.resizeTo( oW + 200, oH + 200 );	 
			  var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
			  if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
			  else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
			  else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
			  if( window.opera && !document.childNodes ) { myW += 16; }
			  // add some extra padding
			  oW+=10;
			  oH+=10;
			  x.resizeTo( oW + ( ( oW + 200 ) - myW ), oH + ( (oH + 200 ) - myH ) );
			  x.focus();
}

