// 
function submitform(formname, action, page, special)
{
  if(action)
  {
	formname.ac.value = action;
  }
  // page count indicates not a actual form submit, but a navigation
  if(page)
  {
	formname.pg.value = page;
  }	
 //else if(action != '0014')
 //{
 // 	formname.dc.value = action;
 // }	
  if(special)
  {
	var special_array=special.split("=");
	formname[special_array[0]].value = special_array[1]; 
  }
  formname.submit();
}

function selectAll(formname, n)
{
	for(var i = 0; i < formname.elements.length; i++)
	{
		if(formname.elements[i].type == 'checkbox')
		{
			formname.elements[i].checked = n;
		}
	}
}

function CamSelectAll(formname, n)
{
        for(var i = 0; i < formname.elements.length; i++)
        {
                if(formname.elements[i].type == 'checkbox')
                {
                        formname.elements[i].checked = n;
                }
        }
}


function textLightCounter(field, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
       	{
       		field.value = field.value.substring(0, maxlimit);
               	alert("lightbox name too long");
     	}

      	var last_char = field.value.charAt(field.value.length - 1);
       	var valid   = /[ \w\.\;]/;
       	var checker = last_char.match(valid);
       	if(!checker)
       	{
       		alert("invalid character");
              	field.value = field.value.substring(0, field.value.length - 1);
 	}
}

function sumSize(docu, formname)
{
	var counter = 0;
	var totaliser	= docu.getElementById('resultsNum').innerHTML.match(/\d+\.\d+/g);
	var currentSize = 0;
	if(totaliser){
	    currentSize = parseFloat(docu.getElementById('resultsNum').innerHTML.match(/\d+\.\d+/g));
	}
	else
	{
            currentSize = parseFloat(docu.getElementById('resultsNum').innerHTML.match(/\d+/g));
	}

        for(var i = 0; i < formname.elements.length; i++)
        {
                if((formname.elements[i].type == 'radio')&&(formname.elements[i].checked))
                {
			var previousValue = "esc"+formname.elements[i].name;
			if(formname[previousValue])
			{
				counter -= parseFloat(formname[previousValue].value);
				formname[previousValue].value = formname.elements[i].value;
			}
                        counter += parseFloat(formname.elements[i].value);
			counter = Math.round(counter*10)/10;
                }
        }
	var finalFigure = Math.round((parseFloat(currentSize) + parseFloat(counter))*10)/10;
	docu.getElementById('resultsNum').innerHTML = "Total download size: "+finalFigure+" MB";
}

 var effectDone = false;
 function toggleEffect(formname) {
      if ( !effectDone ) {
         effectDone = true;
         document.getElementById('selectAll').innerHTML = "<a href=\"javascript:selectAll(document.theForm,0);toggleEffect(document.theForm);\" class=\"functionLink\">Deselect all images on this page &#187;<\/a>";
      }
      else {
         effectDone = false;
         document.getElementById('selectAll').innerHTML = "<a href=\"javascript:selectAll(document.theForm,1);toggleEffect(document.theForm);\" class=\"functionLink\">Select all images on this page &#187;<\/a>";
      }
   }

function CamToggleEffect(formname) {
      if ( !effectDone ) {
         effectDone = true;
         document.getElementById('selectAll').innerHTML = "<a href=\"javascript:CamSelectAll(document.theForm,0);CamToggleEffect(document.theForm);\" class=\"functionLink\">Deselect all images on this page &#187;<\/a>";
	 formname.pallRID.value = 1;
      }
      else {
         effectDone = false;
         document.getElementById('selectAll').innerHTML = "<a href=\"javascript:CamSelectAll(document.theForm,1);CamToggleEffect(document.theForm);\" class=\"functionLink\">Select all images on this page &#187;<\/a>";
	 formname.pallRID.value = 0;
      }
   }

function preview(imageName) {
	previewPane = window.open("",imageName,"scrollbars=yes,resizable=yes,width=820,height=580")
	previewPane.focus()
}
		
function info(recInfo) {
	infoPane = window.open("",recInfo,"scrollbars=yes,resizable=yes,width=875,height=395")
	infoPane.focus()
}

function help(fileLocation) {
	helpPane = window.open(fileLocation,"","scrollbars=yes,resizable=yes,width=955,height=650")
	helpPane.focus()
}

