

var authorId = "A5FA056E-C95D-4357-91B3-6D353C15948F";
var pageOrientation = "0";
var topMargin = "0.5";
var bottomMargin = "0.5";
var leftMargin = "0.5";
var rightMargin = "0.5";
function savePageAsPDF()
{
	var sUriRequest = "";

	sUriRequest = "author_id=" + authorId;
	sUriRequest += "&page=" + pageOrientation;
	sUriRequest += "&top=" + topMargin;
	sUriRequest += "&bottom=" + bottomMargin;
	sUriRequest += "&left=" + leftMargin;
	sUriRequest += "&right=" + rightMargin;

	var pURL = "http://savepageaspdf.pdfonline.com/pdfonline/pdfonline.asp?cURL=" + escape(document.location.href) + "&" + sUriRequest;
	window.open(pURL, "PDFOnline", "scrollbars=yes,resizable=yes,width=640,height=480,menubar,toolbar,location");
}

function saveAdAsPDF(uri)
{
	var sUriRequest = "";

	sUriRequest = "author_id=" + authorId;
	sUriRequest += "&page=" + pageOrientation;
	sUriRequest += "&top=" + topMargin;
	sUriRequest += "&bottom=" + bottomMargin;
	sUriRequest += "&left=" + leftMargin;
	sUriRequest += "&right=" + rightMargin;
	var pURL = "http://savepageaspdf.pdfonline.com/pdfonline/pdfonline.asp?cURL=" + escape(uri) + "&" + sUriRequest;
	window.open(pURL, "PDFOnline", "scrollbars=no,resizable=no,width=640,height=250");
}


function showlayer(layer){
var myLayer = document.getElementById(layer);
if(myLayer.style.display=="none" || myLayer.style.display==""){
myLayer.style.display="block";
} else {
myLayer.style.display="none";
}
}

function getAbuseEmail()
{
	/***********************************************
	* Encrypt Email script- Please keep notice intact
	* Tool URL: http://www.dynamicdrive.com/emailriddler/
	* **********************************************/
	var emailriddlerarray=[97,98,117,115,101,64,98,97,114,103,97,105,110,99,105,116,121,46,99,97]
	var encryptedemail_id72='' //variable to contain encrypted email 
	for (var i=0; i<emailriddlerarray.length; i++)
		encryptedemail_id72+=String.fromCharCode(emailriddlerarray[i])
	// return encryptedemail_id72;
	
	return '<a href="mailto:'+encryptedemail_id72+'">'+encryptedemail_id72+'</a>'
	
}


 // Textbox accepting integers only with javascript
 // Usage: <input type="text" onChange="intOnly(this);" onKeyUp="intOnly(this);" onKeyPress="intOnly(this);">
function intOnly(i) {
	if(i.value.length>0) {
		i.value = i.value.replace(/[^\d]+/g, ''); 
	}
}

function priceOnly(i){
	if(i.value.length>0) {
		i.value = i.value.replace(/[^\d*(\.\d{1,2})]+/g, ''); 
	}

}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : evt.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

<!-- This script repurposed from http://javascript.internet.com -->

<!-- Begin
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
//  End -->


// expandCollapseTerms and expandCollapsePrivacy are used on the Compose Ad and Registration pages
function expandCollapseTerms() {
	whichelement = document.getElementById('terms'); 
    

   if (whichelement.className=="shown") { 
      whichelement.className="hidden"; 
   } 
   else { 
      otherelement = document.getElementById('privacy');
      otherelement.className = 'hidden';
      whichelement.className="shown"; 
   } 

}	
function expandCollapsePrivacy() {
	whichelement = document.getElementById('privacy'); 
   
   if (whichelement.className=="shown") { 
      whichelement.className="hidden"; 
   } 
   else {
   	  otherelement = document.getElementById('terms');  
      otherelement.className="hidden"; 
      whichelement.className="shown"; 
   } 
}	


function HideContent(d) {
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
	if(document.getElementById(d).style.display == "none") { 
		document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}


function wopen(url, name, w, h) {
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=yes, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}

function checkEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true 					
}

function showAdvertiseHere(width, height, vertical){
	var output = "";
	output += getStyles("advertiseHere",width,height,vertical); 	
	output += '<a href="/info/advertise.php" title="Advertise Here" class="adhere">Advertise Here</a>';
	document.write("<div class=\"advertiseHere\">" + output + "</div>");
}

