function isRequiredfield(reqfield,txtmessage)
{
    if (document.getElementById(reqfield).value=="")
    {
        alert(txtmessage);
        return false;
    }
    
    return true;
}
function stristr( haystack, needle, bool ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '

    var pos = 0;

    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function isValidEmail(email, required) {
    var email = document.getElementById(email).value;

    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
	    alert("Please supply a valid Email address.");
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
	    alert("Please supply a valid Email address.");
            return false;
        }
        return true;
    }

    if (! allValidChars(email)) {  // check to make sure all characters are valid
	alert("Please supply a valid Email address.");
        return false;
    }

    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        alert("Please supply a valid Email address.");
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        alert("Please supply a valid Email address.");
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        alert("Please supply a valid Email address.");
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
        alert("Please supply a valid Email address.");
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
        alert("Please supply a valid Email address.");
	return false;
    }

    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}


function changechild(parent_name,child_name, max_id)
{
	cat_id = document.getElementById(parent_name).value;

        //de-activate all dropdowns
	for (i=0;i<=max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
	       document.getElementById(child_name+i).style.display = "none";
	       
        }
	}
        //activate the selected category's sub-category list
	document.getElementById(child_name+cat_id).style.display = "";

	return 0;
}
function changechild_search(parent_name,child_name, max_id)
{
	cat_id = document.getElementById(parent_name).value;

        //de-activate all dropdowns
	for (i=0;i<=max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
	       document.getElementById(child_name+i).style.display = "none";

        }
	}

        //activate the selected category's sub-category list
    if (document.getElementById(child_name+cat_id))
    {
    	document.getElementById(child_name+cat_id).style.display = "";
    }
    else
    {
    	document.getElementById(child_name+"0").style.display = "";
    }


	return 0;
}
function changechild_disable(parent_name,child_name, max_id)
{
	cat_id = document.getElementById(parent_name).value;

    //de-activate all dropdowns
	for (i=0;i<=max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
	       document.getElementById(child_name+i).disabled = true;
	       document.getElementById(child_name+i).style.display = "none";
        }
	}

    //activate the selected category's sub-category list
    if (document.getElementById(child_name+cat_id))
    {
    	//if (cat_id>0)
        //    {
                document.getElementById(child_name+cat_id).disabled = false;
         //   }
    	document.getElementById(child_name+cat_id).style.display = "";
	}
	return 0;
}
function check_children(parent_name,child_name)
{
    cat_id = document.getElementById(parent_name).value;
    if (document.getElementById(child_name+cat_id))
    {
        return true;
    }
    else
    {
        return false;
    }
}
function custom_dodropdownwithchildren(parent_name,child_name, max_id,alertmessage)
{
    changechild_disable(parent_name,child_name, max_id);
    myvar2 = check_children(parent_name,child_name);
    if (!myvar2)
    {
        alert(alertmessage);
    }
    return 0;
}

function changeparent(parent_name, child_name, grand_child_name, parent_max_id, child_max_id)
{
	parent_id = document.getElementById(parent_name).value;
	
    //de-activate all dropdowns
	for (i=0;i<=parent_max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
            document.getElementById(child_name+i).style.display = "none";
          	for (k=0;k<=child_max_id;k++)
            {
                if (document.getElementById(grand_child_name+k))
                {
                    document.getElementById(grand_child_name+k).style.display = "none";
                }
            }
        }
	}

        //activate the selected category's sub-category list
	document.getElementById(child_name+parent_id).style.display = "";

	return 0;
}
function changeparent_search(parent_name, child_name, grand_child_name, parent_max_id, child_max_id)
{
	parent_id = document.getElementById(parent_name).value;

    //de-activate all dropdowns
	for (i=0;i<=parent_max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
            document.getElementById(child_name+i).style.display = "none";
          	for (k=0;k<=child_max_id;k++)
            {
                if (document.getElementById(grand_child_name+k))
                {
                    document.getElementById(grand_child_name+k).style.display = "none";
                }
            }
        }
	}

    //activate the selected category's sub-category list
   if (document.getElementById(child_name+parent_id))
   {
    document.getElementById(child_name+parent_id).style.display = "";
   }
   else
   {
    document.getElementById(child_name+"0").style.display = "";
   }
   document.getElementById(grand_child_name+"0").style.display = "";

	return 0;
}

function changeparent_disable(parent_name, child_name, grand_child_name, parent_max_id, child_max_id)
{
	parent_id = document.getElementById(parent_name).value;
    //de-activate all dropdowns
	for (i=0;i<=parent_max_id;i++)
	{
        if (document.getElementById(child_name+i))
        {
            document.getElementById(child_name+i).style.display = "none";
            document.getElementById(child_name+i).disabled = true;
        }
	}
	
	for (k=0;k<=child_max_id;k++)
    {
        if (document.getElementById(grand_child_name+k))
        {
            document.getElementById(grand_child_name+k).style.display = "none";
            document.getElementById(grand_child_name+k).disabled = true;
        }
    }
    
    //activate the selected category's sub-category list
    document.getElementById(child_name+parent_id).style.display = "";
	if (parent_id>0) { document.getElementById(child_name+parent_id).disabled=false; }

	return 0;
}

