/********* ******** **********\
*                             *
*   (C) 2004 Kamran Layegh    *
*      kamran@layegh.com      *
*                             *
\******************* *********/

/* CONF START */
//RUN_MODE="localDev";//set to 'localDev' | 'siteProd'
RUN_MODE="siteProd";//set to 'localDev' | 'siteProd'
var URL_HTMLS='';
switch (RUN_MODE){
	case 'localDev':
		URL_HTMLS='http://localhost:8080/25cars.com/htmls.php4';
	break;
	case 'siteProd':
		URL_HTMLS='http://www.25cars.com/htmls.php4';
	break;
}

var NO_REDIRECT="Sorry, for security reasons you may not use > and <";
var SABordStyle=new Array('solid','double','groove','ridge','inset','dashed','dotted','none');
var SAColour=new Array('#00ffcc','#00ccff','#666600','#ffcc33','#ff99cc','ff9966','yellow','white','#222222','#666666','#330000','660000','red','blue');
var adBordStyle=new Array('none','solid','double');
var adColour=new Array('black','white','red','green','blue','yellow','#cc9933');
var NEW_DIV_STYLE=0;
var NEW_DIV_COLOR=0;
var NEW_SPEC_ANNOU_STYLE=0;
var NEW_SPEC_ANNOU_COLOR=0;
var PHOTO_PHP_FILE='./photos.php4';
/* CONF END */
var spAnArr=null;
var leftCol=null;
var rightCol=null;
//html for Special Announcement div incl admin buttons
var specAnnouNew="<div class='specialAnnounce'id='_0_0'>";
			specAnnouNew+="<div class='avoidShadow'>";
			specAnnouNew+="<div class='announceHead'id='annouHeadNew'contentEditable=true style='FILTER: progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#000000,strength=5);'>";
			specAnnouNew+="</div></div>";
			specAnnouNew+="<div class='announceDescrip'id='annouDescrNew'contentEditable=true>";
			specAnnouNew+="</div>";
			specAnnouNew+="<br/>";
			specAnnouNew+="<div class='announceButtons'>";
				specAnnouNew+="<button class='btnBorder'>Border</button><button class='btnColour'>Colour</button><button class='spAnNewCancel'id='SANewCancel'>Cancel</button><button class='btnNewSASave'>Save</button> ";
			specAnnouNew+="</div><!-- announceButtons -->";
		specAnnouNew+="</div>";
//html for form with file upload (fu) and all other inputs
var fu="<form id='dynaForm'method='post'>";
			fu+="Please use the Browse buttons to select up to three photos, then click on Save";
			fu+="<input type='file' name='file0'class='fileField'/><br/>";
			fu+="<input type='file' name='file1'class='fileField'/><br/>";
			fu+="<input type='file' name='file2'class='fileField'/><br/>";
			fu+="<input type='hidden' name='doWhat'/>";
			fu+="<input type='hidden' name='col'/>";
			fu+="<input type='hidden' name='ordinal'/>";
			fu+="<input type='hidden' name='heading'/>";
			fu+="<input type='hidden' name='description'/>";
			fu+="<input type='hidden' name='numPhotos'/>";
			fu+="<input type='hidden' name='SABorderStyle'/>";
			fu+="<input type='hidden' name='SABorderColour'/>";
			fu+="<input type='hidden' name='adBorderStyle'/>";
			fu+="<input type='hidden' name='adBorderColour'/>";
		fu+="</form>";
//html for a 'blank ad' with 'head', 'descrip', admin btns and
//form with file upload (fu) and all other inputs
var adHTML="<div class='ad'>";
     adHTML+="<div class='newHead'contentEditable='true'></div>";
     adHTML+="<div class='newDescrip'contentEditable='true'></div>";
     adHTML+="<br/><br/>";
     adHTML+="<div class='adButtons'id='newAdButtons'>";
       adHTML+="<button class='adBorder'>Border</button>";
       adHTML+="<button class='adColor'>Colour</button>";
       adHTML+="<button class='newAdCancel'id='newCancel'>Cancel</button>";
       adHTML+="<button class='newAdSave'id='newSave'>Save</button>";
    adHTML+=" </div>";
   adHTML+=fu;
   adHTML+="</div>";
