﻿// commonly validated items in forms
function validateCommon(objBool)
{
    var currentStatus = objBool;
    if ($('newcust1') != null)
    {
        currentStatus = errorLabel(currentStatus,'err_newcust',($('newcust1').checked || $('charterCus').checked ));
        if (($('billingacctno1') != null) && $('charterCus').checked)
            currentStatus = errorLabel(currentStatus,'err_billingacctno',(chkNumber($F('billingacctno1'),3) && chkNumber($F('billingacctno2'),3) && chkNumber($F('billingacctno3'),4)));
    } 
    else
        if ($('billingacctno1') != null)
           currentStatus = errorLabel(currentStatus,'err_billingacctno',(chkNumber($F('billingacctno1'),3) && chkNumber($F('billingacctno2'),3) && chkNumber($F('billingacctno3'),4)));
    if ($('company_name') != null)
        currentStatus = errorLabel(currentStatus,'err_company_name',(validateNotEmpty($F('company_name'))));
    if ($('serv_address1') != null)
        currentStatus = errorLabel(currentStatus,'err_serv_address',(validateNotEmpty($F('serv_address1'))));
    if ($('serv_city') != null)
        currentStatus = errorLabel(currentStatus,'err_serv_city',(validateNotEmpty($F('serv_city'))));
    if ($('serv_st') != null)
        currentStatus = errorLabel(currentStatus,'err_serv_st',($('serv_st').selectedIndex > 0));
    if ($('serv_zip') != null)
        currentStatus = errorLabel(currentStatus,'err_serv_zip',(validateNotEmpty($F('serv_zip'))));
    if ($('first_name') != null)
        currentStatus = errorLabel(currentStatus,'err_first_name',(validateNotEmpty($F('first_name'))));
    if ($('last_name') != null)
        currentStatus = errorLabel(currentStatus,'err_last_name',(validateNotEmpty($F('last_name'))));
    if ($('contact_email') != null)
    {
        currentStatus = errorLabel(currentStatus,'err_contact_email',(validateEmail($F('contact_email'))));
         if ($('contact_email2') != null)
            currentStatus = errorLabel(currentStatus,'err_contact_email2',($F('contact_email') == $F('contact_email2')));
    }
    if ($('main_phone1') != null)
        currentStatus = errorLabel(currentStatus,'err_main_phone',(chkNumber($F('main_phone1'),3) && chkNumber($F('main_phone2'),3) && chkNumber($F('main_phone3'),4)));
    if ($('day_phone1') != null)
        currentStatus = errorLabel(currentStatus,'err_day_phone',(chkNumber($F('day_phone1'),3) && chkNumber($F('day_phone2'),3) && chkNumber($F('day_phone3'),4)));
    if ($('alt_phone1') != null)
        currentStatus = errorLabel(currentStatus,'err_alt_phone',(chkNumber($F('alt_phone1'),3) && chkNumber($F('alt_phone2'),3) && chkNumber($F('alt_phone3'),4)));
    if ($('timetocall') != null)
        currentStatus = errorLabel(currentStatus,'err_timetocall',($('timetocall').selectedIndex > 0));
    if ($('BellsouthEnhanceServices1') != null)
        currentStatus = errorLabel(currentStatus,'err_BellsouthEnhanceServices',($('BellsouthEnhanceServices1').checked || $('BellsouthEnhanceServices2').checked )); 
    return currentStatus;
}

// show hide btn
function showBTN(chkYes,objBtn)
{
    $(objBtn).style.display = 'none';
    if ($(chkYes).checked)
        $(objBtn).style.display = 'block';
}

//  if error with input highlite label
function errorLabel(boolStatus,strObj,boolObj)
{
    if (boolObj)
    {
        $(strObj).className='unspan';
        if (!boolStatus)
            return false;
        else
            return true;
    }
    if ($(strObj) == null)
        alert(strObj);
    $(strObj).className='errRed';
    return false;
}

//  set notice of errors and resets form to top of page
function errorNotice(boolObj)
{
    if (!boolObj)
	{
		$("err_notice").innerHTML= '<img src="_images/forms/icon_warning.gif"><span class=errRed><b>Please complete the information highlighted in red below.</b></span>';
		window.scroll(0,1);
	}

}
// get object reference by the object id
function $(strObj)
{
//    alert(strObj);
  if (document.getElementById && document.getElementById(strObj)) 
        return document.getElementById(strObj);
  else 
    if (document.all && document.all(strObj)) 
        return document.all(strObj);
    else   
        return null;
}


//get object value
function $F(strObj)
{
    return $(strObj).value;
}

// get query string variable from end of url
function queryValue(strKey) 
{
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) 
    {
        var pos = parms[i].indexOf('=');
        if (pos > 0) 
            if (parms[i].substring(0,pos).toLowerCase() == strKey.toLowerCase())
                return parms[i].substring(pos+1);
    }
    return null;
}      