function contact(fileLocation) {
	helpPane = window.open(fileLocation,"","scrollbars=yes,resizable=yes,width=955,height=500")
	helpPane.focus()
}

//Pass in variables e.g. onclick="genericPop('../../help/bmw_legal.html','width=955','height=500');"
function genericPop(fileLocation,width,height) {
	pop = window.open(fileLocation,"","scrollbars=yes,resizable=yes,"+ width +","+ height +"")
	pop.focus()
}

function sendToMe(formname,isType) {
	if(isType == "CD") {
		formname.Rec_Phone.value = formname.Orig_Phone.value;
		formname.Rec_Address.value = formname.Orig_Add.value;	
	}

	formname.Rec_Name.value = formname.Orig_Name.value;
        formname.Rec_Email.value = formname.Orig_Email.value;
	formname.Rec_Company.value = formname.Orig_Company.value;
}

function textCounter(field, maxlimit) {
                        if (field.value.length > maxlimit){
                        	field.value = field.value.substring(0, maxlimit);
                        }else{
				var countLeft = maxlimit - field.value.length;
				var counterElementID	= "boardCount_"+field.name;
                        	document.getElementById(counterElementID).innerHTML = "Maximum characters "+maxlimit+". "+countLeft+" characters remaining.";
			}
}

function orderUrgency(select) {
	if(select == "Post") {
		document.getElementById('urgency').innerHTML = "Urgency:<a href=\"#\" onClick=\"javascript:orderUrgency('Post')\" class=method_selected>Post<\/a><a href=\"#\" onClick=\"javascript:orderUrgency('Overnight')\" class=method>Overnight<\/a><a href=\"#\" class=\"method\" onClick=\"javascript:orderUrgency('Sameday')\">Sameday<\/a><input type=\"hidden\" name=\"Ord_Urg\" value=\"Post\" \/>";
	}

	if(select == "Overnight") {
		document.getElementById('urgency').innerHTML = "Urgency:<a href=\"#\" onClick=\"javascript:orderUrgency('Post')\" class=method>Post<\/a><a href=\"#\" class=\"method_selected\" onClick=\"javascript:orderUrgency('Overnight')\">Overnight<\/a><a href=\"#\" class=method onClick=\"javascript:orderUrgency('Sameday')\" >Sameday<\/a><input type=\"hidden\" name=\"Ord_Urg\" value=\"Overnight\" \/>";
	}

	if(select == "Sameday") {
		document.getElementById('urgency').innerHTML = "Urgency:<a href=\"#\" onClick=\"javascript:orderUrgency('Post')\" class=\"method\">Post<\/a><a href=\"#\" class=\"method\" onClick=\"javascript:orderUrgency('Overnight')\">Overnight<\/a><a href=\"#\" onClick=\"javascript:orderUrgency('Sameday')\" class=\"method_selected\">Sameday<\/a><input type=\"hidden\" name=\"Ord_Urg\" value=\"Sameday\" \/>";
	}
}

function validate(x, type) {
        var password = '';
        if(type == "password"){
                if(x.PWD1.value == "") {
                        return true;
                }
                if(x.PWD1.value != x.PWD2.value){
                        alert('The password you have entered does not match! ['+x.PWD1.value+']['+x.PWD2.value+']');
                        return false;
                }
        }
        var illegalChars = /[\W_]/; // allow only letters and numbers
        if ((x.PWD1.value.length < 6) || (x.PWD1.value.length > 8)) {
                alert('The password must be 6-8 characters long.');
                return false;
        } else if (illegalChars.test(x.PWD1.value)) {
                alert('The password you have entered contains invalid characters (letters and numbers only)');
                return false;
        }
        return true;
}

function passwordView() {
        if(document.getElementById('passp').style.display == 'none'){
                document.getElementById('passp').style.display = 'block';
        }else{
                document.getElementById('passp').style.display = 'none';
        }

}

function divView(id,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\\\s)user_panel(\\\\s|$)");
        for (i = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        els[i].style.display = 'none';
                        els[i].style.height = '0px';
                }
        }
        if(document.getElementById(id).style.display != 'block'){
                document.getElementById(id).style.display = 'block';
                document.getElementById(id).style.height = '100%';
        }else{
                document.getElementById(id).style.display = 'none';
                document.getElementById(id).style.height = '0px';
        }

}

function multiSelect(field) {
    document.getElementById(field).multiple=true;
}