function init(){
	setProps();
	//force browser to allow scrolling to bottom of content
	expandAdsDiv();
	//slide in the Special Announcement Heading and Description
	slide()
}
function setProps(){
  //put all the props for border and colour of divs in 'spAnArr','leftCol',
  //'rightCol' as delivered by server into ONE array, divsArr.
  //Then use divsArr to set style attribs AND corresponding enumes (ints)
  //for use when administrator changes border style or colour
  var dataArrType=new Array(spAnArr,leftCol,rightCol);
  var divsArr=new Array();
  for(k=0;k<dataArrType.length;k++){
		divsArr[k]=(typeof eval(dataArrType[k])=='undefined')?null:eval(dataArrType[k]);
	}
	//set the border and colour enumerator properties for all divs
	for(i=0;i<divsArr.length;i++){//i is the colNum field in DB table
		//divsArr[0],divsArr[1],divsArr[2],hold info about
		//Special Announcement, ads in lftDiv and ads in rightDiv respectively
		if(divsArr[i]!=null){
			if((divsArr[i].length>0)){
				for(j=0;j<(divsArr[i]).length;j++){//j is the ordinal field in DB table
					var divObj=eval('_'+i+'_'+j);
					divObj.styleEnum=divsArr[i][j][0];
					divObj.colorEnum=divsArr[i][j][1];
					setDivStyle(divObj,'init');
					setDivColor(divObj,'init');
				}
			}
		}
	}
}

function hideUpload(e){
	var contDiv=eval('upload'+e.srcElement.parentElement.parentElement.id);
	contDiv.style.display='none';
}
function letNewEntry(e){
	var newEntBtn=e.srcElement;
	var btnIDEnd=(newEntBtn.id).substr(9);//get _1_0 from newEntBtn_1_0
	var targetDiv=eval('newEntDiv'+btnIDEnd);
	//put a 'blank ad' with head, descrip fields and admin buttons and
	//file uplaod (fu) inputs in target div
	targetDiv.innerHTML=adHTML;
	dynaForm.id='newEntForm'+btnIDEnd;
	//init the new div obj
	var newAdDiv=targetDiv.children[0];
	newAdDiv.id=btnIDEnd;
	newAdDiv.styleEnum=NEW_DIV_STYLE;
	newAdDiv.colorEnum=NEW_DIV_COLOR;
	setDivStyle(newAdDiv,'init');
	setDivColor(newAdDiv,'init');
	targetDiv.style.display='block';
	//give "Cancel" button a ref to the "New Entry" button that invoked this
	newAdDiv.children.newAdButtons.children.newCancel.theNewEntBtn=newEntBtn;
	//hide "New Entry" button
	newEntBtn.style.display='none';
}
function hideNewAd(e){
	var targetDiv=e.srcElement.parentElement.parentElement.parentElement;
	targetDiv.style.display='none';
	//show "New Entry" button
	e.srcElement.theNewEntBtn.style.display='block';
}
function letNewSpecAnnou(e){
	newSADiv.innerHTML=specAnnouNew;
	var newEntBtn=e.srcElement;
	//init the new div obj
	_0_0.styleEnum=NEW_SPEC_ANNOU_STYLE;
	_0_0.colorEnum=NEW_SPEC_ANNOU_COLOR;
	setDivStyle(_0_0,'init');
	setDivColor(_0_0,'init');
	newSADiv.style.display='block';
	//give "Cancel" button a ref to the "New Headline" button that invoked this
	SANewCancel.theNewSpeAnnou=newEntBtn;
	newEntBtn.style.display='none';
}
function hideNewSpecAnnou(e){
	_0_0.style.display='none';
	//show "New Headline" button
	e.srcElement.theNewSpeAnnou.style.display='block';
}
function letAddPhoto(e){
	var adDivID=e.srcElement.parentElement.parentElement.id;
	var contDiv=eval('upload'+adDivID);
	contDiv.innerHTML=fu+"<button class='photoCancel'>Cancel</button>";
	dynaForm.id='addPhotoForm'+adDivID;
	contDiv.style.display='block';
}
function doAdSave(e){
	var saveButton=e.srcElement;
	var theDiv=saveButton.parentElement.parentElement;
	var contDiv=eval('upload'+theDiv.id);
	//distinguish between saving with or without photos
	var theForm=staticForm;
	var $doWhat="updateTextOnly";
	if(contDiv.style.display=='block'){
		theForm=eval('addPhotoForm'+theDiv.id);
		theForm.encoding="multipart/form-data";
		$doWhat="updateTextAndPhoto";
	}
	setSubmit(theForm,theDiv,$doWhat,e);
}
function setSubmit(theForm,theDiv,doWht,e){
	var divID=theDiv.id;
	//split the id into col number and ordinal number
	var colOrd=divID.split('_');										
	if(e.srcElement.className=='newAdSave'){
		colOrd=e.srcElement.parentElement.parentElement.parentElement.id.split('_');
	}
	theForm.doWhat.value=doWht;															
	theForm.col.value=colOrd[1];															
	theForm.ordinal.value=colOrd[2];													
	theForm.adBorderStyle.value=theDiv.styleEnum;													
	theForm.adBorderColour.value=theDiv.colorEnum;													
	theForm.heading.value=translate(theDiv.children[0].innerText);
	theForm.description.value=translate(theDiv.children[1].innerText);
	if(e.srcElement.className=='visitPhoto'){
		theForm.numPhotos.value=(e.srcElement.id).substr(1);
		theForm.action=PHOTO_PHP_FILE;
	}
//ff(theForm);	
	theForm.submit();
}
function deleteAd(e){
	var saveButton=e.srcElement;
	var theDiv=saveButton.parentElement.parentElement;
	var theForm=staticForm;
	setSubmit(theForm,theDiv,'delAd',e);
}
function saveNewAd(e){
	var saveButton=e.srcElement;
	var theDiv=saveButton.parentElement.parentElement;
	theForm=eval('newEntForm'+theDiv.id);
	theForm.encoding="multipart/form-data";
	setSubmit(theForm,theDiv,'insertAd',e);
}

