/**************************************************************************************
Author	:	Marcy
Date		:	July 2008
Version	:	1.0
Company	:	Laguna Sites      http://lagunasites.com
Description	:	Channelpromo.com
***************************************************************************************/
/*
  @param      cleanString cleans all nonalphanumeric characters and the space from a string
*/
function testing(){
alert ("this is a test"); 
}

function cleanString(strtoclean){
strtoclean = strtoclean.replace(/[^a-zA-Z0-9]+/g,'');  /*(/[^a-zA-Z 0-9]+/g,'')   add space back in */
return strtoclean;
}
/*
  @param      clearListBox removes all  options from listbox
*/
function clearListBox(listboxID) {  
var mylistbox = document.getElementById(listboxID);
  if(mylistbox == null) return 1;  
  while(mylistbox.length > 0)  { 
	mylistbox.remove(0);  
  }  
  return 1; 
}

/*
  @param      ProcessSelected
*/
function ProcessSelected(sptype,spcat,spsub,smanu){

  var chglistBox = document.getElementById("pcat"); 

	if(sptype != "") {

	   clearListBox("pcat");
	   clearListBox("psub");
	   clearListBox("manu");	
		sptype = cleanString(sptype);

		for(var x=0;x<window[sptype].length;x++){
			if ( spcat == window[sptype][x] ) {
				chglistBox[x+1] = new Option(window[sptype][x],window[sptype][x],true);
				chglistBox[x+1].selected = true;
			} else {
				chglistBox[x+1] = new Option(window[sptype][x],window[sptype][x]);
			}
		}	

	  listBox = document.getElementById("pcat");
	  chglistBox = document.getElementById("psub"); 
		if(spcat != "") {
			spcat = escape(spcat);			
			spsub = escape(spsub);	
			smanu = escape(smanu);	
			showAll(spcat,spsub,smanu);
			}
		
	} else {
	   // remove all in the listbox
	   clearListBox("pcat");
	   clearListBox("psub");
	   clearListBox("manu"); 	
	}
}


/*
  @param      Promo Types
*/
function ProcessTypes(selection){
  var listBox = document.getElementById("ptype");
  var chglistBox = document.getElementById("pcat"); 

	if(selection != "") {
	   clearListBox("pcat");
	   clearListBox("psub");
	   clearListBox("manu");	
		selection = cleanString(selection);
		for(var x=0;x<window[selection].length;x++){
			chglistBox[x+1] = new Option(window[selection][x],window[selection][x]);
		}		
	} else {
	   // remove all in the listbox
	   clearListBox("pcat");
	   clearListBox("psub");
	   clearListBox("manu");
		chglistBox[0] = new Option("","");	   
		chglistBox = document.getElementById("psub"); 	   
		chglistBox[0] = new Option("All","All");
		chglistBox = document.getElementById("manu"); 	
		chglistBox[0] = new Option("All","All");		
	}
}
/*
  @param      Promo Cats
*/
function ProcessCats(selection){

  var strResponseArray;
  var listBox = document.getElementById("pcat");
  var chglistBox = document.getElementById("psub"); 
	   clearListBox("psub");
	   clearListBox("manu");	
		chglistBox = document.getElementById("psub"); 	   
		chglistBox[0] = new Option("All","All");
		chglistBox = document.getElementById("manu"); 	
		chglistBox[0] = new Option("All","All");		    
	if(selection != "") {
		//selection = cleanString(selection);
		selection = escape(selection);	
		showSubCats(selection);
	} 
}
/*
  @param      Promo Cats ProcessCatsSelected
*/
function ProcessCatsSelected(selection, subcatselection){

  var strResponseArray;
  var listBox = document.getElementById("pcat");
  var chglistBox = document.getElementById("psub"); 
	if(selection != "") {
	   clearListBox("psub");
	   clearListBox("manu");	
		//selection = cleanString(selection);
		selection = escape(selection);
		//subcatselection = escape(subcatselection);		
		showSubCatsSelected(selection, subcatselection);
	} else {
	   // remove all in the listbox
	   clearListBox("psub");
	   clearListBox("manu");
		chglistBox = document.getElementById("psub"); 	   
		chglistBox[0] = new Option("All","All");
		chglistBox = document.getElementById("manu"); 	
		chglistBox[0] = new Option("All","All");		
	}
}