function getStyles(div,width, height, vertical)
{
	var styleCode = "";
	var newWidth = width - 2;
	var newHeight = height - 2;
	div = '.' + div;
	
	if(vertical == false)
	{
		var blockWidth = "100%";
	} else {
		var blockWidth = width + "px";
	}
	styleCode += "<style type=text/css>";
	styleCode += div + " {width:" + blockWidth + ";display:block;}";
	styleCode += div + " a{width:" + width + "px;}";
	styleCode += div + " a img{padding:0;}";
	styleCode += div + " a em{font-style:normal;}";
	
	if(vertical == true)
	{
		styleCode += div + " a{margin-right:0;}";
	}
	if(width < "100")
	{
		styleCode += div + " a em{display:block;text-indent:-9000px;}" + div + " a{height:" + height + ";line-height:0;} " + div + " a.adhere{font-size:0;}";
	}
	styleCode += div + " a.adhere{width:" + width + "px;height:" + height + "px;line-height:" + height*8 + "%;}";
	styleCode += " html>body " + div + " a.adhere{width:" + newWidth + "px;height:" + newHeight + "px;} " + div + " img.s{height:0;width:0;}</style>";
	return styleCode;
} 

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
/*
	Checks login form
*/
function checkLoginForm(f) {
	if (f.email.value == "" || !checkEmail(f.email.value)) {
		alert("Please check your email address.");
		f.email.focus();
		return false;
	} else if (f.password.value == "") {
		alert("Please check your password.");
		f.password.focus();
		return false;
	} 
}

