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


function openWin(url,w,h) {
	window.open(url,'pop','width='+w+',height='+h+',scrollbars=no,toolbars=no');
}
//Returns a value of a drop down
function valueOfDropDown(aDropDown){return aDropDown.options[aDropDown.selectedIndex].value;}
//Returns a text of a drop down
function textOfDropDown(aDropDown){return aDropDown.options[aDropDown.selectedIndex].text;}

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}
function isDate (day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
} 

function getOBJ(name,document){try{return document.getElementById(name);}catch(e){return null;}}
function showOBJ(name) {
  theDiv = document.getElementById(''+name);
  theDiv.style.display='block';
}
function hideOBJ(name) {
  theDiv = document.getElementById(''+name);
  theDiv.style.display='none';
}
function popOBJ(name,content) {
  theDiv = document.getElementById(''+name);
  theContent = parent.frames.loader.document.getElementById(''+content).innerHTML;
  theDiv.innerHTML = theContent;
  theContent = '';
  loading(0);
  
  
}
function popOBJWithString(name,content) {
  theDiv = document.getElementById(''+name);
  theDiv.innerHTML = content;
}
function setLoaderSrc(iframe, src) {
  frame = document.getElementById(iframe+'');
  frame.src = src;
}

var hex=255;
function fadeIt(obj, page) {
  buttocks = obj;
  whereTo = page;
  theObj = obj.id;
  theID = theObj;
  startPos = (theID.indexOf('w')+1);
  endPos = theID.length;
  theID = theID.substring( startPos, endPos );

  hex-=11; // increase colour darkness
  obj.style.backgroundColor="rgb("+hex+","+hex+","+hex+")";
  if (hex == 46) {
    hex = 255;
    if (theID.indexOf('&') >= 0 ) {
      startPos = (theObj.indexOf('w')+1);
      endPos = theObj.indexOf('&');
      theID = theObj.substring( startPos, endPos );
      startPos = (theObj.indexOf('&'));
      endPos = theObj.length;
      extras = theObj.substring( startPos, endPos );
      if(whereTo == 'clientDetail.asp') {
        parent.pullTheWool();
      }
      obj.style.backgroundColor="rgb(255,255,255)";
     parent.frames.document.getElementById('contentLoader').src = whereTo+'?recordID='+theID+extras;
    } 
    else {
      if(whereTo == 'clientDetail.asp') {
        parent.pullTheWool();
      }
      obj.style.backgroundColor="rgb(255,255,255)";parent.frames.document.getElementById('contentLoader').src = whereTo+'?recordID='+theID;
    }
  } else {setTimeout("fadeIt(buttocks, whereTo);",20);}
}

function editCell(cell) {
  if (document.all) {
    cell.innerHTML =
    '<input ' +
    ' id="editCell"' +
    ' onclick="event.cancelBubble = true;"' + 
    ' onchange="setCell(this.parentElement, this.value)" ' +
    ' onblur="setCell(this.parentElement, this.value)" ' +
    ' value="' + cell.innerText + '"' +
    ' size="20"' +
    '>';
    document.all.editCell.focus();
    document.all.editCell.select();
  }
  else if (document.getElementById) {
    cell.normalize();
    var input = document.createElement('INPUT');
    input.setAttribute('value', cell.firstChild.nodeValue);
    input.setAttribute('size', cell.firstChild.nodeValue.length);
    input.setAttribute('style', 'font-size:11px;');
    input.onchange = function (evt) { setCell(this.parentNode, this.value); };
    input.onclick = function (evt) { evt.cancelBubble = true; if (evt.stopPropagation) evt.stopPropagation(); };
    cell.replaceChild(input, cell.firstChild);
    input.focus();
    input.select();
  }
}

function setCell(cell, value) {
  if (document.all) {
    cell.innerText = value;
  }
  else if (document.getElementById) {
    cell.replaceChild(document.createTextNode(value), cell.firstChild);
  }
}

function changeClass(obj, newClass, multi) {
  if(multi) {
    for(var i=0;i<mItemCount;i++) {
      theObj = obj+i;
      theObj = getOBJ(theObj,document);
      theObj.className = newClass;
    }
  }
  else {
    theObj = getOBJ(obj,document);
    theObj.className = newClass;
  }
}

function changeImg(img, newImg) {
  img = getOBJ(img,document);
  img.src=newImg;
}

function frameBreak() {
  if (top!= self) top.location=self.document.location; 
}

