var GCW = {		// base VC client object
	version: "0.001c",
	validation: {
		// this function unifies a form across server and client
		unify: function (form) {
			return o = form.serialize(true)
		}	
	},
	client: true,
	requestCount: 0,		// ajax request count
	callbacks: {},
	mpu: {}
}

GCW.alert = function(s){
	var pat=new RegExp(/MSIE 6.0/);
	var sTop = (pat.test(navigator.appVersion)==true) ? parseInt((document.viewport.getScrollOffsets().top + document.viewport.getHeight())-600) + "px" : parseInt((document.viewport.getHeight() - 300)/2) + "px"
	
	oFrame = new Element('DIV',{'class':'alertBox'}).setStyle({
		marginTop: sTop,
		marginLeft: parseInt((document.viewport.getWidth() - 294)/2) + "px"
	})
	
	oImg = new Element('IMG', {'class':'alertMClose','src':'/images/btn-sm-close.gif'})
	oHead = new Element('DIV', {'class':'alertHead'})
	oHead.innerHTML = 'Ooops!'
	oHead.appendChild(oImg)
	
	oP = new Element('P')
	oP.innerHTML = s 
	
	oImg = new Element('IMG', {'class':'alertClose','src':'/images/btn-ok.gif'})
	
	oFrame.appendChild(oHead)
	oFrame.appendChild(oP)
	oFrame.appendChild(oImg)
	
	$(document.body).overlay(oFrame)
}

document.observe("dom:loaded", function () {
	// hide elements for non js approach
	$$(".VC-NoJS").invoke("hide")
	
	// set up ajax responders
	Ajax.Responders.register({
		onCreate: function (xport) {
			document.body.setStyle({ cursor: "wait" })
			GCW.requestCount = GCW.requestCount +1
		},
		onComplete: function (xport) {
			if (!xport.success()) {
				alert("ERROR: " + (xport.getHeader("VC-Error") || "errorneous response from page"))
			}
			document.body.setStyle({ cursor: "" })
			GCW.requestCount = GCW.requestCount -1
		}
	})
	
	
	// navigation drop down
	if ($('navigation')) {
		var onav = $('navigation').getElementsByTagName('div')
		var sClass
		for(i=0;i<onav.length;i++){
			
			if(onav[i].getElementsByTagName('ul').length > 0){
				$(onav[i].id).observe('mouseover',function(){
					sClass = this.getElementsByTagName('ul')[0].className.replace(/hidden/,'')
					this.getElementsByTagName('ul')[0].className = sClass
				})
				
				$(onav[i].id).observe('mouseout',function(){
					this.getElementsByTagName('ul')[0].className += ' hidden'
				})
			}
		}
	}
	
	
	// init for rich's daddyTabs
	if($$('.tab')){
		$$('.tab').each(function(el){
			if(!el.className.match(/scTab/)){
				el.observe('click',function(){
					arrGrp = el.id.split("-")
					GCW.daddyTabs.openTab(el,arrGrp[0])
				})
			}
		})
	}

	// init for survey
	if($('vc_survey_open')){
		$('vc_survey_open').observe('click',function(){
			$('vc_survey_open').style.display = 'none'
			$('vc_survey').style.display = 'block'
		})
	}
	
	if($('surveyno_btn')){
		$('surveyno_btn').observe('click', function(){
			$('vc_survey').style.display = 'none'
		})
	}
	
	if($('frm_survey'))	{
		$('frm_survey').observe('submit', function(ev){
			ev.stop()
			
			if($('survey_stage').value == '2'){
				if(!GCW.validation.isValidEmail($('survey_email').value)){
					$('email_val').innerHTML = 'the email is not correctly formatted'
					//$('survey_email').flashBorder().focus()
					return false;
				}
			}
			
			new Ajax.Request("/server/surveyform.asp", {
				method: "get",
				parameters: {
					stg: $('survey_stage').value,
					qoc: $('cboQOC').value,
					dl: $('cboDL').value,
					age: $('cboAge').value,
					eou: $('cboEOU').value,
					va: $('cboVA').value,
					sex: $('cboSex').value,
					email: $('survey_email').value,
					surveyID: $('surveyID').value
				},
				onSuccess: function(xport){
					if($('survey_stage').value == '2'){
						$('frm_survey').style.display = 'none'
						$('survey_ty').style.display = 'block'
						var t=setTimeout("$('vc_survey').style.display = 'none'",4000)
					}
					if($('survey_stage').value == '1'){
						var response = xport.responseText.evalJSON(true)
						var surveyID = response.data.surveyID	
						$('surveyID').value =surveyID
						
						$('survey_stage').value = '2'
						$('survey_step1').style.display = 'none'					
						$('survey_step2').style.display = 'block'
						$('surveyno_btn').style.display = 'block'
						
					}
					
					
				}
			})
		})	
	}
	
	// init for the teaser panels 'a href' didnt cover the panel correctly when wrapped round div
	if($('carvaluesP')){
		$('carvaluesP').observe('click', function(){
			document.location = '/car-values.html'									   
		})
	}		
	if($('cardiaryP')){
		$('cardiaryP').observe('click', function(){
			document.location = 'https://www.visitcars.co.uk/dashboard/car-diary.html'									   
		})	
	}
	if($('mobileP')){
		$('mobileP').observe('click', function(){
			document.location = '/sell-my-car/create-mobile-car-advert.html'									   
		})	
	}
	if($('quotorP')){
		$('quotorP').observe('click', function(){
			document.location = '/motor-insurance/motor-insurance-quote.html'									   
		})	
	}
	
	// msgbox transparency
	var trans = $$(".semitrans")
	trans.each(function(x) { x.setStyle({ opacity: .6 }) })

})

// rollover
function sgRoll(oImg) {
	if (oImg.src.indexOf("-a.gif") == -1) {
		oImg.src = oImg.src.replace(".gif", "-a.gif");
	} else {
		oImg.src = oImg.src.replace("-a.gif", ".gif");
	}
	if (oImg.src.indexOf("-a.jpg") == -1) {
		oImg.src = oImg.src.replace(".jpg", "-a.jpg");
	} else {
		oImg.src = oImg.src.replace("-a.jpg", ".jpg");
	}
	if (oImg.src.indexOf("-a.png") == -1) {
		oImg.src = oImg.src.replace(".png", "-a.png");
	} else {
		oImg.src = oImg.src.replace("-a.png", ".png");
	}
}


document.observe('dom:loaded', function() {

	$$(".rollover").each(function(el) {
		el.observe("mouseover", sgRoll.curry(el))
		el.observe("mouseout", sgRoll.curry(el))
	})
})

