﻿// SIG Submission JScript File

//alert("Loading Jscript File");

function validateAllFields()
{

if
(
validateArray(document.getElementById('accredited')) &&
validateInteger(document.getElementById('hcoid')) &&
validateArray(document.getElementById('orgtype'))&& 
validate(document.getElementById('requester_firstname')) &&
validate(document.getElementById('requester_lastname')) &&
validate(document.getElementById('requester_email')) && 
email(document.getElementById('requester_email')) &&
validate(document.getElementById('requester_phone')) && 
validateAddress(document.getElementById('street_address'), document.getElementById('country')) &&
validateAddress(document.getElementById('city'), document.getElementById('country')) &&
validateAddress(document.getElementById('state'), document.getElementById('country')) &&
validateAddress(document.getElementById('zipcode'), document.getElementById('country')) && 
validateArray(document.getElementById('standardsubject')) && 
validateArray(document.getElementById('accreditationmanual')) && 
validate(document.getElementById('txtQuestionArea'))
)
{
return true;
}
else
{
return false;
}
}// end function validateAllFields()

function hideState(countryArray)
{
    var countryLength = countryArray.length;
    var j = 1;
    var continueLooping = true;

    while ( j < countryLength) 
	{
      var countryName = countryArray[j].value;
      var countrySelectedItem = countryArray[j].selected ;

     	 if (countrySelectedItem == true)
	 		{
	 		countryValue = countryName
      		continueLooping = false;
      		}

	    	if (continueLooping == false) break;
      j++;
    }
    
    if (countryValue == 'USA')
    {
        //Show State
        document.getElementById("lblState").style.visibility='visible';
        document.getElementById("state").style.visibility='visible';
        document.getElementById("lblErrorSt").style.visibility='visible';
    }
    else
    {
        //Hide State
        document.getElementById("lblState").style.visibility='hidden';
        document.getElementById("state").style.visibility='hidden';
        document.getElementById("lblErrorSt").style.visibility='hidden';
    }
      
}


function email(elm)
{
// Check for email address: look for [@] and [.] function isEmail(elm) {
    if (elm.value.indexOf("@") + "" != "-1" &&
        elm.value.indexOf(".") + "" != "-1" && elm.value != "") 
   		{
   		return true;    
    		}
    else 
    		{
		alert("You must enter a valid Email address." + "\n\n" + "i.e. johndoe@myorg.com");
		elm.focus();
		return false;
   		}
} // end function emailvalidation


function validate(field)
{

   	if (field.name == "requester_firstname")
	{
	FieldNameText = "the First Name."
	}
	
	if (field.name == "requester_lastname")
	{
	FieldNameText = "the Last Name."
	}

	if (field.name == "requester_email")
	{
	FieldNameText = "the Requester's E-mail."
	}

	if (field.name == "requester_phone")
	{
	FieldNameText = "the Requester's Phone Number."
	}

	if (field.name == "txtQuestionArea")
	{
	FieldNameText = "the Question you would like answered."
	}

	// Validate Field Values
	var checkFieldValues = field.value;
	newcheckFieldValue = checkFieldValues.replace(/\s/g, "");

	if (newcheckFieldValue == "")
	{
		alreadyValidating = true;
		alert("You must enter a value for " + FieldNameText);
		field.focus();
		return false;
	} // end if
	return true;
	
} // end function validate()


function validateAddress(fieldObject, countryArray)
{

	if (fieldObject.name == "city")
	{
	FieldNameText = "the City."
	}

     if (fieldObject.name == "street_address")
	{
	FieldNameText = "the Street Address."
	}

     if (fieldObject.name == "zipcode")
	{
	FieldNameText = "the Zip Code."
	}

	
// Get Country Selected Value.

	var countryLength = countryArray.length;
   var j = 1;
   var continueLooping = true;

   while ( j < countryLength) 
	{
      var countryName = countryArray[j].value;
      var countrySelectedItem = countryArray[j].selected ;

     	 if (countrySelectedItem == true)
	 		{
	 		countryValue = countryName
      		continueLooping = false;
      		}

	    	if (continueLooping == false) break;
      j++;
      }

	// Validate Field Values
	
	if (fieldObject.name == "state")
	
	{
	    var stateArrayLength = fieldObject.length;
   		var j = 1;
   		var continueLooping = true;

   		while ( j < stateArrayLength) 
		
		{
      		var StateItem = fieldObject[j].value;
      		var stateSelectedItem = fieldObject[j].selected ;

     	 	if (stateSelectedItem == true)
	 			{
      				continueLooping = false;
      			}

	    	if (continueLooping == false) break;
      			j++;
   	   	}

      		if (continueLooping != false && countryValue == "USA") 
				{
					alert("You must select which state you are from!");
					fieldObject.focus();
					return false;
      			}
      			else
      			{
      				return true;
      			}

		// end function validateaccredited()

	}
	
	else
	
	{
		var checkFieldValues = fieldObject.value;
	}
	
	newcheckFieldValue = checkFieldValues.replace(/\s/g, "");
	
	if (newcheckFieldValue == "" && countryValue =="USA")   
	{
		alreadyValidating = true;
		alert("You must enter a value for " + FieldNameText);
		fieldObject.focus();
		return false;
	} // end if
	return true;
	
} // end function validateAddress()