function confirmDelete(delUrl)
{
	if (confirm("Are you sure you want to delete this item?"))
	{
		document.location = delUrl;
	}
}

function confirmDeleteWatch(delUrl)
{
	if (confirm("Are you sure you want to remove this item from your watchlist?"))
	{
		document.location = delUrl;
	}
}

function confirmDeleteMySupplier(delUrl)
{
	if (confirm("Are you sure you want to remove this Supplier from your watchlist?"))
	{
		document.location = delUrl;
	}
}

function confirmPassword(password,confpassword)
{
    var varpassword = document.getElementById(password).value;
    var varconfpassword = document.getElementById(confpassword).value;
    if (varpassword=="")
    {
        alert("Please provide a password.");
        return false;
    }
    if (varpassword == varconfpassword)
    {
        return true;
    }
    else
    {
        alert("Password and confirmation password does not match.");
        return false;
    }
}

function validateDropdowns_addlistingform(ind_max_id, cat_max_id)
{

//return false;

    catflag = false;
    //check if category is selected
	for (i=0;i<=ind_max_id;i++)
	{
        //check if category div exists
        if (document.getElementById("category"+i))
            {
                if (document.getElementById("category"+i).style.display == "")
                    {
                        var e = document.getElementById("category_id"+i); // select element
                        var strUser = e.options[e.selectedIndex].value;
                        if (strUser > 0)
                            {
                                catflag = true;
//                                alert("found: category"+i);
                            }
                    }

            }
	}
	
	//alert("here!");
    subcatflag = false;
    //check if sub-category is selected
	for (i=0;i<=cat_max_id;i++)
	{
        //check if category div exists
        if (document.getElementById("subcategory"+i))
            {
                if (document.getElementById("subcategory"+i).style.display == "")
                    {
                        var k = document.getElementById("subcategory_id"+i); // select element
                        var strUser2 = k.options[k.selectedIndex].value;
                        if (strUser2 > 0)
                            {
                                subcatflag = true;
//                                alert("found: sub-category"+i);
                            }
                    }

            }
	}


    if (catflag==false)
        {
            alert('please select a category.');
            return false;
        }
    if (subcatflag==false)
        {
            alert('please select a sub-category.');
            return false;
        }
    return true;
}

function custom_addlisting(maxind_id,maxcat_id)
{
    if (isRequiredfield('title','Please supply a title.')==false)
    {
        return false;
    }
    if (isRequiredfield('price','Please supply a price.')==false)
    {
        return false;
    }
    if (validateDropdowns_addlistingform(maxind_id,maxcat_id)==false)
    {
        return false;
    }
    if (isRequiredfield('startdate','Please supply a start date.')==false)
    {
        return false;
    }

    return true;
}


function custom_check_jpg(inputname)
{
  var ext = document.getElementById(inputname).value;
  ext = ext.substring(ext.length-3,ext.length);
  ext = ext.toLowerCase();
  if((ext == 'jpg') || (ext == 'gif'))
  {
    return true;
  }
//  if(ext == 'gif')
//  {
//    return true;
//  }
  alert('You selected a .'+ext+' file; please select a .jpg or .gif file instead.');
  return false;

}

function custom_highlightDIV(targetDIV,BGcolor)
{
    document.getElementById(targetDIV).style.background=BGcolor;
}

function custom_showDIV(targetDIV)
{
    if (document.getElementById(targetDIV))
    {
        if (document.getElementById(targetDIV).style.display=="none")
        {
            document.getElementById(targetDIV).style.display="";
        }
        else
        {
            document.getElementById(targetDIV).style.display="none";
        }
    }
    
    //for images dropdown (categories)
//    arrow_image_treeview_ind'.$row["id"]
    if (document.getElementById('arrow_open_'+targetDIV))
    {
        if(document.getElementById('arrow_open_'+targetDIV).style.display == "none")
        {
            document.getElementById('arrow_open_'+targetDIV).style.display = "";
            document.getElementById('arrow_close_'+targetDIV).style.display = "none";
        }
        else
        {
            document.getElementById('arrow_open_'+targetDIV).style.display = "none";
            document.getElementById('arrow_close_'+targetDIV).style.display = "";
        }
    }
}

function custom_hideDIV(targetDIV)
{
    document.getElementById(targetDIV).style.display="none";
}


/****************************************/
function custom_movedivtomouse(e,targetDIV){
    //if (!e) e = window.event;

    var IE = document.all?true:false;
    if (!IE) document.captureEvents(Event.MOUSEMOVE)
    //document.onmousemove = getMouseXY;
    var tempX = 0;
    var tempY = 0;
    
    if (IE)
    { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.documentElement.scrollTop;
    }
    else
    {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }

    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;}

    document.getElementById(targetDIV).style.left = tempX+"px";
    document.getElementById(targetDIV).style.top = tempY+"px";
    