//  check checkbox if value is correct
function checkObject(strObj,strKey,strValue)
{
    var rtnValue = queryValue(strKey)
    if (rtnValue != null)
        if (rtnValue == strValue)
            $(strObj).checked = true;
}



//  valid currency
function validateCurrency(strValue)  
{

  var objRegExp = /(^\$\d{1,3}(,\d{3})*\.\d{2}$)|(^\(\$\d{1,3}(,\d{3})*\.\d{2}\)$)/;
  return objRegExp.test( strValue );
}

// valid email
function validateEmail(strValue) 
{
    var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,6}(\.[a-z]{2}){0,2})$/i;
    return objRegExp.test(strValue);
}

// valid numeric
function  validateNumeric(strValue ) 
{
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
  return objRegExp.test(strValue);
}

// not empty 
function validateNotEmpty(strValue ) 
{
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0)
   {
        return true;
   }  
   return false;
}

// valid date
function validateUSDate(strValue) 
{
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
  if(!objRegExp.test(strValue))
    return false; 
  else{
    var arrayDate = strValue.split(RegExp.$1); 
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    if(arrayLookup[arrayDate[0]] != null) 
    {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true;
    }
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; 
  }
  return false;
}

function trimAll(strValue) 
{
 var objRegExp = /^(\s*)$/;
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}



function removeHTMLTags(strTextBlob)
{
    var strInputCode = strTextBlob;
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 	return strInputCode.replace(/<\/?[^>]+(>|$)/g, "");

}

function chkNumber(chkNumber,chkLength)
{
	var chkStatus = true;

	if (chkNumber.length == chkLength)
	{
		for (var i=0; i < chkNumber.length; i++)
		{
			oneChar = chkNumber.charAt(i);
			if ((oneChar < "0") || (oneChar > "9"))	
				chkStatus = false;
		}
		return chkStatus;
	}
	else
		return false;
}

// auto tab
function autoTab(element, nextElement) 
{
    if (element.value.length == element.maxLength && nextElement != null)
        element.form.elements[nextElement].focus();
}

// popup window
var popup = null;

function openWin(page,PWidth,PHeight,flags) 
{ 
 	if (popup)
	{
		if (!popup.closed)
		{
			popup.close();
		}
	}
	popup = null; 

	var PTop = (screen.height/2)-(PHeight/2);
 	var PLeft = (screen.width/2)-(PWidth/2);
 	
	if (flags != null)
	{
		if (flags.substring(0,1) != ",")
		{
			flags = "," + flags;
		}
	}
	
	popup = window.open(page, "ATT", "TOP=" + PTop + ",LEFT=" + PLeft + ",HEIGHT=" + PHeight + ",WIDTH=" + PWidth + flags);
	popup.focus();
 }
 
 // pad numeric values
function padWithZeros(value,decimalPlaces) {
	valueStr = (Math.round((value + 0.0005) * 100) /100).toString();
	var decimalPos = valueStr.indexOf(".");
	if (decimalPos == -1) {
		decimalPartLen = 0;
		valueStr += (decimalPlaces > 0 ? "." : "");
	} else {
		decimalPartLen = valueStr.length - decimalPos - 1;
	}
	var padTotal = decimalPlaces - decimalPartLen;
	if (padTotal > 0) {
		for (k=0;k<padTotal;k++) {
			valueStr += "0";
		}
	}
	return valueStr;
}
 
 //foating div scripts

function getStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	return document.layers[objectId];
    } else {
	return false;
    }
}

function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
//    alert(newXCoordinate +  " " + newYCoordinate);	
	styleObject.left = newXCoordinate + 'px';
	styleObject.top = newYCoordinate + 'px';
	
	return true;
    } else {
	return false;
    }
}


function mouseX(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}

function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
}

function showPopup (targetObjectId, strInfo, winSize, xNew, yNew, eventObj) {
    var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else 
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
    {
    //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else 
        if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
        {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
        }

    
    
    if(eventObj) {
	hideCurrentPopup();
	eventObj.cancelBubble = true;

	var newXCoordinate = eventObj.clientX + scrOfX + xNew; //(eventObj.pageX)?eventObj.pageX + xNew:eventObj.x + xNew + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	var newYCoordinate = eventObj.clientY + scrOfY + yNew; //(eventObj.pageY)?eventObj.pageY + yNew:eventObj.y + yNew + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    window.currentlyVisiblePopup = targetObjectId;
		setPopInfo(strInfo,winSize);
	    return true;
	} else {
	    return false;
	}
    } else {
	return false;
    }
}

function hideCurrentPopup() {
    if(window.currentlyVisiblePopup) {
	document.getElementById('popTitle').innerHTML = '';
	document.getElementById('popText').innerHTML = '';

	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} 

window.onload = initializeHacks;
document.onclick = hideCurrentPopup;

function initializeHacks() {
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    createFakeEventObj();
}

function createFakeEventObj() {
    if (!window.event) {
	window.event = false;
    }
}

function resizeBlankDiv() {
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function doNothing()
{ }