function validateArray(fieldArray)
{

if (fieldArray != null)
{
//Set Return Text Values.

        if (fieldArray.name == "accredited")
	    {
	    FieldNameText = "You must choose Yes or No if you are accredited with JCAHO."
	    }
		
	    if (fieldArray.name == "orgtype")
	    {
	    FieldNameText = "You must select a Type of Organization."
	    }

	    if (fieldArray.name == "standardsubject")
	    {
	    FieldNameText = "You must select a Subject of Standard."
	    }

	    if (fieldArray.name == "accreditationmanual")
	    {
	    FieldNameText = "You must select an Accreditation Manual."
	    }
	
// Checking for the absence of the array fields;
	
   var fieldArrayLength = fieldArray.length;
   var j = 1;
   var continueLooping = true;

   while ( j < fieldArrayLength) 
	{
      var ArrayValue = fieldArray[j].value;
      var fieldArraySelectedItem = fieldArray[j].selected ;

     	 if (fieldArraySelectedItem == true)
	 		{
      		continueLooping = false;
      		}

	    	if (continueLooping == false) break;
      j++;
      }

      if (continueLooping != false) 
	{
	alert( FieldNameText );
	fieldArray.focus();
	return false;
      }
      else
      {
      return true;
      }
 }
 else
 {
 return true;
 } 
} // end function validateArray()

function validateInteger(Field)
{
var newValue = Field.value;
newValue = newValue.replace(/^\s+|\s+$/g,"");
var newLength = newValue.length;
	for(var i = 0; i != newLength; i++){
		aChar = newValue.substring(i,i+1);
			if(aChar < "0" || aChar > "9"){
				alert("Only numbers are valid for use in an HCO Number.  Please remove all other characters that are not numbers.\nIf you do not know your HCO Number, please leave the field blank.");
				Field.focus();
				return false;
			}
	}
Field.value = newValue;
return true;
}// end function Validate Integer


function popup(url) 
{
 var valie = document.getElementById('lblTicketNum').innerText;
 var valother = document.getElementById('lblTicketNum').textContent;
 if (valie == null)
 {
 url = url + valother;
 }
 else
 {
 url = url + valie;
 }
 
 var width  = 800;
 var height = 700;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function formatPhone(value)
{    
	var digits = value.replace(/[^0-9]/ig, '');
	
	if (digits.length == 10)
	{
		var phonenumber = digits.substring(0, 3) + '-' +  digits.substring(3, 6) + '-' +  digits.substring(6, 10); 
	}
	else if (digits.length > 10)
	{
		var phonenumber = digits.substring(0, 3) + '-' +  digits.substring(3, 6) + '-' +  digits.substring(6, 10) + " x " + digits.substring(10, digits.length); 
	}
	else
	{
		var phonenumber = value;
	}
	
	return phonenumber;
}

function formatFax(value)
{
    var digits = value.replace(/[^0-9]/ig, '');
	
	if (digits.length == 10)
	{
		var phonenumber = digits.substring(0, 3) + '-' +  digits.substring(3, 6) + '-' +  digits.substring(6, 10); 
	}
	else if (digits.length > 10)
	{
		var phonenumber = digits.substring(0, 3) + '-' +  digits.substring(3, 6) + '-' +  digits.substring(6, 10) + " x " + digits.substring(10, digits.length); 
	}
	else
	{
		var phonenumber = value;
	}
	
	return phonenumber;
}

function stripPhone(value)
{
	return value.replace(/[^0-9]/ig, '');
}

function stripPhone2(value)
{

	var str=value.replace(/[^0-9]/ig, '');
	var str2=str.substring(0,10);

	return str2;
}

function formatPostalCode(value)
{

	try
	{
		var p = /^([\d]{5})([\d]{4,})$/.exec(value);
		value = p[1]+"-"+p[2];
	}
	catch(ex)
	{
		// do nothing
	}
	finally
	{
 		return true;
	}
}

function replace(originalString,findString,replaceString) {
// Replaces findString with replaceString in originalString
    var strLength = originalString.length, txtLength = findString.length;
    if ((strLength == 0) || (txtLength == 0)) return originalString;

    var i = originalString.indexOf(findString);
    if ((!i) && (findString != originalString.substring(0,txtLength))) return originalString;
    if (i == -1) return originalString;

    var newstr = originalString.substring(0,i) + replaceString;

    if (i+txtLength < strLength)
        newstr += replace(originalString.substring(i+txtLength,strLength),findString,replaceString);

    return newstr;
}