///////////////////////////////////////////////////////////
//This is a JavaScript include file
//
//Using this file in your HTML Pages
//
// <html>
// <head>
// <title>Untitled Document</title>
//   <script LANGUAGE="JavaScript1.1" SRC="global.js"></script>
//   <script LANGUAGE="JavaScript1.1">
//   <!--
//     //Lets say you have a form called "myform" with edit box called "surname" and
//     //you want to check if it is not empty.
//
//     function checkinput()
//     {
//       if (!isEmpty(document.myform.surname.value))
//         {
//           alert('Full')
//         } else
//         {
//           alert('Empty')
//         }
//     }
//   //-->
//   </script>
// </head>
// <body bgcolor="#FFFFFF">
// <form method="post" action="" name="myform">
//   <input type="text" name="surname" onchange="checkinput()">
//   <input type="text" name="age">
// </form>
// </body>
// </html>


///////////////////////////////////////////////////////////////////////////////////
//The functions
//////////////////////////////////////////////////////////////////////////////////
//isEmpty(s)   	    		Returns True if s is empty
//isWhitespace(s)  	 		Returns True if string s is empty or whitespace characters only
//isEmail(s [,canBeEmpty])	Returns True if valid Email
//getNameValue(thestring)  Retruns a array with edit object name and value
//fillTheForm()   			This function fills two drop down boxes
//updateForm()   				This function fills two drop down boxes
//CCU(the_object,max) This function displays the amount of characters left in the status bar
//										CCU == Count Characters Used   :)
//getProperties(obj,sep)  Get all the properties of a object and seperate them using the sep value
//getRadioValue(radioObject) Get the selected value of a group of radion buttons

///////////////////////////////////////////////////////////////////////////////
//Global vars
//////////////////////////////////////////////////////////////////////////////

// whitespace characters
var whitespace = " \t\n\r";
//All the ilegal characters in a email adress i think!

var ilegal = "!\"#$%&'()*+,/:;<=>?[\\]^`{|}~";
//var ilegal = "!\"#$%&'()*+,/:;<=>?[\\]^`{|}~  "
var g_strFromDB = "";
// Check whether string s is empty.
function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
function isEmail (s,canBeEmpty)
{   if (isEmpty(s))
       if (!canBeEmpty) return false;
       else return true;

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
   while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    //look for a dubble @
	 if (s.indexOf("@") != -1)
	 {
		if (s.indexOf("@",s.indexOf("@")+1) != -1)
		{
			return false;
		}
	 }
	 // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function test_path()
{
alert("it works!");
}

// checks for ilegal chars
function isLegal (s)
{   var i;

	//Well if the string is not a valid email adress then  add the @ sign
	if (isEmail(s)){ilegal1 = ilegal}else{ilegal1 = ilegal + "@"}
    // Search through string's characters one by one
    // until we find a ilegal character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (ilegal1.indexOf(c) != -1) return false;
    }

    // All characters are whitespace.
	 return true;
}

function getNameValue(thestring)
{
/*
This function returns a array where the number and text is stored in
ex: The result will be 888
var myString = "888||My category" ;
alert(getNameValue(myString)[0]);
*/
var intPipeAt = 0
var returnValue = new Array("","")
	if (thestring != null)
	{
		intPipeAt = thestring.indexOf("||")
		returnValue[0] = thestring.substring(0,intPipeAt)
		returnValue[1] = thestring.substring(intPipeAt+2,thestring.length)
	}
	return returnValue
}


function updateForm()
{
/*
This function needs to have 2 objects called
	obj_sectionlist	:A drop down list
	obj_catlist			:A drop down list
and a array that holds the sections and categories (see catsec.js)
call this function when your section drop down box changes
*/
	if ((obj_sectionlist)&&(obj_catlist))
	{
		var intCurrentSection =  obj_sectionlist.selectedIndex;
		var intI
		var newCategorie = new Option("Not Selected");
		var maxlenght = new Array(obj_sectionlist.length,obj_catlist.length)

//		alert(intCurrentSection);
		if (intCurrentSection < 0)
		{
		intCurrentSection = 1;
//		alert(intCurrentSection < 0);
		}
			//empty the categorie selection box
			for(intI=0; intI<maxlenght[1]; intI++)
			{
				obj_catlist.options[0]=null;
			}

			obj_catlist.options[0]=newCategorie
			obj_catlist.options[0].value = "*"

		   //if the user selected the Not Selected then we just do nothing!
			if (obj_sectionlist.selectedIndex == 0) return;

			//fill our categorie selection list!
			if (arrayCat[intCurrentSection-1].length > 1)
			{
				for (intI=1;intI<arrayCat[intCurrentSection-1].length;intI++)
				{
					if (getNameValue(arrayCat[intCurrentSection-1][intI])[1] != "")
					{
						var newCategorie = new Option(getNameValue(arrayCat[intCurrentSection-1][intI])[1]);
						obj_catlist.options[intI]=newCategorie;
						obj_catlist.options[intI].value = getNameValue(arrayCat[intCurrentSection-1][intI])[0];
					}//if (get
				}//for
				obj_catlist.selectedIndex = 0;
			}//if (array
	}
	else
	{
		alert("updateForm();\n Error!\n Please contact Job Mail website support to report this error.");
		return false;
	}
}