/*
  @param      Promo Cats ProcessCatSelectedbyManu
*/
function ProcessCatSelectedbyManu(selection, manu){
  var chglistBox = document.getElementById("psub"); 
  clearListBox("psub");
	if(selection != "") {	  
		selection = escape(selection);	
		showSubCatsbyManu(selection, manu);
	} 
}



/*
  @param      Promo Cats ProcessCatsSelectedbyManu
*/
function ProcessCatsSelectedbyManu(selection, subcatselection, manu){

  var strResponseArray;
  var chglistBox = document.getElementById("psub"); 
	if(selection != "") {
	   clearListBox("psub");
		selection = escape(selection);	
		showSubCatsSelectedbyManu(selection, subcatselection, manu);
	} else {
	   // remove all in the listbox
	   clearListBox("psub");
		chglistBox = document.getElementById("psub"); 	   
	}
}

/*
  @param      Promo ProcessManuSelected
*/
function ProcessManuSelected(selection){

manu = selection;

  var listBox = document.getElementById("pcat");
  var chglistBox = document.getElementById("psub"); 
 	   clearListBox("psub");
	   clearListBox("pcat"); 
	if(selection != "") {
		selection = escape(selection);		
		showManuCats(selection);
	} 
}


/*
  @param      Promo SubCats
*/
function ProcessSubCats(selection){
  var listBox = document.getElementById("psub");
  var chglistBox = document.getElementById("manu"); 
 	if(selection != "All" ) {
	   clearListBox("manu");	
 	//if(selection != "All" && window[selection] != undefined) {
	selection = escape(selection);
		showManus(selection);
	} else {
	   // remove all in the listbox
	   clearListBox("manu");
		chglistBox = document.getElementById("manu"); 	
		chglistBox[0] = new Option("All","All");		
	} 
}



/*
  @param      AJAX showManuCats
*/

function showManuCats (manu){

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getcatsbymanu.php"  
	url=url+"?m="+manu
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateCatbyManuChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/*
  @param      AJAX showSubCatsbyManu
*/

function showSubCatsbyManu (str, manu){

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getsubcatsbymanu.php"
	url=url+"?q="+str+"&m="+manu
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateSubCbyManuChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/*
  @param      AJAX showSubCatsSelectedbyManu
*/

function showSubCatsSelectedbyManu (str, subcatselection, manu){

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getsubcatselectedbymanu.php"
	url=url+"?q="+str+"&s="+subcatselection+"&m="+manu
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateSubCSelectedbyManuChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/*
  @param      AJAX showSubCatsSelected
*/

function showSubCatsSelected (str, subcatselection){

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getsubcatselected.php"
	url=url+"?q="+str+"&s="+subcatselection
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateSubCSelectedChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}



/*
  @param      AJAX SubCats
*/

function showSubCats (str){ 
	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getsubcats.php"
	url=url+"?q="+str
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateSubCChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/*
  @param      AJAX SubCats
*/

function showAll (strCat,strSub,strManu){ 
	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getall.php"
	url=url+"?c="+strCat+"&s="+strSub+"&m="+strManu
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateAllChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function stateAllChanged() 
{

	var scSelected = "All";
	var mnSelected = "All";
	var sep = "______";
	var len = 0;
	
    var chglistBox = document.getElementById("psub"); 
	var strResponseArray;
	
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');
		 strResponseArray.pop();

		scSelected = strResponseArray.shift();
		mnSelected = strResponseArray.shift();
	
		len = strResponseArray.indexOf(sep);

		var manulistBox = document.getElementById("manu"); 
					
		var i=0;
		if ( scSelected == "All" ) {
			chglistBox[i] = new Option("All","All",true);
			chglistBox[i].selected = true;
			manulistBox = document.getElementById("manu"); 
			manulistBox[i] = new Option("All","All",true);
			manulistBox[i].selected = true;
		} else {
			chglistBox[i] = new Option("All","All");
		}
			i++;		
			for(var x=0;x<len;x++){			
				if ( scSelected == strResponseArray[x] ) {
					chglistBox[i] = new Option(strResponseArray[x],strResponseArray[x],true);
					chglistBox[i].selected = true;
				} else {
					chglistBox[i] = new Option(strResponseArray[x],strResponseArray[x]);
				}	
				i++;			
			}

			len++;
			len++;			
			i=0;
				if ( mnSelected == "All" ) {
					manulistBox[i] = new Option("All","All",true);
					manulistBox[i].selected = true;					
				} else {
					manulistBox[i] = new Option("All","All");
				}
				i++;
				for(var x=len;x<strResponseArray.length;x++){				
					if ( mnSelected == strResponseArray[x] ) {
						manulistBox[i] = new Option(strResponseArray[x],strResponseArray[x],true);
						manulistBox[i].selected = true;
					} else {
						manulistBox[i] = new Option(strResponseArray[x],strResponseArray[x]);
					}
					i++;			
				}		
	 } 
}


function stateSubCChanged() 
{

    var chglistBox = document.getElementById("psub"); 
	var strResponseArray;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');
		 strResponseArray.pop();
		 
		for(var x=0;x<strResponseArray.length;x++){	
	        chglistBox[x+1] = new Option(strResponseArray[x],strResponseArray[x]);
		}
	 } 
}



function stateSubCSelectedChanged() 
{

 

    var chglistBox = document.getElementById("psub"); 
	var strResponseArray;
	var isselected = false;

	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');
		 strResponseArray.pop();
		 
		var scSelected = strResponseArray[0];

		for(var x=1;x<strResponseArray.length;x++){
			if (strResponseArray[x]==scSelected)
			 {
				isselected = true;
			 }		
	        chglistBox[x+1] = new Option(strResponseArray[x],strResponseArray[x],false,isselected);
				isselected = false;			
		}  
	 } 
}


function stateSubCSelectedbyManuChanged() 
{

    var chglistBox = document.getElementById("psub"); 
	var strResponseArray;
	var isselected = false;
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');

		var scSelected = strResponseArray[0];
		strResponseArray.shift();
		
			//alert (strResponseArray);
		 var i =0;  /////////////////////////////////0
		for(var x=0;x<strResponseArray.length-1;x+=2){
			if (strResponseArray[x+1]==scSelected)  ////////////////////////////////////////////////////////// x
			 {
				isselected = true;
			 }
	        chglistBox[i] = new Option(strResponseArray[x+1],strResponseArray[x],false,isselected);  ////

				isselected = false;	
				i++;				
				
		}  
	 } 
}

/*
  @param      AJAX stateSubCbyManuChanged
*/

function stateSubCbyManuChanged() 
{

    var chglistBox = document.getElementById("psub"); 
	var strResponseArray;
	var isselected = false;

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');

		 strResponseArray.pop();
		 
		//alert(strResponseArray );
		 
		for(var x=0;x<strResponseArray.length;x++){
			isselected = false;	
	        chglistBox[x+1] = new Option(strResponseArray[x],strResponseArray[x],false,isselected);

		}  
	 } 
}

