/*
|-----------------------------------------------
| Adjust CSS Layout
|-----------------------------------------------
*/

function adjustLayout()
{
  // Get natural heights
  var cHeight = xHeight("pageBodyData");
  var lHeight = xHeight("pageLeftData");
  var rHeight = xHeight("pageRightData");

  // Find the maximum height
  var maxHeight = Math.max(cHeight, Math.max(lHeight, rHeight));
  maxHeight = maxHeight + 10;

  // Assign maximum height to all columns
  xHeight("pageBody", maxHeight);
  xHeight("pageLeft", maxHeight);
  xHeight("pageRight", maxHeight);

  // Show the footer
  xShow("pageFooter");
}

function adjustLayoutSpanRight()
{
  // Get natural heights
  var cHeight = xHeight("pageBodyData");
  var lHeight = xHeight("pageLeftData");

  // Find the maximum height
  var maxHeight = Math.max(cHeight, lHeight);
  maxHeight = maxHeight + 10;

  // Assign maximum height to all columns
  xHeight("pageBodySpanRight", maxHeight);
  xHeight("pageLeft", maxHeight);

  // Show the footer
  xShow("pageFooter");
}

function xVisibility(e, bShow)
{
  if(!(e=xGetElementById(e))) return null;
  if(e.style && xDef(e.style.visibility)) {
    if (xDef(bShow)) e.style.visibility = bShow ? 'visible' : 'hidden';
    return e.style.visibility;
  }
  return null;
}

function xShow(e)
{
  return xVisibility(e,1);
}

/*
|-----------------------------------------------
| Valid Email
|-----------------------------------------------
*/

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

/*
|-----------------------------------------------
| Reseller Form
|-----------------------------------------------
*/


function validateResellerForm() {
	var form = document.reseller;
	
	if(form.firstName.value == "") {
		alert("Please Enter Your First Name.");
		form.firstName.focus();
		return false;
	}else if(form.lastName.value == "") {
		alert("Please Enter Your Last Name.");
		form.lastName.focus();
		return false;
	}else if(isValidEmail(form.pemail.value) == false) {
		alert("Please Enter A Valid E-Mail.");
		form.pemail.focus();
		return false;
	}else if(form.phone.value == "") {
		alert("Please Enter Your Phone Number.");
		form.phone.focus();
		return false;
	}else if(form.country.value == "") {
		alert("Please Select Your Country.");
		form.country.focus();
		return false;
	}else if(form.language.value == "") {
		alert("Please Select Your Language.");
		form.language.focus();
		return false;
	}else if(document.getElementById("speak_english_yes").checked==false && document.getElementById("speak_english_no").checked==false && form.language.value != 'English') {
		alert("Please Select Your Communication Mode.");
		document.getElementById("speak_english_yes").focus();
		return false;
	}else if(form.imclient.value == "") {
		alert("Please select your Internet Messaging(IM) Client.");
		form.imclient.focus();
		return false;
	}else if(form.imusername.value == "") {
		alert("Please enter your Internet Messaging(IM) Username.");
		form.imusername.focus();
		return false;
	}else if(document.getElementById("voip_reseller_yes").checked==false && document.getElementById("voip_reseller_no").checked==false){
		alert( "Please let us know if you are currently a VoIP Reseller." );
		document.getElementById("voip_reseller_yes").focus();
		return false;
	}else if(document.getElementById("voip_reseller_yes").checked==true && form.salesvolume.value == ''){
		alert( "As a current VoIP Reseller, please select your Monthly Volume range." );
		form.salesvolume.focus();
		return false;
	}
	
	return true;
	
}