function fillTheForm()
{
/*
This function needs to have 2 objects called
	obj_sectionlist	:A drop down list
	obj_catlist			:A drop down list
and a array that holds the sections and categories (see catsec.js)
call this function on your onload!
*/
	if ((obj_sectionlist)&&(obj_catlist))
	{
		var intI = 0
		var intJ = 0
		var intOldValues = new Array(obj_sectionlist.selectedIndex,obj_catlist.selectedIndex)
		var maxlenght = new Array(obj_sectionlist.length,obj_catlist.length)

			//If the global var with the name g_strFromDB has been created
			//g_strFromDB you must create and give some value!
			if (g_strFromDB != "")
			{
				/*
				Why this code?
				Ok, you see the guy comes in with categorie id that is stored
				with his data and I try to find that categorie in our array list
				if so change the a array called intOldValues witch I use to
				restore his previos selected section and categorie
				*/
				for (intI=0;intI<arrayCat.length;intI++)
				{
					if (arrayCat[intI][0] != "")
					{
						for (intJ=0;intJ<arrayCat[intI].length;intJ++)
						{
							if (arrayCat[intI][intJ] != "")
							{
								if (getNameValue(arrayCat[intI][intJ])[0] == g_strFromDB)
								{
									intOldValues[0] = intI+1;
									intOldValues[1] = intJ;
								}
							}
						}
					}
				}
			}

			//clean the section and categories selection boxes!
			for(intI=0; intI<maxlenght[0]; intI++)
			{
				obj_sectionlist.options[0]=null;
			}

			for(intI=0; intI<maxlenght[1]; intI++)
			{
				obj_catlist.options[0]=null;
			}

			//Add the fisrt item the selection boxes
			var newSection = new Option("Not Selected");
			obj_sectionlist.options[0]=newSection
			obj_sectionlist.options[0].value = "*"
			var newCategorie = new Option("Not Selected");
			obj_catlist.options[0]=newCategorie
			obj_catlist.options[0].value = "*"

			//Add all the sections!
			for (intI=0;intI<arrayCat.length;intI++)
			{
				if (arrayCat[intI][0] != "")
				{
					var newSection = new Option(getNameValue(arrayCat[intI][0])[1]);
					obj_sectionlist.options[intI+1]=newSection;
					obj_sectionlist.options[intI+1].value=getNameValue(arrayCat[intI][0])[0]
				}
			}

			//Looking at this you might think i am "(_. () |)"  or something! :)
			//I have solved the IE BUG !!!
			if (intOldValues[0] != 0){obj_sectionlist.selectedIndex = intOldValues[0]}else{obj_sectionlist.selectedIndex = 0}
			updateForm();
			if (intOldValues[1] != 0){obj_catlist.selectedIndex = intOldValues[1]}else{obj_catlist.selectedIndex = 0}
	}
	else
	{
		alert("fillTheForm();\n Error!\n Please contact Job Mail website support to report this error.");
		return false;
	}

}

//CCU = Count Characters Used   :)
function CCU(the_object,max)
{
var intUsed = 0
	if (the_object != null)
	{
		intUsed = max - the_object.value.length
		if (intUsed > -1)
		{
			window.status = intUsed + ' character(s) left'
		}
		else
		{
			window.status = 'Exceeding maximum length with '+  Math.abs(intUsed) + ' character(s)'
		}
	}
}

function getProperties(obj,sep) {
    var properties = "";
    for (var propName in obj) {
   properties +=propName+"="+obj[propName]+sep;
    }
    return properties;
}

function getRadioValue(radioObject)
{
	var value = null
	for (var i=0;i < radioObject.length; i++)
	{
		if (radioObject[i].checked)
		{
			value = radioObject[i].value;
			break;
		}
	}
	return value;
}

function write_img_src(IMG_SRC)
{
   random = Math.round(Math.random() * 100000000);
   document.write("<IMG SRC=\"" + IMG_SRC + random + "\" BORDER=\"0\">");
}

