// JavaScript API to the Contact Information Form
// Copyright 2007, VentureDirect Worldwide, Inc.
// Authors: Jerry Kapron

// BEGIN: include 'cif_api.js';
	
	window.focus();
	
	document.lastZipChecked = '';
	document.allowZipBlur = true;
	
	
	function onBlurZip () {
		
		if( cif.controls.zip.value.length > 0 && cif.controls.zip.value.length < 5 ) {
			
			document.allowZipBlur = false;			
			alert( 'The Zip Code you entered is incomplete!' );
			cifFocus( 'zip' );

			
		} else {
			
			document.allowZipBlur = true;
		}
		
	}
	

	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/;domain=.directdegree.com";
	}
	
	
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}




	
	
	function cifFocus( control_id ) {
		
		var control = cif.controls[ control_id ]

		for ( var i in cif.controls ) {
			
			if ( i == control_id ) {
				
				if( i != 'optin' ) {
					
					cif.controls[i].style.backgroundColor = '#cfc'
					cif.controls[i].style.border = '1px solid #666'
					cif.controls[i].style.borderRight = '3px solid #333'
					cif.controls[i].style.backgroundImage = 'url(/img/text_input_left_pointer.gif)'
					cif.controls[i].style.backgroundRepeat = 'no-repeat'
					cif.controls[i].style.backgroundPosition = '0px 0px'
					
				}
				
				cif.controls[i].focus();
		
			} else {
				
				cif.controls[i].style.backgroundColor = '#eee'
				cif.controls[i].style.border = '1px solid #999'
				cif.controls[i].style.backgroundImage = ''
			}
		}
	}
	
	
	// default event handler methods
	
	function onNewZip() {	};
	
	function saluteOnChange() {  }
	function fnameOnChange() {  }
	function lnameOnChange() {  }
	function address1OnChange() {  }
	function address2OnChange() {  }
	function cityOnChange() {  }
	function stateOnChange() {  }
	function zipOnChange() {  }
	function phoneOnChange() {  }
	function emailOnChange() {  }
	function confirmemailOnChange() {  }
	function optinOnChange() {  }			

	/*****************************************************************************/	
	/*****************************************************************************/	
	 function JSONscriptRequest(fullUrl) {
	
	  // REST request path
	  this.fullUrl = fullUrl;
	  // Keep IE from caching requests
	  this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
	  // Get the DOM location to put the script tag
	  this.headLoc = document.getElementsByTagName("head").item(0);
	  // Generate a unique script tag id
	  this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
	
	}
	
	
	/*****************************************************************************/	
	/*****************************************************************************/	
	// Static script ID counter
	JSONscriptRequest.scriptCounter = 1;
	/*****************************************************************************/	
	/*****************************************************************************/	
	// buildScriptTag method
	JSONscriptRequest.prototype.buildScriptTag = function () {
	  // Create the script tag
	  this.scriptObj = document.createElement("script");
	
	  // Add script object attributes
	  this.scriptObj.setAttribute("type", "text/javascript");
	  this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	  this.scriptObj.setAttribute("id", this.scriptId);
	}
	
	
	/*****************************************************************************/	
	/*****************************************************************************/	
	// removeScriptTag method
	JSONscriptRequest.prototype.removeScriptTag = function () {
	  // Destroy the script tag
	  this.headLoc.removeChild(this.scriptObj);
	}
	
	
	/*****************************************************************************/	
	/*****************************************************************************/	
	// addScriptTag method
	JSONscriptRequest.prototype.addScriptTag = function () {
	  // Create the script tag
	  this.headLoc.appendChild(this.scriptObj);
	
	}




	function getCs( zipCode ) {
		request = 'http://scholarshipexplorer.com/api/ajax/getCsByZip.json.php?zip=' + zipCode + '&callback=popCs';
		aObj = new JSONscriptRequest(request);
		aObj.buildScriptTag();
		aObj.addScriptTag();
	}

	function popCs( cszObj ) {
		
		if( cszObj.city == '' && cszObj.state == '' ) {
			
			alert( 'Zip Code does not exist!' );
			cif.controls.zip.focus();
			return;
		}
		cif.controls.city.value = cszObj.city
		
		for( var sI=0; sI < cif.controls.state.options.length; sI++ ) {
			
			if( cif.controls.state.options[ sI ].value.substring( 0,2 ) == cszObj.state ) {
				cif.controls.state.selectedIndex = sI
				break;
			}
			
		}
		
		cif.controls.email.focus()
	}

	function qVar( variable ) {
		var query = window.location.search.substring( 1 );
		var vars = query.split( '&' );
		for ( var i=0; i < vars.length; i++ ) {
			var pair = vars[i].split( '=' );
			if ( pair[0] == variable ) {
				return pair[1];
			}
		} 
		return ''
	}





	//if ( qVar('debug')==1 ) {
	if ( 1 ) {
	
		var cif = new Object();
		cif.rows = new Object();
		cif.controls = new Object();
		cif.rowIndex = new Object();
		
		theForm = document.forms[0]
		
		function getThatTable() { return theForm.getElementsByTagName('table')[0] }
		
		theTable = theForm.getElementsByTagName('table')[0]
		inputs = theTable.getElementsByTagName('input')
		rows = theTable.getElementsByTagName('tr')
		
		populated = true
		
		// preCreate the Show/Hide Button
		controlDiv = document.createElement('div')
		controlDiv.innerHTML = '- Hide Contact Info';
		controlDiv.style.height = '12px'
		controlDiv.style.width = '125px'
		controlDiv.style.border = '1px solid #999'
		controlDiv.style.fontSize = '10px'
		controlDiv.style.fontFamily = 'Verdana'
		controlDiv.style.backgroundColor = '#ddd'
		controlDiv.style.textAlign = 'center'
		controlDiv.style.marginLeft = '7px'
		controlDiv.style.cursor = 'pointer';
		
		
		

		// Event Handlers for the Show/Hide Button
		function hideCif () {
			collapseButt.innerHTML = '+ Show Contact Info';
			collapseButt.onclick = function () { showCif() }
			theTable.style.display = 'none'
		}
		
		function showCif () {
			collapseButt.innerHTML = '- Hide Contact Info';
			collapseButt.onclick = function () { hideCif() }
			theTable.style.display = 'block'
		}
		
		


		theTable.setAttribute('cellSpacing', 3);
		
		
		// Populate cif.rowIndex, cif.rows, cif.controls objects
		for ( var i=0; i < rows.length; i++ ) {
			
			
			if ( rows[i].getElementsByTagName('input').length ) {
				
				
				cif.rows[ rows[i].getElementsByTagName('input')[0].getAttribute('name') ] = rows[i]
				cif.controls[ rows[i].getElementsByTagName('input')[0].getAttribute('name') ] = rows[i].getElementsByTagName('input')[0]
				cif.rowIndex[ rows[i].getElementsByTagName('input')[0].getAttribute('name') ] = i
				
				
				
			} else if ( rows[i].getElementsByTagName('select').length ) {
				
				cif.rows[ rows[i].getElementsByTagName('select')[0].getAttribute('name') ] = rows[i]
				cif.controls[ rows[i].getElementsByTagName('select')[0].getAttribute('name') ] = rows[i].getElementsByTagName('select')[0]
				cif.rowIndex[ rows[i].getElementsByTagName('select')[0].getAttribute('name') ] = i
			}
			
			
		}
		
		
		
		
		// Move the ZIP ROW	
		theZip = theTable.getElementsByTagName('tbody')[0].removeChild( cif.rows.zip )
		cif.rows.zip = theTable.getElementsByTagName('tbody')[0].insertBefore( theZip, cif.rows.city )
		
			
		
		
		// Add styles, attributes and event handlers to all CONTROLS
		for ( var i in cif.controls ) {
		
				
			cif.controls[i].onchange = eval ( i + 'OnChange' );


			cif.controls[i].style.fontSize = '11px'
			cif.controls[i].style.fontFamily = 'Arial'
			cif.controls[i].style.color = '#004'
			
			if( i != 'optin' && i != 'salute' ) { cif.controls[i].style.width = '160px' }
			
			
			if( i != 'optin' ) {
				
				cif.controls[i].style.backgroundColor = '#eee'
				cif.controls[i].style.border = '1px solid #999'
				cif.controls[i].style.paddingLeft = '8px'

				
				cif.controls[i].onfocus = function () {
					if ( document.allowZipBlur ) {
						
						this.style.backgroundColor = '#cfc'
						this.style.border = '1px solid #666'
						this.style.borderRight = '3px solid #333'
						this.style.backgroundImage = 'url(/img/text_input_left_pointer.gif)'
						this.style.backgroundRepeat = 'no-repeat'
						this.style.backgroundPosition = '0px 0px'
						
					} else {
						
						cifFocus( 'zip' );
					}
				}
				
				cif.controls[i].onblur = function () {
					this.style.backgroundColor = '#eee'
					this.style.border = '1px solid #999'
					this.style.backgroundImage = ''
					if ( this == cif.controls.zip ) {
						return onBlurZip();
					}
				}
				

				
			
			}	
			
			if( i != 'optin' && i != 'salute' && i != 'address2' ) {
				
				populated = populated && ( cif.controls[i].value.length > 0 )

			}
			
		}
		
		
		// Add Show/Hide button + Hide the CIF
		if ( populated ) {
			
			collapseButt = theForm.insertBefore( controlDiv, theTable )
			//collapseButt.onclick = function () { hideCif() }
			
			hideCif ()
			
		}
		
		cif.controls.salute.style.width = '50px'
		
		
		
		
		// Add style, attributes and other changes to all ROWS
		for ( var i in cif.rows ) { 
		
		
			if( i != 'optin' ) {
				cif.rows[i].getElementsByTagName('td')[0].setAttribute('noWrap', 'noWrap')
				cif.rows[i].getElementsByTagName('td')[0].innerHTML = cif.rows[i].getElementsByTagName('td')[0].innerHTML.replace(/<[^>]*>/g, '')
				cif.rows[i].getElementsByTagName('td')[0].innerHTML = cif.rows[i].getElementsByTagName('td')[0].innerHTML.replace(/&nbsp;/g, '')
				cif.rows[i].getElementsByTagName('td')[0].innerHTML = cif.rows[i].getElementsByTagName('td')[0].innerHTML.replace(/\*/, '<font color="#FF0000">*</font>')
				cif.rows[i].getElementsByTagName('td')[0].setAttribute('align', 'right')
				cif.rows[i].getElementsByTagName('td')[0].style.textAlign = 'right'
			}
			
			cif.rows[i].getElementsByTagName('td')[0].style.fontSize = '10px'
			cif.rows[i].getElementsByTagName('td')[0].style.fontFamily = 'Verdana, Arial'
			cif.rows[i].getElementsByTagName('td')[0].style.fontWeight = 'normal'
			
		}




		// zip control attributes
		cif.controls.zip.setAttribute( 'maxLength', 5 )
		cif.controls.zip.maxlength = 5;
		cif.controls.zip.setAttribute( 'tabIndex', 25 )
		
		
				
		
		// zip control event handlers
		cif.controls.zip.onkeyup = function () {
			if( this.value.length == 5 && this.value != document.lastZipChecked ) {
				
				document.lastZipChecked = this.value
				getCs( this.value );
				onNewZip();
				
			}
		}
	
		cif.controls.zip.onkeypress = function ( evt ) {
			
			if( window.ActiveXObject ) {
				
				if( !evt ) var evt = window.event;
				
				if( evt.keyCode && ( ( evt.keyCode < 48 || evt.keyCode > 57 ) && evt.charCode != 0 ) ) {
					evt.returnValue = false
					//return false
				}
				
			} else {
				
				if( ( evt.which < 48 || evt.which > 57 ) && evt.charCode != 0 ) {
					return false
				}
				
			}
			
		}
		

		
		//cif.controls.address2.onchange =  eval ( 'address2' + 'OnChange' )
		//function saluteOnChange() { alert ('Debugging....') }
		
		//cif.controls.salute.onchange = saluteOnChange;
		
	}
	
	
	
	
	
	// Affiliate Code Fix
	if ( qVar( 'code' ) != '' ) {
		
		createCookie( 'code', qVar( 'code' ), 0 )
		
	} else if ( readCookie( 'code' ) == null || readCookie( 'code' ) == '0000' ) {
		
		createCookie( 'code', 'VEN-NST', 9999 )
		
	}

	cif.controls.state.style.fontSize = '10px'
	cif.controls.state.style.width = '175px'

	//alert( cif.controls.code.value )
	
	
	
	for(var si=0;si < cif.controls.state.options.length;si++) {
		cif.controls.state.options[si].innerHTML = cif.controls.state.options[si].innerHTML.replace(/ \[USA\]/gi, '');
		cif.controls.state.options[si].innerHTML = cif.controls.state.options[si].innerHTML.replace(/ \(except Canada\)/gi, '');
	}
	cif.controls.state.options[0].innerHTML = ' - select state - '
	
	
//	document.write( typeof cif.controls.fname )

	
	
// END: include 'cif_api.js';