function checkContactForm(f)
{
	var errorFlag = 0;
	
	if ( f.subject.selectedIndex == 0 ) {
        errorFlag = 1;
    }
	
	if ( f.message.value == "" ) {
        errorFlag = 1;
    }
	var i;
	for (i = 0; i < f.elements.length; i++) {
		if (f.elements[i].className == "required") {
			if (f.elements[i].type == "text") {
				if (f.elements[i].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	// Check email fields
	if( !checkEmail(f.email.value) ) {
		errorFlag = 1;
	}
	
	if (errorFlag == 1) {
		alert("Your email address is invalid!\n\nyourname@domain.com");
		f.email.focus();
		return false;
	}
	
}

function checkFeedbackForm(f)
{
	var errorFlag = 0;
	
	if ( f.os.selectedIndex == 0 ) {
        errorFlag = 1;
    }
	
	if ( f.browser.selectedIndex == 0 ) {
        errorFlag = 1;
    }
	
	if ( f.comments.value == "" ) {
        errorFlag = 1;
    }
	
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	// Check email fields
	if( !checkEmail(f.email.value) ) {
		errorFlag = 1;
	}
	
	if (errorFlag == 1) {
		alert("Your email address is invalid!\n\nyourname@domain.com");
		f.email.focus();
		return false;
	}
	
}

function updateStage(src, thumb) {
	document.preview.src='http://images.bargaincity.ca/uploads/'+src; 
	updateThumbnails(thumb);	
}
function updateThumbnails(num) {

	for(i=0;i<6;i++) {
		thumbnail = 'thumbnail' + i;
		if(document.getElementById(thumbnail) != null) {
			if(i == num) {
				document.getElementById(thumbnail).className = 'image-selected';

			} else {
				document.getElementById(thumbnail).className = 'image';
			}
		}			
	}
}






function checkUsername(str)
{
	if (str.length < 4) { 
	  document.getElementById("checkUsername").innerHTML="<span style='color:#CC0000'>That username is too short.</span>";
	  return;
	}
	if (str.length > 25) { 
	  document.getElementById("checkUsername").innerHTML="<span style='color:#CC0000'>That username is too long.</span>";
	  return;
	}	
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/js/username.js.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	document.getElementById("checkUsername").innerHTML="<img src=\"http://images.bargaincity.ca/loading3.gif\" />";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4) { 
	document.getElementById("checkUsername").innerHTML=xmlHttp.responseText;
	}
}

function updateUsernameStatus() {
	var username = document.joinform.username.value;
	if (username != "") {
		checkUsername(username)
	}
	else{
		clearUsernameStatus();
	}
}



function clearUsernameStatus() {
	document.getElementById("checkUsername").innerHTML = '';
}

function checkForm(f)
{
	var errorFlag = 0;
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text" || f.elements[x].type == "password") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	var illegalChars = /\W/;
	if (f.username.value.length < 4) {
		alert("The username you entered is too short.");
    	return false;
	} else if (f.username.value.length > 25) {
    	alert("The username you entered is too long.");
    	return false;
	} else if (illegalChars.test(f.username.value)) {
       alert("The username you entered is invalid.");
       return false;
    } 
    

    
 	
	if( !checkEmail(f.email.value) ) {
		alert("The email address you entered is invalid.");
		f.email.focus();
		return false;
	}

    if ( f.email.value != f.email2.value ) {
       alert("Your emails do not match.");
       return false;
    }
	
    if (f.password1.value.length < 6) {
       alert("The password you entered is too short");
       return false;
    } else if (f.password1.value.length > 25) {
       alert("The password you entered is too long");
       return false;
    }
    
    if ( f.password1.value != f.password2.value ) {
       alert("Your passwords do not match.");
       return false;
    } 
}

// Compose page ---------
function loaderOff(){document.getElementById("loaderContainer").style.display = "none";}
function loaderOn() {document.getElementById("loaderContainer").style.display = "";}
function submitComposeForm(form)
{
	form.process.value='1'
	form.submit();
}

function removeUpload(form, index)
{
	setcoords(form);
	form.remove.value = 1;
	form.index.value = index;
	form.submit();
}
function addUpload(form)
{
	setcoords(form);
	form.image.value=1;
	loaderOn();
	form.submit();	
}
function handleAcronym(form)
{	
	if(form.seeker.value != '' && form.sought.value != '') {
		document.getElementById('acronym').innerHTML= '<b> - '+form.seeker.value+'4'+form.sought.value+'</b> &nbsp;';	
	} else {
		document.getElementById('acronym').innerHTML='';
	}
}

function checkManageEmailForm(f)
{
	var errorFlag = 0;
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	if( !checkEmail(f.email.value) ) {
		alert("Your email address is invalid!");
		return false;
	}
}

function checkPasswordForm(f)
{
	var errorFlag = 0;
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "password") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	var illegalChars = /[\W_]/; // allow only letters and numbers
    if ((f.password2.value.length < 6) || (f.password2.value.length > 25)) {
       alert("New password is the wrong length.");
       f.password2.focus();
       return false;
    } else if (illegalChars.test(f.password2.value)) {
      alert("New password contains illegal characters.");
      f.password2.focus();
      return false;
    }
    
    if ( f.password2.value != f.password3.value ) {
       alert("Passwords do not match.");
       f.password2.focus();
       return false;
    } 
	
	if ( f.password1.value == f.password2.value ) {
       alert("New password matches current one.");
       f.password2.focus();
       return false;
    } 
	
}

function checkRecoveryForm(f)
{
	var errorFlag = 0;
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	if( !checkEmail(f.email.value) ) {
		alert("Your email address is invalid!");
		return false;
	}
}

function checkAdvancedSearchForm(f) {
	var errorFlag = 0;
		
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}
	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
}

function checkConfirmationForm(f)
{
	var errorFlag = 0;
		
	
	for (x = 0; x < f.elements.length; x++) {
		if (f.elements[x].className == "required") {
			if (f.elements[x].type == "text") {
				if (f.elements[x].value == "") {
					errorFlag = 1;
				}
			}
		}
	}

	
	if (errorFlag == 1) {
		alert("You must fill out all required fields before you can submit the form!");
		return false;
	}
	
	// Check email fields
	if( !checkEmail(f.email.value) ) {
		errorFlag = 1;
	}
	
	if (errorFlag == 1) {
		alert("Your email address is invalid!");
		f.email.focus();
		return false;
	}
	
}

function ScrollIt(form){
    window.scrollTo(form.PageX.value, form.PageY.value);
}
function setcoords(form){
    var myPageX;
    var myPageY;
    if (document.all){
        myPageX = document.body.scrollLeft;
        myPageY = document.body.scrollTop;
        }
    else{
        myPageX = window.pageXOffset;
        myPageY = window.pageYOffset;
        }
    form.PageX.value = myPageX;
    form.PageY.value = myPageY;
}

function bookmark(ad)
{
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	var url="bookmark.php?item="+ad+"&sid="+Math.random();
	document.getElementById("saveIcon"+ad).src = 'http://images.bargaincity.ca/starring.gif';
	xmlHttp.onreadystatechange=function() { 
		if (xmlHttp.readyState==4) { 			
			if(xmlHttp.responseText == 'saved') {
				document.getElementById("saveIcon"+ad).src = 'http://images.bargaincity.ca/starred.png';
			} else {
				document.getElementById("saveIcon"+ad).src = 'http://images.bargaincity.ca/unstarred.png';
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

