var NL = '\n';


/* Developer form validation and ajax submit */
function submitDeveloperForm(form) {

	var theform		= $(form);
	var errors		= [];
	var error_cnt 	= 0;

	// Turn off submit button, just in case
	//theform.select('INPUT[type=submit]').invoke('disable');

	// Clear any error fields
	theform.select('DIV.field_error').invoke('update');
	
	// Validate required fields
	theform.select('.required').each(function(field) {
		input = field.down('INPUT') || field.down('SELECT');
		if (!(input.value.strip()).length) {
			//var title = input.getAttribute('title');
			//errors.push('* '+title+' is a required field.');
			field.down('.field_error').update('Required');
			error_cnt++;
		}
	});

	// Validate email fields
	theform.select('.validate_email').each(function(field) {
		input = field.down('INPUT');
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(input.value)) {
			//var title = input.getAttribute('title');
			//errors.push('* '+title+' must be a valid email address.');
			field.down('.field_error').update('Invalid');
			error_cnt++;
		}
	});

	if (error_cnt > 0) {
	
		/*
		var msg = 'ERROR' + NL + 'There was a problem with your form. Please correct and submit again.' + NL;
		errors.each(function(e) { msg += NL + e; });
		alert(msg);
		*/

	} else {
	
		// Need to submit the form via hidden iframe. Ajax would fail because of cross-domain issue.

		var params 			= theform.serialize();
		var iframe_html 	= '<iframe src="http://ovi.tendercreative.com/index.php?'+params+'" class="hide" height="1" width="1"> </iframe>';
		
		$('layout').insert({bottom: iframe_html});

		// Update then show the confirmation modal
		$('modal_confirmation_companyname').update($F('fields_company'));
		toggleModal('confirmation', 'open');
		
		// Clear fields
		theform.select('INPUT[type=text]').invoke('clear');
		
		// Omniture Tracking
		s.pageName="ovi store:launch annoucement:developer:registered";
		s.events="event5";
		var s_code=s.t();
		if(s_code) document.write(s_code);

	}

	// Turn back on submit button
	//theform.select('INPUT[type=submit]').invoke('enable');

}		



function toggleModal(name, action) {

		var is_ie6 = (!window.XMLHttpRequest) ? true : false;

		switch(action) {
			case 'close':
				if (is_ie6) $$('SELECT').invoke('show');
				$('overlay', 'modal_'+name).invoke('hide');
				break;
			case 'open':

				if (is_ie6) {
					// Need to do some special stuff for ie6

					var arrayPageSize 	= getPageSize();
					var arrayPageScroll = getPageScroll();
					
					$('overlay').style.width 	= arrayPageSize[0] + 'px';
					$('overlay').style.height 	= arrayPageSize[1] + 'px';
					$('modal_'+name).style.top 	= (arrayPageScroll[1] + 100) + 'px';
					
					$$('SELECT').invoke('hide');
				}

				$('overlay', 'modal_'+name).invoke('show');
				break;
			default:
				break;
		}

}



//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	return new Array(pageWidth,pageHeight);
}