function readCookie(Name)
{
	var cookies = ' ' + document.cookie;
	if (cookies.indexOf(' ' + Name + '=') == -1) return null;
	var start = cookies.indexOf(' ' + Name + '=') + (Name.length + 2);
	var finish = cookies.substring(start,cookies.length);
	finish = (finish.indexOf(';') == -1) ? cookies.length : start + finish.indexOf(';');
	return unescape(cookies.substring(start,finish));
}





function toggleSaveAds(adid, imgid){
	// Set the cookie's name
	cookieName = 'junkmail_ads';

	// Read the cookie
	cookieAds = "" + readCookie(cookieName)
	if (cookieAds == 'null'){
		// If empty, then add a fake record to it
		cookieStr  = cookieName + "=0; EXPIRES=Thu, 01-Jan-2099 00:00:00 GMT; "
		document.cookie = cookieStr;
	}
	cookieAds = "" + readCookie(cookieName)
	if (cookieAds == 'null'){
		// If it is not being read, then send error message
		alert("Your browser does not support cookies, or you have turned them off!")
	}
	else{
		// Read cookie into an array
		tmpArr = cookieAds.split( "," )

		// See if the ad already exists
		foundFlag = 0
		for (k=0; k<tmpArr.length; k++){
			if (1*adid == 1*tmpArr[k]){
				foundFlag = 1
				break;
			}
		}
		if (foundFlag == 1){
			// Delete from basket
			newArr = new Array();
			counter = 0;
			for (k=0; k < tmpArr.length; k++){
				if (1*tmpArr[k] != 1*adid){
					newArr[counter] = tmpArr[k]
					counter++;
				}
			}
			tmpArr = new Array()
			tmpArr = newArr
			eval("document."+imgid+".src = (savead) ")
		}
		else{
			// Add to basket
			tmpArr[tmpArr.length] = adid
			eval("document."+imgid+".src = (delad) ")
		}
		tmpStr = tmpArr.join( ',' )
		cookieStr = cookieName + "="+tmpStr+"; EXPIRES=Thu, 01-Jan-2099 00:00:00 GMT; "
		document.cookie = cookieStr
	}
}
function clearBasket(theUrl){

	document.cookie = "junkmail_ads=; EXPIRES=Thu, 01-Jan-2099 00:00:00 GMT; "
	window.open(theUrl, '_top')
}

function changeBasketImages(){
	// Set the cookie's name
	cookieName = 'junkmail_ads';

	// Read the cookie
	cookieAds = "" + readCookie(cookieName)
	if (cookieAds == 'null'){
		// If empty, then add a fake record to it
		cookieStr  = cookieName + "=0; EXPIRES=Thu, 01-Jan-2099 00:00:00 GMT; "
		document.cookie = cookieStr;
	}
	cookieAds = "" + readCookie(cookieName)
	if (cookieAds != 'null'){
		// Read cookie into an array
		tmpArr = cookieAds.split( "," )
		if (arguments.length != 0){
			for (k=0; k < tmpArr.length; k++){
				if (1*tmpArr[k] == arguments[0]){
					img = "img_"+tmpArr[k]
					eval("if (document."+img+"){document."+img+".src = (delad)} ")
				}
			}
		}
		else{
			for (k=0; k < tmpArr.length; k++){
				if (1*tmpArr[k] != 0){
					img = "img_"+tmpArr[k]
					eval("if (document."+img+"){document."+img+".src = (delad)} ")
				}
			}
		}
	}
}

function show_banner(bannerStr){
	random = Math.round(Math.random() * 100000000)
	bannerStr+="&cb="+random+"&w=468&h=60"
	if (!document.layers){
		document.write("<NOLAYER>")
		document.write("	<IFRAME VSPACE=0 HSPACE=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO FRAMEBORDER=0 WIDTH=468 HEIGHT=60 SRC=\"http://www.lovemail.co.za/ad/oasisi-i.php?n="+bannerStr+"\"");
		document.write("		<A HREF=\"http://www.lovemail.co.za/ad/oasisc.php?n="+bannerStr+"&w=468&h=60\">")
		document.write("			<IMG SRC=\"http://www.lovemail.co.za/ad/oasisi.php?n="+bannerStr+"&w=468&h=60\" BORDER=0 WIDTH=468 HEIGHT=60>")
		document.write("		</A>")
		document.write("	</IFRAME>")
		document.write("</NOLAYER>")
	}
	else{
		document.write("<A HREF=\"http://www.lovemail.co.za/ad/oasisc.php?n="+bannerStr+"&w=468&h=60\" target=\"_blank\">")
		document.write("<IMG SRC=\"http://www.lovemail.co.za/ad/oasisi.php?n="+bannerStr+"&w=468&h=60\" WIDTH=468 HEIGHT=60 BORDER=0></A>")
	}
}