var vehCount=0;
function orderMeVehicle(vCode, vName, vGrpName, vColour) {
  vehCount = vehCount+1;
  theField = getOBJ('ItemCode',document);
  theColourField = getOBJ('ItemColour',document);
  theGrpNameField = getOBJ('ItemModel',document);
  theNameField = getOBJ('ItemDesc',document);
  theDisplay = getOBJ('depositVehicle',document);
  theCount = getOBJ('vehOrdered',document);
  
  html = '<li>'+vCode+' - '+vName+' - '+vColour+' </li>';
  
  if (theField.value == '') {
    theField.value = '\''+vCode+'\'';
    theColourField.value ='\''+vColour+'\'';
    theNameField.value ='\''+vName+'\'';
    theGrpNameField.value ='\''+vGrpName+'\'';
  }
  else {
    theField.value = theField.value+','+'\''+vCode+'\'';
    theColourField.value = theColourField.value+','+'\''+vColour+'\'';
    theNameField.value = theNameField.value+','+'\''+vName+'\'';
    theGrpNameField.value = theGrpNameField.value+','+'\''+vGrpName+'\'';
  }
  
  if (theDisplay.innerHTML == '') {
    theDisplay.innerHTML = html;
  } else {theDisplay.innerHTML = theDisplay.innerHTML+html;}
  
  if (vehCount == 1) {
    theCount.innerHTML = vehCount+' Vehicle Ordered';
  } else {theCount.innerHTML = vehCount+' Vehicles Ordered';}
}

function cancelOrder() {
  theField = getOBJ('ItemCode',document);
  theDisplay = getOBJ('depositVehicle',document);
  theCount = getOBJ('vehOrdered',document);
  theField.value = '';
  theDisplay.innerHTML = '';
  theCount.innerHTML = '';
  vehCount = 0;
}

function emptyInput(input) {
  if(input.value == 'Click to edit') {
    input.value = '';
  }
}

function mailOrder() {
  getOBJ('mailContent',document).value=getOBJ('thisContent',document).innerHTML;
  getOBJ('mailTo',document).value = getOBJ('EmailTo',document).value;
  document.EmailIt.submit();
}

function changeStatus(orderID, type, status) {
  if(type) {
    theField = 'bAppMarketing';
  } else {
    theField = 'bAppFinance';
  }
  setLoaderSrc('loader', 'changeStatus.asp?ipkOrderID='+orderID+'&'+theField+'='+status);
}

function pullTheWool(flip){
  var oListing = document.getElementById('oListing');
  var oDetail = document.getElementById('oDetail');
  if(flip) {
    oListing.style.display = 'none';
    oDetail.style.display = 'block'; 
    oDetail.innerHTML = 'Loading...';
  } else {
    oListing.style.display = 'block';
    oDetail.style.display = 'none';  
  }
}

var row = 0;
function addPart() {
  row++;
  if (row == 10) {
    row = 1;
  }
  var partRow = document.getElementById('partRow'+row);
  if (document.all) {
    partRow.style.display = 'block'; 
  } else {
    partRow.style.display = 'table-row';
  }
  if (row == 9) {
    var addPartBut = document.getElementById('addPartBut');
    addPartBut.style.display = 'none'; 
  }
}


function chooseColour(cell, sName) {
  
  var colourSelectStart = 
  '<select name="theColour" class="editable" onchange="gotColour(this.parentElement.id, this.value, \''+sName+'\');">' +
    '<option value="">Select</option>';
  var colourSelectEnd = '</select>';
  
  var theCell = document.getElementById(cell);
  var colourList = sName.replace(' ', '');
  theCell.innerHTML = colourSelectStart+ eval(colourList) +colourSelectEnd;
}

var startColourCell = '<a href="#" onclick="parent.chooseColour(\'';
var middleColourCell = '\');" class="BigLink">';
var endColourCell = '</a>';
function gotColour(cellID, colour, sName) {
  var theCell = document.getElementById(cellID);
  var theField = eval('document.vehicles.'+cellID);
  sName = '\''+sName.replace(' ', '');
  theCell.innerHTML = startColourCell + cellID + '\'' + ', ' + sName + middleColourCell + colour + endColourCell;
  theField.value = colour;
}

function loading(status) {
  var showLoading = document.getElementById('showLoading');
  if (status) { showLoading.style.visibility = 'visible'; }
  else { showLoading.style.visibility = 'hidden'; }
}

function popIt () {
  document.getElementById('Benefits').style.visibility = 'visible';
  setTimeout("document.getElementById('Benefits').style.visibility = 'hidden';", 3000); 
}
function twodec(value) {
  tempValue = parseInt(value);
	return tempValue;
}

function calcIt() {
  fMonthly = document.getElementById('fMonthly').value;
  fDeposit = fMonthly*1.5;
  fPaid = fDeposit*0.2;
  fAmount = parseFloat(fDeposit)-parseFloat(fPaid);
  fPremium = fAmount*0.063333;
  
  document.getElementById('fDeposit').value = twodec(fDeposit);
  document.getElementById('fPaid').value = twodec(fPaid);
  document.getElementById('fAmount').value = twodec(fAmount);
  document.getElementById('fPremium').value = twodec(fPremium);
}