function saveSpAn(e){
	setSubmit(staticForm,_0_0,'updateSATexts',e);
}
function insertNewAnn(e){
	setSubmit(staticForm,_0_0,'insertNewAnnou',e);
}
function setDivStyle(divObj,initIncrem){
  //Called by init() or user clicking a 'Border' button.
  //set border style if divObj is 'Special Announcement'  
  var enumer=divObj.styleEnum;
  if(divObj.className=='specialAnnounce'){
		if(initIncrem=='increment'){
			enumer=(++enumer)%(SABordStyle.length);
			divObj.styleEnum=enumer;
		}
		divObj.style.borderStyle=SABordStyle[enumer];
  }  
  //set border style if divObj is 'ad'
  else if(divObj.className=='ad'){
		if(initIncrem=='increment')enumer=(++enumer)%(adBordStyle.length);
		divObj.style.borderStyle=adBordStyle[enumer];
			divObj.styleEnum=enumer;
		var bordSty=adBordStyle[enumer];
    if(bordSty=='solid')divObj.style.borderWidth=1;
    if(bordSty=='double')divObj.style.borderWidth=3;
  }
}
function setDivColor(divObj,initIncrem){
  //Called by init() or user clicking a 'Colour' button.
  //set border and text colour if divObj is 'Special Announcement'  
  var enumer=divObj.colorEnum;
  if(divObj.className=='specialAnnounce'){
		if(initIncrem=='increment'){
			enumer=(++enumer)%(SAColour.length);
			divObj.colorEnum=enumer;
		}
		divObj.style.color=SAColour[enumer];
		divObj.style.borderColor=SAColour[enumer];
  }  
  //set border style if divObj is 'ad'
  else if(divObj.className=='ad'){
		if(initIncrem=='increment')enumer=(++enumer)%(adColour.length);
		divObj.style.borderColor=adColour[enumer];
		divObj.colorEnum=enumer;
  }
}
function expandAdsDiv(){
  var	l=leftDiv.offsetHeight;
  var	r=rightDiv.offsetHeight;
  ads.style.height=(((l>r)?l:r)+200)+'px';
}
//conf slide function
var accFactor=20;
var slideCounter=0;
function slide(){
	if(typeof _0_0!='undefined'){
		if(_0_0){
			_0_0.children[0].style.left=(-210+slideCounter)+"px";
			_0_0.children[1].style.left=(210-slideCounter)+"px";
			slideCounter+=accFactor;
			accFactor--;
			if(accFactor>0)setTimeout("slide()",40);
		}
	}
}
function handleClick(e){
	//validate className before you do anything
	if(typeof e.srcElement.className!='undefined'){var clsNm=e.srcElement.className;if((clsNm!=null)&&(clsNm!='')){//big if()
		//className is a non-empty string
		switch(clsNm){
			case 'visitPhoto':		//visitor 'Photos>>' button
				setSubmit(staticForm,e.srcElement.parentElement,'getPhotos',e);
				break;
			case 'btnBorder':			//admin Special Announce Border button
				setDivStyle(e.srcElement.parentElement.parentElement,'increment');
				break;
			case 'btnColour':			//admin Special Announce Colour button
				setDivColor(e.srcElement.parentElement.parentElement,'increment');
				break;
			case 'adBorder':			//change ad border style
				setDivStyle(e.srcElement.parentElement.parentElement,'increment');
				break;
			case 'adColor':				//ad Colour button
				setDivColor(e.srcElement.parentElement.parentElement,'increment');
				break;
			case 'adminPhoto':		//administrator 'Photos' button
				letAddPhoto(e);
				break;
			case 'photoCancel':		//'Cancel' button for file upload inputs
				hideUpload(e);
				break;
			case 'newEntryBtn':		//'New Entry' button
				letNewEntry(e);
				break;
			case 'newAdCancel':		//Cancel button on a new Ad
				hideNewAd(e);
				break;
			case 'newSpecAnn':		//"New Headline" button
				letNewSpecAnnou(e);
				break;
			case 'spAnNewCancel':	//"Cancel" button in "New Headline" entry
				hideNewSpecAnnou(e);
				break;
			case 'adSave':				//save button in old ads
				doAdSave(e);
				break;
			case 'spAnnSave':			//save button in an old Special Announcement
				saveSpAn(e);
				break;
			case 'newAdSave':			//save button in a new ad (generated by js)
				saveNewAd(e);
				break;
			case 'adDelete':			// button
				deleteAd(e);
				break;
			case 'btnNewSASave':	//save button in a new Special Announcement
				insertNewAnn(e);
				break;
		}//switch
	}}//big if()
}
function adminOffers(){
	headTextEditForm.action=URL_HTMLS;
	headTextEditForm.submit();
}
function viewHeadText(){
	headTextViewForm.submit();
}
function editHeadText(){
	headTextEditForm.submit();
}
function validateNewPass(){
	var pass1=newPassForm.newPass1FieldID.value;
	var pass2=newPassForm.newPass2FieldID.value;
	if((pass1==pass2)&&(pass1!=''))newPassForm.submit();
	else{
		rubric.innerText='Sorry, the passwords you entered did not match. Please try again:';
		rubric.className='passesNoMatch';
	}
}
function translate(str){
  //delete codee for "<" and ">"
  str=str.replace(/&lt;|&gt;|&#0*60;|&#0*62;/gi,'');
  var transStr="";
  for(i=0;i<str.length;i++){
    var charCode=str.charCodeAt(i);
    switch (charCode){
      //linefeed + carriage return -> <br/>
      case 13:
        break;
      case 10:
        transStr+="<br/>";
        break;
      case 32:
        //translate spaces:
        //leave the FIRST space "breaking", translate 
        //the rest (if any) to non-breaking
        if((i!=0)&&(str.charCodeAt(i-1)==32))transStr+="&nbsp";
        else transStr+=str.charAt(i);
        break;
      //delete "<", and ">"
      case 60:
        alert(NO_REDIRECT);
        break;
      case 62:
        alert(NO_REDIRECT);
        break;
      case 38:
        transStr+="&amp;";
        break;
      case 34:
        transStr+="&quot;";
        break;
      case 39:
        transStr+="&#39;";
        break;
      default:
        transStr+=str.charAt(i);
    }
  }
  return transStr;
}
/* DEBUG START */
function ff(theForm){
	alert('theForm.doWhat.value:'+theForm.doWhat.value);													
	alert('theForm.col.value:'+theForm.col.value);													
	alert('theForm.ordinal.value:'+theForm.ordinal.value);													
	alert('theForm.numPhotos.value:'+theForm.numPhotos.value);														
//	alert(theForm.heading.value);															
//	alert(theForm.description.value);															
	alert('theForm.adBorderColour.value:'+theForm.adBorderColour.value);													
	alert('theForm.adBorderStyle.value:'+theForm.adBorderStyle.value);													
	alert('theForm.id:'+theForm.id);													
}
/* DEBUG END */
