// JavaScript Document
var ios = 0;
var aos = 0;
var limit = 4;
var isvalid = false;

function insertOldSchool(theSel, newText, newValue)
{
  //alert("called");
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      if (newCount == theSel.selectedIndex) {
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newCount++;
        newSelected = newCount;
      }
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function appendOldSchool(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
      
      if (newCount == theSel.selectedIndex) {
        newCount++;
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newSelected = newCount - 1;
      }
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function removeOldSchool(theSel)
{
  var selIndex = theSel.selectedIndex;
  if (selIndex != -1) {
    for(i=theSel.length-1; i>=0; i--)
    {
      if(theSel.options[i].selected)
      {
        theSel.options[i] = null;
      }
    }
    if (theSel.length > 0) {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    }
  }
  ios = getNumber();
  	if(ios < limit){
		//alert('You have hit the current limit');
		enableField(document.getElementById('add_button1'));
		enableField(document.getElementById('add_button2'));
		hidediv('limit_message');
	}
	if(ios == 0){
		disableField(document.getElementById('remove_button'));
		showdiv('grey_checkout');
		hidediv('active_checkout');
	}
	if(ios <= 1){
		hidediv('charge_message');
	}
	if(ios > 0){
		document.getElementById('x_amount').value = parseFloat(document.getElementById('x_amount').value)-25;
		updateCostFields();
	}
	else{
		updateCostFields();
	}
}




function remove2(theSel)
{
  var selIndex = theSel.selectedIndex;
  if (selIndex != -1) {
    for(i=theSel.length-1; i>=0; i--)
    {
      if(theSel.options[i].selected)
      {
        theSel.options[i] = null;
      }
    }
    if (theSel.length > 0) {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    }
  }
  	ios = getNumber();
  	if(ios < limit){
		//alert('You have hit the current limit');
		enableField(document.getElementById('add_button1'));
		enableField(document.getElementById('add_button2'));
		hidediv('limit_message');
	}
	if(ios == 1){
		disableField(document.getElementById('remove_button'));
		document.getElementById('checkout').innerHTML = '<input type="button" class="next_greyed" value="Checkout" class="disabled"/><div style="clear:both"></div>';
	}
	if(ios <= 1){
		hidediv('charge_message');
	}
	if(ios > 0){
		document.getElementById('x_amount').value = parseFloat(document.getElementById('x_amount').value)-25;
		updateCostFields();
	}
	else{
		updateCostFields();
	}
}


function addValues2(target, source){	
	//alert("CALLED");
	target = document.getElementById(target);
	source = document.getElementById(source);
	appendOldSchool(target, source.options[source.selectedIndex].text,  source.options[source.selectedIndex].value);
	ios = getNumber(); 
	//alert(ios);
	if(ios >= limit){
		//alert('You have hit the current limit');
		showdiv('limit_message');
		disableField(document.getElementById('add_button1'));
		disableField(document.getElementById('add_button2'));
		alert("Your limit of 4 destinations/themes has been reached");
	}
	if(ios > 1){
		enableField(document.getElementById('remove_button'));
		document.getElementById('checkout').innerHTML = '<input type="button" class="next_arrow" value="Checkout"  onclick="checkoutProcedure(); document.getElementById(\'signup_form\').submit();"/><div style="clear:both"></div>';
	}
}










function addValues(target, source){	
	//alert("CALLED");
	target = document.getElementById(target);
	source = document.getElementById(source);
	insertOldSchool(target, source.options[source.selectedIndex].text,  source.options[source.selectedIndex].value);
	ios = getNumber(); 
	if(ios > 0){
		updateCostFields();
	}
	if(ios > 1){
		showdiv('charge_message');
		document.getElementById('x_amount').value = parseFloat(document.getElementById('x_amount').value)+25;
		updateCostFields();
	}
	if(ios >= limit){
		//alert('You have hit the current limit');
		showdiv('limit_message');
		disableField(document.getElementById('add_button1'));
		disableField(document.getElementById('add_button2'));
		alert("Your limit of 4 destinations/themes has been reached");
	}
	if(ios == 1){
		enableField(document.getElementById('remove_button'));
		hidediv('grey_checkout');
		showdiv('active_checkout');
	}
}

function updateCostFields(){	
	var opts = document.getElementById('selos').options;
	var dest = document.getElementById('destinations');
	var themes = document.getElementById('themes');
	var specials = document.getElementById('has_specials');

	var dcount = 0;
	var tcount = 0;
	
	var dlist = Array();
	var tlist = Array();
	
	for(i=0; i<opts.length; i++){
		var t = opts[i].value;
		if(t.match('dest')){
			dlist[dcount]=t.replace('dest-','');
			dcount++;
		}
		else{
			tlist[tcount]=t.replace('theme-','');
			tcount++;
		}
	}
	
	//alert(tcount + " " + dcount);
	
	var fields = '<table>';
	fields += '<tr><td class="left">1</td><td>TripWitt Base Package<br />$250.00</td></tr>';
	
	var tl = tlist.length;
	var dl = dlist.length;
	
	if(getNumber() > 0){
		if(dl > 0){
			fields += "<tr><td class='left' style='color:#77a736;'>1 </td><td style='color:#77a736;'>Free Destination Listing  <br/> $0</td></tr>";
		}
		else{
			fields += "<tr><td class='left' style='color:#77a736;'>1 </td><td style='color:#77a736;'>Free Theme Listing  <br/> $0</td></tr>";
		}
	}
	
	if(dl+tl >= 2 && dl > 0 && dl >= tl){
		dl-=1;
	}
	else if(dl+tl >= 2 && tl > 0 && tl > dl){
		tl-=1;
	}
	
	//alert(dl+tl);
	//alert(dl);
	//alert(tl);
	
	if(getNumber() > 1){
		if(dl > 0 && (dl + tl) > 0){
			fields += "<tr><td class='left'>"+dl+"</td><td> TripWitt Destination Listing <br/> $"+(dl *25)+"</td></tr>";
		}
		if(tl > 0 && (dl + tl) > 0){
			
			fields += "<tr><td class='left'>"+tl+"</td><td> TripWitt Theme Listing <br/> $"+(tl *25)+"</td></tr>"; 
		}
	}
	
	if(typeof(specials) != 'undefined' && specials.checked == true){
		fields += "<tr><td class='left'>1</td><td> Profile Specials Listing <br/> $50</td></tr>"; 
	}
	
	
	fields +="<tr><td>&nbsp;</td></tr><tr><td colspan='2' class='total'>Total $"+document.getElementById('x_amount').value+"</td></tr></table>";
	
	
	document.getElementById('costFields').innerHTML = fields;
}

function updateSpecials(bool){
	if(bool == true){
		document.getElementById('x_amount').value = parseFloat(document.getElementById('x_amount').value)+50;
		updateCostFields();
	}
	else{
		document.getElementById('x_amount').value = parseFloat(document.getElementById('x_amount').value)-50;
		updateCostFields();	
	}
}

function getDestinationsCartList(){
	var opts = document.getElementById('selos').options;
	var dest = document.getElementById('destinations-list');
	var themes = document.getElementById('themes-list');
	
	var tval='';
	var dval='';

	var dcount = 0;
	var tcount = 0;
	
	var dlist = Array();
	var tlist = Array();
	
	for(i=0; i<opts.length; i++){
		var t = opts[i].value;
		//alert(t);
		if(t.match('dest')){
			dlist[dcount]=t.replace('dest-','');
			dcount++;
		}
		else{
			tlist[tcount]=t.replace('theme-','');
			tcount++;
		}
	}
	//alert("Dcount:"+dcount);
	//alert("Tcount:"+tcount);
	
	if(dcount > 0){
		for(i=0; i<dcount-1; i++){
			dval += dlist[i]+","; 
		}
		dval += dlist[dcount-1];
	}
	if(tlist.length >0){
		for(i=0; i<tcount-1; i++){
			tval += tlist[i]+",";
		}
		tval += tlist[tcount-1];
	}
	
	//alert("Tval:"+tval);
	//alert("Dval:"+dval);
	document.getElementById('themes-list').value = tval;
	//alert("THEMES"+tval);
	document.getElementById('destinations-list').value = dval;
	//alert("DESTINATIONS"+dval);
	//alert("Themes:"+document.getElementById('themes-list').value);
	//alert("Destinations:"+document.getElementById('destinations-list').value);
}

function getNumber(){
	var opts = document.getElementById('selos').options;
	var dest = document.getElementById('destinations');
	var themes = document.getElementById('themes');	
	return opts.length;
}

function cartEnableField(field){
	//alert(limit);
	if(getNumber() < limit){
		enableField(field);
	}
}

function checkValidPass(){
	if(document.getElementById('password').value == document.getElementById('confirm').value){
		return true;
	}		
	else{
		alert("Passwords do not match!");
		return false;
	}
}

function checkValid(){
	//alert('called');
	if(isvalid == true){
		//alert('TRUE');
		return true;
	}
	else{
		//alert('ERROR: There is an unknown problem in your code');
		return false;
	}
}
/*
function checkValid(){
	//alert('called');
	setTimeout( "end();", 5000); 
}

function checkValidLogic{
	if(isvalid == true){
		//alert('TRUE');
		return true;
	}
	else{
		//alert('ERROR: There is an unknown problem in your code');
		return false;
	}
}
*/
function checkUserName(){
	var login = document.getElementById('login').value;
	
	if(login.length < 4){
		alert("Your username must be greater than 4 characters");
		return false;
	}
	else{
		return true;
	}
}

function checkoutProcedure(){
	getDestinationsCartList();
	if(checkValidPass() && checkUserName()){
		return true;
	}
	else{
		return false;
	}
}
/// These functions help with the checkout fields
