// JavaScript Document

var flag_sampleCheck = false;
var animate_delay = 100;
var color_highlight = '#0FAFFF';
var color_normalize = '#F2FCFE';
var color_error = '#FFC4C4';
var color_valid = '#00CC00';


function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}


function field_isempty(field) { 
	if ($(field).val() == '')
	{ 
		return true;
	}
	else 
	{	
		return false;
	}
}

function field_isnumeric(field) 
{ 
   var sText = $(field).val();
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
		 IsNumber = false;			
      }
   }
   
   if (IsNumber == true)  { return true; }
   else { return false; }
}



function field_isemail(field) 
{ 
	email = $(field).val();
	filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) 
	{
  		return true;
	}
	else
  	{
		return false;
	}
}


function field_emailcheck(field,optionObj) 
{
	if ((field.val().length < 1) || (!(field_isemail(field))))
	{ 
		if (field.hasClass('error'))
			keepError(field); 
		else
			setError(field,optionObj.errorMsg);	
		return 0;
	}
	else { removeError(field); return 1; }
}



function field_phonecheck(field,optionObj) 
{	
	
	var phone_type = field.attr('title');
	var phone_length; 
	var phone_complete; 

	if (phone_type == 'HomePhone') { 
		phone_length = $("#id_HomePhone_1").val().length;
		phone_length += $("#id_HomePhone_2").val().length;
		phone_length += $("#id_HomePhone_3").val().length;
		phone_complete = $("#id_HomePhone_1").val() + $("#id_HomePhone_2").val() + $("#id_HomePhone_3").val()
	}
	
	if (phone_type == 'WorkPhone') { 
		phone_length = $("#id_WorkPhone_1").val().length;
		phone_length += $("#id_WorkPhone_2").val().length;
		phone_length += $("#id_WorkPhone_3").val().length;
		phone_complete = $("#id_WorkPhone_1").val() + $("#id_WorkPhone_2").val() + $("#id_WorkPhone_3").val()
	}	
	
	
	if ((phone_length < optionObj.minLength) || (!(IsNumeric(phone_complete))))
	{ 
		if (field.hasClass('error'))
			keepError(field); 
		else
			setError(field,"Valid phone # required");			
		return 0;
	}
	else { removeError(field); return 1; }
}




function setError(field,ErrorMsg) 
{ 
	var field_name = field.attr('title');
	var error_id = 'ErrID_' + field_name;
	field.addClass('error');
	// field.parent().animate({'backgroundColor' : color_error},animate_delay);
	field.parent().append('<div id="' + error_id + '" class="error_msg"> ' + ErrorMsg + '</div>');	
}

function removeError(field) 
{ 
	var field_name = field.attr('title');
	var error_id = '#ErrID_' + field_name;
	field.removeClass('error');
	$(error_id).remove();
}

function keepError(field) 
{ 
	// field.parent().animate({'backgroundColor' : color_error},animate_delay);
}

function field_alphanumeric(field,optionObj) 
{
	if (field.val().length < optionObj.minLength)
	{ 
		if (field.hasClass('error'))
			keepError(field); 
		else
			setError(field,optionObj.errorMsg);		
			
		return 0;
	}
	else { removeError(field); return 1; }		
}



function field_focus() 
{ 
	$(this).css({'border' : '1px solid #00A4FF'});
//	$(this).parent().animate({'backgroundColor' : color_highlight},animate_delay);
			
}


function field_normalize() { 
//	$(this).parent().css({'backgroundColor' : color_normalize}); 
	$(this).css({'border' : '1px solid #CCCCCC'}); 
}


function validateform() { 

	var errorCode = new Array();
	for (var i=0; i <= 12; i++) { errorCode[i] = -1; } 

    var optionobj = { minLength : 1, errorMsg : 'Required Field' };
	
	errorCode[0] = field_alphanumeric($("#id_FirstName"),optionobj);
	errorCode[1] = field_alphanumeric($("#id_LastName"),optionobj);
	errorCode[2] = field_alphanumeric($("#id_Address"),optionobj);
	errorCode[3] = field_alphanumeric($("#id_City"),optionobj);
	errorCode[4] = field_alphanumeric($("#id_State"),optionobj);
	
	var optionobj = { minLength : 5, errorMsg : 'Valid Zip Code required' };	
	
	errorCode[5] = field_alphanumeric($("#id_Zipcode"),optionobj);
	
	var optionobj = { errorMsg : 'Valid email required' };
	errorCode[6] = field_emailcheck($("#id_Email"),optionobj);
		
	var optionobj = { minLength : 10, errorMsg : 'Valid Phone# required' };
	
	errorCode[7] = field_phonecheck($("#id_HomePhone"),optionobj);
	errorCode[8] = field_phonecheck($("#id_WorkPhone"),optionobj);
	
	var optionobj = { minLength : 1, errorMsg : 'Required Field' };
	
	errorCode[9] = field_alphanumeric($("#id_ContactTime"),optionobj);
	errorCode[10] = field_alphanumeric($("#id_AmountOwed"),optionobj);
	errorCode[11] = field_alphanumeric($("#id_Income"),optionobj);
	errorCode[12] = field_alphanumeric($("#id_Home"),optionobj);	
	
	
	
	var errorFound = false;
	for (var i=0; i <= 12; i++) 
	{ 
		
		if (errorCode[i] <= 0) { errorFound = true; }
	} 	
	
	var errorheader;
	
	if (errorFound == true) 
	{ 
		errorheader = '<h1 class="errorheader">Oops, look like you missed filling in some required information</h1>'; 
		errorheader += '<h2 class="errormsg">Please double check the form for any required fields or errors you may have missed and complete them so we can go ahead and begin your debt relief consultation.</h2>'
		errorheader += '<h3 class="closewindow" style="padding-top:20px;">This message will automatically close in <b>8 seconds</b>. To <b>CLOSE</b> it manually click anywhere outside this window.</h3>';
		
		$.blockUI({ message: errorheader  }); 
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
		setTimeout($.unblockUI, 8000); 
		return false; 
	} 
	
	
	else { 
	
	var p1window;
	var successheader;
	successheader = '<h1 class="successform_v3">Your debt relief consultancy application is being processed. Please be patient as this process can take upto 90 seconds to complete! </h1>'; 	
	
	$.blockUI({ message: successheader, timeout: 30000  }); 

	

	return true; 
	
	} 
	
	
	
}