/*
  @param      AJAX stateCatbyManuChanged //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

function stateCatbyManuChanged() 
{
    var chglistBox = document.getElementById("pcat"); 
	var strResponseArray;
	var isselected = false;

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');
		 
		strResponseArray.pop();
		var i = 1;
		for(var x=0;x<strResponseArray.length;x+=2){
			isselected = false;	
	        chglistBox[i] = new Option(strResponseArray[x+1],strResponseArray[x],false,isselected);
			i++;
		}  
	 } 
}



/*
  @param      AJAX Manus
*/

function showManus (str){ 
 //alert ("showManus = " + str); 

	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	var url="includes/getmanus.php"
	url=url+"?q="+str
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateManuChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateManuChanged() 
{
    var chglistBox = document.getElementById("manu"); 
	var strResponseArray;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	  //document.getElementById("testing").innerHTML=xmlHttp.responseText ;
	  	   // remove all in the listbox
		clearListBox("manu");
		chglistBox = document.getElementById("manu"); 	
		chglistBox[0] = new Option("All","All");	
	 	 strResponseArray = xmlHttp.responseText.split('\n\n');
		 //alert ("strResponseArray = " + xmlHttp.responseText); 
		 strResponseArray.pop();
		for(var x=0;x<strResponseArray.length;x++){
	        chglistBox[x+1] = new Option(strResponseArray[x],strResponseArray[x]);
		}  
	 } 
}




/*
  @param      AJAX Request
*/

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;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
// prototype 
if (!Array.prototype.indexOf){
  Array.prototype.indexOf = function(elt /*, from*/)  
  {    
  var len = this.length;
  var from = Number(arguments[1]) || 0;
  from = (from < 0)
		? Math.ceil(from)
		: Math.floor(from);
    if (from < 0)
		from += len;
		for (; from < len; from++)
		{
			if (from in this &&
			this[from] === elt)
			return from;
			}
			return -1;  
			};
}
  
  
  
  
  
  
  