/*
function show_button(bannerStr, width, height){
	random = Math.round(Math.random() * 100000000)
	bannerStr+="&cb="+random+"&w="+width+"&h="+height
	if (!document.layers){
		document.write("<NOLAYER>")
		document.write("	<IFRAME VSPACE=0 HSPACE=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO FRAMEBORDER=0 WIDTH="+width+" HEIGHT="+height+" SRC=\"http://www.auxion.co.za/ads/oasisi-i.php?n="+bannerStr+"\""); // SRC=\"http://www.auxion.co.za/ads/oasisi-i.php?"+encodedBannerStr+"&w="+width+"&h="+height+"\"");//+"%22%20WIDTH%3D"+width+"%20HEIGHT%3D"+height+"%20FRAMEBORDER%3D%22no%22%20BORDER%3D0%20MARGINWIDTH%3D0%20MARGINHEIGHT%3D0%20SCROLLING%3D%22no%22%3E%0D%0A%3CA%20HREF%3D%22http%3A//www.auxion.co.za/ads/oasisc.php%3Fn%3D"+encodedBannerStr+"%26w%3D"+width+"%26h%3D"+height+"%22%3E%0D%0A%3CIMG%20SRC%3D%22http%3A//www.auxion.co.za/ads/oasisi.php%3Fn%3D"+encodedBannerStr+"%26w%3D"+width+"%26h%3D"+height+"%22%20BORDER%3D0%20WIDTH%3D"+width+"%20HEIGHT%3D"+height+"%3E%0D%0A%3C/A%3E%0D%0A%3C/IFRAME%3E%0D%0A%3C/NOLAYER%3E%0D%0A%3CILAYER%20ID%3D%22layer1%22%20VISIBILITY%3D%22hidden%22%20WIDTH%3D"+width+"%20HEIGHT%3D"+height+"%3E%3C/ILAYER%3E\">")
		document.write("		<A HREF=\"http://www.auxion.co.za/ads/oasisc.php?n="+bannerStr+"&w="+width+"&h="+height+"\">")
		document.write("			<IMG SRC=\"http://www.auxion.co.za/ads/oasisi.php?n="+bannerStr+"&w="+width+"&h="+height+"\" BORDER=0 WIDTH="+width+" HEIGHT="+height+">")
		document.write("		</A>")
		document.write("	</IFRAME>")
		document.write("</NOLAYER>")
	}
	else{
		document.write("<A HREF=\"http://www.auxion.co.za/ads/oasisc.php?n="+bannerStr+"&w="+width+"&h="+height+"\" target=\"_blank\">")
		document.write("<IMG SRC=\"http://www.auxion.co.za/ads/oasisi.php?n="+bannerStr+"&w="+width+"&h="+height+"\" WIDTH="+width+" HEIGHT="+height+" BORDER=0></A>")
	}
}

*/

function show_button(bannerStr, width, height){
        random = Math.round(Math.random() * 100000000)
        bannerStr+="&cb="+random+"&w="+width+"&h="+height
        if (!document.layers){
                document.write("<NOLAYER>")
                document.write("        <IFRAME VSPACE=0 HSPACE=0 MARGINWIDTH=0 MARGINHEIGHT=0 SCROLLING=NO FRAMEBORDER=0 WIDTH="+width+" HEIGHT="+height+" SRC=\"http://www.lovemail.co.za/ad/oasisi-i.php?n="+bannerStr+"\"");//th+"%20HEIGHT%3D"+height+"%3E%3C/ILAYER%3E\">"
                document.write("                <A HREF=\"http://www.lovemail.co.za/ad/oasisc.php?n="+bannerStr+"&w="+width+"&h="+height+"\">")
                document.write("                        <IMG SRC=\"http://www.lovemail.co.za/ad/oasisi.php?n="+bannerStr+"&w="+width+"&h="+height+"\" BORDER=0 WIDTH="+width+" HEIGHT="+height+">")
                document.write("                </A>")
                document.write("        </IFRAME>")
                document.write("</NOLAYER>")
        }
        else{
                document.write("<A HREF=\"http://www.lovemail.co.za/ad/oasisc.php?n="+bannerStr+"&w="+width+"&h="+height+"\" target=\"_blank\">")
                document.write("<IMG SRC=\"http://www.lovemail.co.za/ad/oasisi.php?n="+bannerStr+"&w="+width+"&h="+height+"\" WIDTH="+width+" HEIGHT="+height+" BORDER=0></A>")
        }
}