function field_change() {
	// alert(event.data.code)
	var field_name = $(this).attr('title');
	var optionobj = { minLength : 1, errorMsg : 'This field is required' };
	
	switch(field_name) 
	{
		case 'FirstName' : 
			 field_alphanumeric($(this),optionobj);
			 break;		 
		case 'LastName' : 
			 field_alphanumeric($(this),optionobj);
			 break;				
		case 'Email' : 
			var optionobj = { errorMsg : 'Valid email required' };
			 field_emailcheck($(this),optionobj);
			 break; 
		case 'Address' : 
			 field_alphanumeric($(this),optionobj);
			 break;		
		case 'City' : 
			 field_alphanumeric($(this),optionobj);
			 break;			
		case 'State' : 
			 field_alphanumeric($(this),optionobj);
			 break;				 
		case 'Zipcode' : 
		 	 var optionobj = { minLength : 5, errorMsg : 'Valid Zip Code required' };
			 field_alphanumeric($(this),optionobj);
			 break;		
		case 'HomePhone' : 
			 var optionobj = { minLength : 10, errorMsg : 'Valid Phone# required' };
			 field_phonecheck($("#id_HomePhone"),optionobj);
			 break;		
		case 'WorkPhone' :
		  	 var optionobj = { minLength : 10, errorMsg : 'Valid Phone# required' };
			 field_phonecheck($("#id_WorkPhone"),optionobj);
			 break;					 
		case 'ContactTime' : 
			 field_alphanumeric($(this),optionobj);
			 break;	 
		case 'AmountOwed' : 
			 field_alphanumeric($(this),optionobj);
			 break;				 
		case 'Income' : 
			 field_alphanumeric($(this),optionobj);
			 break;			
		case 'Home' : 
			 field_alphanumeric($(this),optionobj);
			 break;				 			 


	}
}


function fieldtriggers() { 	
	$("input.textinput").bind("focus",field_focus);
	$("select.textinput").bind("focus",field_focus);	

	$("input.textinput").bind("blur",field_normalize);
	$("select.textinput").bind("blur",field_normalize);	

	
	$("input.textinput").bind("change",field_change);
	$("input.textinput").bind("blur",field_change);
	$("select.textinput").bind("change",field_change);
	$("select.textinput").bind("blur",field_change);
}


$(document).ready(function() { 
	fieldtriggers();			
	
	
	// override these in your code to change the default behavior and style 
$.blockUI.defaults = { 
    // message displayed when blocking (use null for no message) 
    message:  '<h1>Please wait...</h1>', 
    css: { 
        padding:        20, 
        margin:         0, 
        width:          '600px', 
        top:            '20%', 
        left:           '30%', 
        textAlign:      'left', 
        color:          '#000', 
        border:         '3px solid #aaa', 
		'-webkit-border-radius': '10px', 
        '-moz-border-radius':    '10px', 		
        backgroundColor:'#fff', 
        cursor:         'wait' 
    }, 
 
    // styles for the overlay 
    overlayCSS:  { 
        backgroundColor: '#000', 
        opacity:         0.6 
    }, 
 
    growlCSS: { 
        width:    '350px', 
        top:      '10px', 
        left:     '', 
        right:    '10px', 
        border:   'none', 
        padding:  '5px', 
        opacity:   0.6, 
        cursor:    null, 
        color:    '#fff', 
        backgroundColor: '#000', 
        '-webkit-border-radius': '10px', 
        '-moz-border-radius':    '10px' 
    }, 
	
    iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank', 
    forceIframe: false, 
    baseZ: 1000, 
    centerX: true, // <-- only effects element blocking (page block controlled via css above) 
    centerY: true, 
    allowBodyStretch: true, 
    bindEvents: true, 
    constrainTabKey: true, 
    fadeIn:  200, 
    fadeOut:  300, 
    timeout: 0, 
    showOverlay: true, 
    focusInput: true,  
    applyPlatformOpacityRules: true, 
    onUnblock: null, 
    quirksmodeOffsetHack: 4 
}; 		

});