//    document.getElementById('parentY').value = document.documentElement.scrollTop;
}
function custom_populatelistingtool(listingcount)
{
    document.getElementById('tooltip_image').src = document.getElementById('input_tooltip_image_'+listingcount).value;
    document.getElementById('tooltip_h3').innerHTML = document.getElementById('input_tooltip_h3_'+listingcount).value;
    document.getElementById('tooltip_vendor').innerHTML = document.getElementById('input_tooltip_vendor_'+listingcount).value;
    document.getElementById('tooltip_descr').innerHTML = document.getElementById('input_tooltip_descr_'+listingcount).innerHTML;
    //document.getElementById('tooltip_image').style.backgroundImage = "url('"+document.getElementById('input_tooltip_image_'+listingcount).value+"')";
}
function custom_clearistingtool(listingcount)
{
    document.getElementById('tooltip_image').src = 'listings/images_thumbs/default.jpg';
    document.getElementById('tooltip_h3').innerHTML = '';
    document.getElementById('tooltip_vendor').innerHTML = 'unknown';
    document.getElementById('tooltip_descr').innerHTML = 'Click on the image for more information.';
    //document.getElementById('tooltip_image').style.backgroundImage = "url('"+document.getElementById('input_tooltip_image_'+listingcount).value+"')";
}
/****************************************/
function custom_showDIVmove(targetDIV)
{
    document.getElementById(targetDIV).style.display = '';
}

function shownew()
{
    if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById('searchnew').style.visibility = 'visible';
    document.getElementById('searchused').style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4
        document.searchnew.visibility = 'visible';
        document.searchused.visibility = 'hidden';
        }
        else { // IE 4
        document.all.searchnew.style.visibility = 'visible';
        document.all.searchused.style.visibility = 'hidden';
        }
    }
}

function showused() {
    if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById('searchused').style.visibility = 'visible';
    document.getElementById('searchnew').style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4
        document.searchused.visibility = 'visible';
        document.searchnew.visibility = 'hidden';
        }
        else { // IE 4
        document.all.searchused.style.visibility = 'visible';
        document.all.searchnew.style.visibility = 'hidden';
        }
    }
}
function deny(ff)
{
    return false;
}
function custom_treeview_checkbox_cat(ind_id,catid)
{
    if (document.getElementById('treeview_cat_checkbox'+catid).checked)
    {
        if (document.getElementById('treeview_ind_checkbox'+ind_id).checked==false)
        {
            document.getElementById('treeview_ind_checkbox'+ind_id).click();
        }
    }

    return 0;
}
function custom_treeview_checkbox_sub_cat(ind_id,catid,subcatid)
{
    if (document.getElementById('treeview_sub_cat_checkbox'+subcatid).checked)
    {
        if (document.getElementById('treeview_cat_checkbox'+catid).checked==false)
        {
            document.getElementById('treeview_cat_checkbox'+catid).click();
        }
    }
    myvar = custom_treeview_checkbox_cat(ind_id,catid);
    return 0;
}
function custom_disable_elelment(triggerelement,targetelementid)
{
    if (document.getElementById(triggerelement).checked)
    {
        document.getElementById(targetelementid).style.background = "#efefef";
        document.getElementById(targetelementid).readOnly = true;
    }
    else
    {
        document.getElementById(targetelementid).style.background = "#ffffff";
        document.getElementById(targetelementid).readOnly = false;
    }
    return 0;
}
function custom_setvalue(targetelementid,newvalue)
{
 		newvalue = '';
        document.getElementById(targetelementid).value = newvalue;
}

function slideSwitch() {
    var $active = $('#banner_slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#banner_slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#banner_slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
     var $sibs  = $active.siblings();
     var rndNum = Math.floor(Math.random() * $sibs.length );
     var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function smallslideSwitch() {
    var $active = $('#smallbanner_slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#smallbanner_slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#smallbanner_slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
     var $sibs  = $active.siblings();
     var rndNum = Math.floor(Math.random() * $sibs.length );
     var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


	
$(function() {
	slideSwitch();
	smallslideSwitch();
    setInterval( "slideSwitch()", 10000 );
    setInterval( "smallslideSwitch()", 7000 );
});


jQuery(function() {
	//$("#keywords").suggest("webpages/ajax_search.php");
});	

// verifies only a number was typed into the form element
function numbersonly(e){
    var unicode=e.charCode? e.charCode : e.keyCode
    // if (unicode!=8||unicode!=9)
    if (unicode<8||unicode>9) {
        //if the key isn't the backspace key or tab key (which we should allow)
        if (unicode<48||unicode>57) //if not a number
        return false //disable key press
    }
}






