var ajxloader = new Image();
ajxloader.src = '/images/ajax-loader.gif';
var ccode = '';

function previewAD() {
var vv = validateSell();
if (vv == '') {
dat = genXML() + '<action>Preview</action>';
postXML('Create',dat,function() {prevReturn();});
} else {
alert("Cannot continue, please double check that you have entered the required information:\n" + vv);	
}	
}
function createAD() {
var vv = validateSell();
if (vv == '') {
dat = genXML() + '<action>Create</action>';
postXML('Create',dat,function() {prevReturn();});
} else {
alert("Cannot continue, please double check that you have entered the required information:\n" + vv);	
}	
}


function validateSell() {
var elemid = "new";
var validstr = '';
if (filedata != '') {validstr += 'In order to upload your image please click the upload button before proceeding.\n';}
if (getInputVal('name_new').length < 5) {validstr += 'Product name must be at least 5 characters.\n';}
if (!IsNumeric(getInputVal('price_new'))) {validstr += 'Price must be a numeric value.\n';}
if (getInputVal('des_new').length < 10) {validstr += 'Product description must be at least 10 characters.\n';}
var ptype="ud";
if (document.getElementById('contact_' + elemid + '_both').checked) {
ptype = 0;
}else if (document.getElementById('contact_' + elemid + '_phone').checked) {var ptype = 1;} else if(document.getElementById('contact_' + elemid + '_mobile').checked) {var ptype = 2;}
if (ptype == 'ud') {validstr += 'Please select a contact method.';}
return validstr;
}


function prevReturn() {
if (postxml.readyState == 4) {
  		if (postxml.status == 200) {
  			resp_text = postxml.responseText;
  			document.getElementById('hidden_div').innerHTML = resp_text;
  			openlw();
}
}
}

function openlw() {
	var loc = window.location.href.replace( /#.*/, "");
	loc = loc + "#hidden_div";
  			myLightWindow.activateWindow({href:loc, title: '', params:'lightwindow_height=520,lightwindow_width=550'});
}
function genXML() {
var xml = '';

xml += '<name>' + cdata(getInputVal('name_new')) + '</name>';
xml += '<price>' + cdata(getInputVal('price_new')) + '</price>';
xml += '<category>' + cdata(getSelectVal('category_new')) + '</category>';
xml += '<condition>' + cdata(getSelectVal('condition_new')) + '</condition>';
xml += '<suffix>' + cdata(getSelectVal('pricesuff_new')) + '</suffix>';

xml += '<description>' + cdata(getInputVal('des_new')) + '</description>';
xml += '<brand>' + cdata(getInputVal('brand_new')) + '</brand>';
xml += '<model>' + cdata(getInputVal('model_new')) + '</model>';

elemid = 'new';
if (document.getElementById('contact_' + elemid + '_both').checked) {
var ptype = 0;
}else if (document.getElementById('contact_' + elemid + '_phone').checked) {var ptype = 1;} else {var ptype = 2;}
	
xml += '<typ>' + ptype + '</typ>';

return xml;
}

function submitAD() {
dat = genXML();
dat += '<ccode>' + cdata(ccode) + '</ccode>';
postXML('CreateDone',dat,function() {createdReturn();});
}
function createdReturn() {
if (postxml.readyState == 4) {
  		if (postxml.status == 200) {
  			resp_text = postxml.responseText;
  			if (resp_text == 'warncc') {
  			document.getElementById('ccwarn').innerHTML = "<br/>Invalid security code, please try again.";
  			} else {
  			document.getElementById('adWindow').innerHTML = resp_text;
  		}
  			openlw();
}
}
}
